Showing posts with label unix shell programs. Show all posts
Showing posts with label unix shell programs. Show all posts

Sunday, November 15, 2009

unix shell programs ,shell script ,basic examples

-->
//Date :
Shell script to find simple Interest
Shell script :
read p
read n
read r
si=`expr $p \* $n \* $r`
#si=`expr $si / 100`
PI=$(echo "scale=6; $si / 100" | bc)
#echo $si
echo $PI
//Date :
Shell script to find leap year
Shell script :
yy=0
isleap="false"
echo -n "Enter year (yyyy) : "
read yy
# find out if it is a leap year or not
if [ $((yy % 4)) -ne 0 ] ; then
# not a leap year : means do nothing and use old value of isleap
elif [ $((yy % 400)) -eq 0 ] ; then
# yes, it's a leap year
isleap="true"
elif [ $((yy % 100)) -eq 0 ] ; then
# not a leap year do nothing and use old value of isleap
else
# it is a leap year
isleap="true"
fi
if [ "$isleap" == "true" ];
then
echo "$yy is leap year"
else
echo "$yy is NOT leap year"
fi
//Date:
Shell script to find the number is odd or even
Shell script :
echo -n "Enter numnber : "
read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is even number"
else
echo "$n is odd number"
fi
//Date :
Shell script to perform arithmetic operations
Shell script :
a=$1
op="$2"
b=$3
if [ $# -lt 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
//Date:
Shell script to print Fibonacci series
Shell script :
i=1
a=0
b=1
echo ${a},
while [ $i != 10 ]
do
b=`expr $a + $b`
echo ${b},
c=$a
a=$b
b=$c
i=`expr $i + 1`
done
//Date :
Menu Driven shell script
Shell script :
while :
do
clear
echo " M A I N - M E N U"
echo "1. Date"
echo "2. List of users currently logged"
echo "3. Present handling directory"
echo "4. List of files"
echo "5. Exit"
echo -n "Please enter option [1 - 5]"
read opt
case $opt in
1) echo " $(date) ";
read enterKey;;
2) echo "*********** List of users currently logged $(who) ";
read enterKey;;
3) echo "You are in $(pwd) directory";
echo "Press [enter] key to continue. . .";
read enterKey;;
4) echo " $(ls) ";
read enterKey;;
5) exit 1;;
*) echo "$opt is an invaild option. Please select option between 1-4 only";
echo "Press [enter] key to continue. . .";
read enterKey;;
esac
done
//Date:
Shell script to print sum of digits
Shell script :
read n
sum=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo "Sum of digit for numner is $sum"
//Date:
Shell script to find biggest of three numbers
Shell script :
read n1
read n2
read n3
if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
then
echo "$n1 is Bigest number"
elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ]
then
echo "$n2 is Bigest number"
elif [ $n3 -gt $n1 ] && [ $n3 -gt $n2 ]
then
echo "$n3 is Bigest number"
elif [ $1 -eq $2 ] && [ $1 -eq $3 ] && [ $2 -eq $3 ]
then
echo "All the three numbers are equal"
else
echo "I cannot figure out "
fi
//Date :
Shell script to find factorial of a number
Shell script:
read n
i=1
j=1
k=0
while [[ $n -gt 1 ]]
do
j=`expr $n - 1`
i=`expr $n \* $j`
n=`expr $n - 1`
k=`expr $k + $i`
done
echo " $k "
Sequential stage in datastage