Tuesday, April 20, 2010

Shell Script

Shell script to simulate a simple calculation:

a=$1
op="$2"
b=$3

if[$ # -It 3]
then
echo "$0 num1 opr num2"
echo "opr can be +,-,/,x"
exit 1
fi
case "$op" in
+)echo$(($a+$b));;
-)echo$(($a-$b));;
/)echo$(($a/$b));;
x)echo$(($a*$b));;
*)echo "error";;
esac

Shell script to find largest among 3 integers:

a=$1
op="$2"
b=$3

if[$ # -It 3]
then
echo"$0 n1 n2 n3"
exit 1
fi
if[$a -gt $b -a $a -gt $c]
then
echo "$a is largest"
elif[$a -gt $b -a $b -gt $c]
then
echo "$b is largest"
elif[$a -gt $b -a $c -gt $c]
echo "$c is largest"
else
echo"sorry cannot guess number"
fi


Shell script to sort numbers in file in ascending order:

file=" "
echo -n "enter file name:"
read file
if[! -f $file]
then
echo "$file not a file"
exit 1
fi
sort -n $file


Copy File:

echo"enter sorce and target file names"
read source target
if CP $ source $ target
then
echo
echo "file copied successfully"
else "error in file copy"
fi

0-10 Number-Guess No.:
echo "enter a number from 0 to 10"
read num
if test $num -eq 5
then
echo"guess successfully"
else
echo"try again"
fi

Display 1-10 Numbers:
i=1
until[$i -gt 10]
do
echo $i
i='expr $i +1'
done
 

0 comments:

Post a Comment

 
ShareThis

Visitor

Website counter
Copyright 2009 Code's. Powered by Blogger
Blogger Templates created by Deluxe Templates
Wordpress by Wpthemescreator
Blogger Showcase