Monday 28 October 2013

Bash Exercise - 7

Script 31: Leap year
#!/bin/bash
# This script will test if we're in a leap year or not.
year=`date +%Y`
if [ $[$year % 4] -eq 0 ]

then
echo "This is a leap year, February has 29 days."
else
echo "This is not a leap year. February has 28 days."
fi
else
echo "This is not a leap year. February has 28 days."
fi

Script 32: If – Else Statement
#!/bin/bash
echo "enter some integer"
read a
echo "enter some integer"
read b
c=30
if test $(($a+$b)) -eq $c
then
echo "equal"
else
echo "not equal"
fi
Script 33: Nested If-Else
if test $1 -gt $2
then
echo $1 is greater than $2
elif test $1 -lt $2
then
echo $1 is lesser than $2
fi
Script 34: Let
#!/bin/bash
#letting "lett" do arithmetic
echo

let a=11 # Same as 'a=11'
let a=a+5 # Equivalent to let "a = a + 5"
# (Double quotes and spaces make it more readable.)
echo "11 + 5 = $a" # 16

let "a <<= 3" # Equivalent to let "a = a << 3"
echo "\"\$a\" (=16) left-shifted 3 places = $a"
# 128

let "a /= 4" # Equivalent to let "a = a / 4"
echo "128 / 4 = $a" # 32

let "a -= 5" # Equivalent to let "a = a - 5"
echo "32 - 5 = $a" # 27

let "a *= 10" # Equivalent to let "a = a * 10"
echo "27 * 10 = $a" # 270

let "a %= 8" # Equivalent to let "a = a % 8"
echo "270 modulo 8 = $a (270 / 8 = 33, remainder $a)"
# 6

echo

exit 0
Script 35: Mail
`rm -rf file file1`
`rm -rf file.log`
df -h | sort -rnk5 | awk '{print $5,$6}' | sed '$d' > file
df -h | sort -rnk5 | awk '{print $5,$6}' | awk 'BEGIN { FS="%" } { print $1 }'| sed '$d' > file1
rm -f file.log
a=0
for i in `cat file1`
do
a=$(($a+1))
if [ $i -ge 70 ]
then
##echo `sed -n ''$a'p' file | awk '{print $2}'`
`sed -n ''$a'p' file >> file.log`
#echo `sed -n ''$a'p' file `
fi
done
cat file.log
mail -v -s "disk usage" $1@nerds < /root/file.log

No comments :

Post a Comment