SHELL PROGRAMMING
// Shell
programs
// IF STATEMENT
// Even or Odd
echo -n "Enter a number : "
read n
if [ `expr $n % 2` -eq 0 ]
then
echo "$n is Even"
else
echo "$n is Odd"
fi
// Positive
negative or zero
echo -n "Hello "; logname
echo -n "enter a number : "
read a
if [ $a -lt 0 ]
then
echo " $a is
negetive"
elif [ $a -gt 0 ]
then
echo "$a is
Positive"
else
echo "$a is Zero"
fi
// Biggest
if three numbers
echo -n "Enter three numbers : "
read a b c
if [ $a -gt $b ]
then
if [ $a -gt $c ]
then
echo "Biggest is $a"
else
echo "Biggest is $c"
fi
else
if [ $b -gt $c ]
then
echo "Biggest is $b"
else
echo "Biggest is $c"
fi
fi
//SWITCH
STATEMENTS
//
Arithmetic Operations
echo "Menu"
echo "1 - Addition"
echo "2 - Subtraction"
echo "3 - Multiplication"
echo "4 - Division"
echo -n "Enter your choice : "
read n
echo -n "Enter the numbers : "
read a b
case $n in
1) echo "Sum of $a and $b is `expr $a + $b`";;
2) echo "Difference of $a and $b is `expr $a - $b`";;
3) echo "Product of $a and $b is `expr $a \* $b`";;
4) echo "Division of $a by $b is `expr $a / $b`";;
*) echo "Invalid choice";;
esac
// Number –
words
echo -n "Enter the number less than 10 : "
read n
case $n in
1) echo "$n - ONE";;
2) echo "$n - TWO";;
3) echo "$n - THREE";;
4) echo "$n - FOUR";;
5) echo "$n - FIVE";;
6) echo "$n - SIX";;
7) echo "$n - SEVEN";;
8) echo "$n - EIGHT";;
9) echo "$n - NINE";;
0) echo "$n - ZERO";;
*) echo "Invalid choice";;
esac
//WHILE
LOOP
// Squares
and Cubes up to given number
echo -n "Enter a number : "
read n
echo -e "\t Number \t Square \t Cube"
i=1
while [ $i -le $n ]
do
sq=`expr $i \* $i`
cu=`expr $i \* $sq`
echo -e "\t $i \t\t $sq
\t\t $cu "
i=`expr $i + 1`
done
// Reverse
a number
echo -n "Enter a number : "
read n
ori=$n
i=0
while [ $n -gt 10 ]
do
mod=`expr $n % 10`
i=`expr $i \* 10 + $mod`
n=`expr $n / 10`
done
i=`expr $i \* 10 + $n`
echo "Entered Number : $ori"
echo "Reversed Number : $i"
// Number
of digits
echo -n "Enter a number : "
read n
i=1
while [ $n -gt 10 ]
do
n=`expr $n / 10`
i=`expr $i + 1`
done
echo “Number of digits is $i”
// FOR LOOP
// Read and
display array.
// Find
biggest and smallest in array
echo "Enter 5 values : "
for i in 0 1 2 3 4
do
read a[$i]
done
echo "Entered values are "
for (( i=0; i<5; i++))
do
echo -e "\t
${a[$i]}"
done
big=${a[0]}
small=${a[0]}
for ((i=1;i<5;i++))
do
if [ $big -lt ${a[i]} ]
then
big=${a[$i]}
fi
if [ $small -gt ${a[$i]} ]
then
small=${a[$i]}
fi
done
echo "Biggest is $big"
echo "Smallest is $small"
// Sorting 5 numbers
echo "Enter 5 values : "
for i in 0 1 2 3 4
do
read a[$i]
done
echo "Entered Values are "
for (( i=0; i<5; i++))
do
echo -e "\t
${a[$i]}"
done
// Sorting loop
for ((i=0;i<4;i++))
do
for ((j=`expr $i + 1`; j<5;j++))
do
if [ ${a[$j]} -lt ${a[i]} ]
then
t=${a[$i]}
a[$i]=${a[$j]}
a[$j]=$t
fi
done
done
// Output
echo "Sorted Numbers : "
for ((i=0;i<5;i++))
do
echo -e "\t
${a[$i]}"
done
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu