Shell Programming

Only available on StudyMode
  • Download(s) : 59
  • Published : June 15, 2011
Open Document
Text Preview
1) Shell Program To check the greater number between two numbers #!/bin/sh
[sourcecode language='sh']
echo Enter a Number
read num1
echo Enter Number
read num2
if test $num1 -ge $num2
then echo $num1 is greater than $num2
else
echo $num2 is greater than $num1
fi

2) Shell Program to Check a number is even or odd
[sourcecode language='sh']
echo Enter a Number to Find Even or odd
read num
b=`expr $num % 2`
if [ $b -eq 0 ]
then
echo Even number
else
echo Odd Number
fi

3) Shell Program to check whether a year is leap or Not
[sourcecode language='sh']
echo Enter the year to check for Leap
read year
leap=`expr $year % 4`
check1=`expr $year % 100`
if [ $check1 -eq 0 ]
then
check2=`expr $year % 400 | bc`
if [ $check2 -eq 0 ]
then
echo Leap
else
echo Not Leap
fi
break;
fi
if [ $leap -eq 0 -a $check1 -ne 0 ]
then
echo Leap
fi

4) Shell Program to Find a File exists in the present working directory echo Enter a File Name
read filename
if [ -s $filename ]
then
if [ -f $filename ]
then
echo File Exists…
else
echo Directory Exists…
fi
ls $filename -n
else
echo Not found
fi
[/sourcecode]
5) Shell Program To copy a file into another file
echo Enter Source File Name Destination File name
read srcfile destfile
cp $srcfile $destfile
echo Copy Complete…
echo $destfile “Is a Copy of ” $srcfile

6) How to write shell script that will add two nos, which are supplied as command line argument, and if this two nos are not given show error and its usage

if [ $# -ne 2 ]
then
echo "Usage - $0 x y"
echo " Where x and y are two nos for which I will print sum" exit 1
fi
echo "Sum of $1 and $2 is `expr $1 + $2`"

7) Write Script to find out biggest number from given three nos. Nos are supplies as command line argument. Print error if sufficient arguments are not supplied. # Algo:
# 1) START: Take three nos as n1,n2,n3.
# 2) Is n1 is greater than n2 and n3, if yes
# print n1 is bigest no goto step 5, otherwise goto next step # 3) Is n2 is greater than n1 and n3, if yes
# print n2 is bigest no goto step 5, otherwise goto next step # 4) Is n3 is greater than n1 and n2, if yes
# print n3 is bigest no goto step 5, otherwise goto next step # 5) END
#
#

if [ $# -ne 3 ]
then
echo "$0: number1 number2 number3 are not given" >&2
exit 1
fi
n1=$1
n2=$2
n3=$3
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 can not figure out which number is biger" fi

8) Write script to print nos as 5,4,3,2,1 using while loop.

# Algo:
# 1) START: set value of i to 5 (since we want to start from 5, if you # want to start from other value put that value)
# 2) Start While Loop
# 3) Chechk, Is value of i is zero, If yes goto step 5 else # continue with next step
# 4) print i, decement i by 1 (i.e. i=i-1 to goto zero) and # goto step 3
# 5) END
#
i=5
while test $i != 0
do
echo "$i
"
i=`expr $i - 1`
done

9)Write Script, using case statement to perform basic math operation as follows
+ addition
- subtraction
x multiplication
/ division

if test $# = 3
then
case $2 in
+) let z=$1+$3;;
-) let z=$1-$3;;
/) let z=$1/$3;;
x|X) let z=$1*$3;;
*) echo Warning - $2 invalied operator, only +,-,x,/ operator allowed
exit;;
esac
echo Answer is $z
else
echo "Usage - $0 value1 operator value2"
echo " Where, value1 and value2 are numeric values"
echo " operator can be +,-,/,x (For...
tracking img