Вы находитесь на странице: 1из 22

SIMPLE SHELL PROGRAM

SHELL PROGRAM:

echo "Please enter your name"


read name
echo "Hi !! Welcome to shell script $name"

OUTPUT:

Please enter your name


ECE
Hi !! Welcome to shell script ECE
SUM OF TWO NUMBERS

SHELL PROGRAM:

echo "Enter the values of x and y"


read x y
sum=`expr $x + $y`
echo "The sum is $sum"

OUTPUT:

Enter the values of x and y


12 13
The sum is 25
BIGGEST AMONG TWO NUMBERS

SHELL PROGRAM:

echo "Enter two values"


read a b
if [ $a -le $b ]
then echo "$b is biggest"
else
echo "$a is biggest"
fi

OUTPUT:

Enter two values


55 75
75 is biggest
ODD OR EVEN

SHELL PROGRAM:

echo "Enter the number"


read n
a=`expr $n % 2`
if [ $a -eq 0 ]
then
echo "$n is even"
else
echo "$n is odd"
fi

OUTPUT:

Enter the number

45

45 is odd
ILLUSTRATION OF CASE CONSTRUCTS

SHELL PROGRAM:

echo "Menu"
echo "1.Your current directory"
echo "2.Today's date"
echo "3.List of user's Logged in"
echo "Your choice"
read choice
case $choice in
1) pwd;;
2) date;;
3) who;;
*) echo "Invalid choice";;
esac

OUTPUT:

Menu
1.Your current directory
2.Today's date
3.List of user's Logged in
Your choice
1
/home/administrator

Menu
1.Your current directory
2.Today's date
3.List of user's Logged in
Your choice
2
Thu Apr 7 10:44:19 EDT 2011

Menu
1.Your current directory
2.Today's date
3.List of user's Logged in
Your choice
3
administrator tty7 2011-04-07 10:10 (:0)
administrator pts/0 2011-04-07 10:40 (:0.0)
FINDING THE SQUARE OF NUMBERS USING FOR LOOP

SHELL PROGRAM:

for k in 1 2 3 4 5
do
echo "The Number is $k"
echo "Square of the number is `expr $k \* $k`"
done

OUTPUT:

The Number is 1
Square of the number is 1
The Number is 2
Square of the number is 4
The Number is 3
Square of the number is 9
The Number is 4
Square of the number is 16
The Number is 5
Square of the number is 25
HRA GENERATION

SHELL PROGRAM:

echo "Enter employee basic salary"


read basic
if [ $basic -gt 5000 ]
then
HRA=`expr $basic / 5`
echo The HRA of employee is $HRA
elif [ $basic -ge 4000 -a $basic -le 5000 ]
then
HRA=`expr $basic / 4`
echo The HRA of employee is $HRA
else
HRA=`expr $basic / 10`
echo The HRA of employee is $HRA
fi

OUTPUT:

Enter employee basic salary

7000

The HRA of employee is 1400


MENU DRIVEN PROGRAM WITH UNIX COMMANDS

SHELL PROGRAM:

echo "Menu"
echo "1.Displays a long listing of files"
echo "2.Deletes files from the directory"
echo "Please enter your choice"
read choice
case $choice in
1) ls -l;;
2) echo "Enter filename to be deleted"
read file
rm $file
echo "$file has been deleted";;
*) echo "invalid choice";;
esac

OUTPUT:

Menu
1.Displays a long listing of files
2.Deletes files from the directory
Please enter your choice
1
total 36
drwxr-xr-x 3 administrator administrator 4096 2011-04-07 10:25 Desktop
drwxr-xr-x 2 administrator administrator 4096 2011-04-07 10:10 Documents
drwxr-xr-x 2 administrator administrator 4096 2011-04-07 10:10 Downloads
drwxr-xr-x 2 administrator administrator 4096 2011-04-07 10:10 Music
drwxr-xr-x 2 administrator administrator 4096 2011-04-07 10:10 Pictures
drwxr-xr-x 2 administrator administrator 4096 2011-04-07 10:10 Public
-rw-r--r-- 1 administrator administrator 297 2011-04-07 10:50 sh1.sh
-rw-r--r-- 1 administrator administrator 0 2011-04-07 10:50 sh2.sh
drwxr-xr-x 2 administrator administrator 4096 2011-04-07 10:10 Templates
drwxr-xr-x 2 administrator administrator 4096 2011-04-07 10:10 Videos

Menu
1.Displays a long listing of files
2.Deletes files from the directory
Please enter your choice
2
Enter filename to be deleted
sh2.sh
sh2.sh has been deleted
SHELL SCRIPT TO DISPLAY THE GRADE OF MARKS

SHELL PROGRAM:

echo "Enter the percentage"


read per
if [ $per -ge 75 ]
then
echo "Grade is distinction"
elif [ $per -ge 60 ]
then
echo "Grade is first class"
elif [ $per -ge 45 ]
then
echo "Grade is second class"
else
echo " Grade is fail"
fi

OUTPUT:

Enter the percentage


75
Grade is distinction
SUM OF N NUMBERS

SHELL PROGRAM:

sum=0
for i in 1 2 3 4
do
sum=`expr $sum + $i`
done
echo "The sum of $n numbers is $sum"

OUTPUT:

The sum of n numbers is 10


SWAPPING OF TWO NUMBERS

SHELL PROGRAM:

echo "Enter two numbers"


read a b
echo "Before swapping"
echo "A=$a B=$b"
t=$a
a=$b
b=$t
echo "After swapping"
echo "A=$a B=$b"

OUTPUT:

Enter two numbers


45 63
Before swapping
A=45 B=63
After swapping
A=63 B=45
FINDING SUM AND AVERAGE OF N NUMBERS

SHELL PROGRAM:

echo "Enter the limit"


read n
sum=0
i=1
while [ $i -le $n ]
do
echo "Enter number"
read num
sum=`expr $sum + $num`
i=`expr $i + 1`
done
avg=`expr $sum / $n`
echo "The sum of $n numbers is $sum"
echo "The average of $n numbers is $avg"

OUTPUT:

Enter the limit


3
Enter number
50
Enter number
60
Enter number
120
The sum of 3 numbers is 230
The average of 3 numbers is 76
SUM OF DIGITS IN AN INTEGER

SHELL PROGRAM:

echo "Enter the number"


read num
sum=0
while [ $num -gt 0 ]
do
a=`expr $num % 10`
sum=`expr $sum + $a`
num=`expr $num / 10`
done
echo "The sum of digits is $sum"

OUTPUT:

Enter the number


1456
The sum of digits is 16
FACTORIAL OF A GIVEN NUMBER

SHELL PROGRAM:

echo "Enter a number"


read num
i=1
fact=1
while [ $i -le $num ]
do
fact=`expr $fact \* $i`
i=`expr $i + 1`
done
echo "The factorial value is $fact"

OUTPUT:

Enter a number
5
The factorial value is 120
ARMSTRONG NUMBER OR NOT

SHELL PROGRAM:

echo "Enter a number"


read num
x=$num
sum=0
while [ $num -gt 0 ]
do
a=`expr $num % 10`
b=`expr $a \* $a \* $a`
sum=`expr $sum + $b`
num=`expr $num / 10`
done
if [ $x -eq $sum ]
then
echo "$x is an Armstrong number"
else
echo "$x is not an Armstrong number"
fi

OUTPUT:

Enter a number
153
153 is an Armstrong number
FIBONACCI SERIES

SHELL PROGRAM:

echo "Enter the limit"


read n
echo "The Fibonacci series is"
a=-1
b=1
i=0
while [ $i -lt $n ]
do
c=`expr $a + $b`
a=$b
b=$c
echo "$c"
i=`expr $i + 1`
done

OUTPUT:

Enter the limit


7
The Fibonacci series is
0
1
1
2
3
5
8
ARITHMETIC OPERATIONS

SHELL PROGRAM:

echo "Enter two numbers"


read a b
echo "Enter your choice"
read ch
case $ch in
1) x=`expr $a + $b`
echo "The sum is $x";;
2) x=`expr $a - $b`
echo "The difference is $x";;
3) x=`expr $a \* $b`
echo "The product is $x";;
4) x=`expr $a / $b`
echo "The quotient is $x";;
*) echo "Invalid choice"
esac

OUTPUT:

Enter two numbers


45 45
Enter your choice
1
The sum is 90

Enter two numbers


45 45
Enter your choice
2
The difference is 0

Enter two numbers


45 45
Enter your choice
3
The product is 2025

Enter two numbers


45 45
Enter your choice
4
The quotient is 1
DISPLAY THE LIST OF ODD NUMBERS BELOW A GIVEN NUMBER

SHELL PROGRAM:

cnt=1
if [ $# -lt 1 ]
then
echo "Invalid usage $0 number"
exit
else
echo "Displaying the odd numbers below $1"
while [ $cnt -lt $1 ]
do
echo $cnt
cnt=`expr $cnt + 2`
done
fi

OUTPUT:

administrator@linuxmint ~ $ sh sh1.sh 10
Displaying the odd numbers below 10
1
3
5
7
9
ACCEPT A NUMBER FROM THE USER AND CALCULATE SUM UP TO THE NUMBER

SHELL PROGRAM:

case $# in
0) vari=10;;
*) vari=$1;;
esac
while true
do
var1=`expr $vari + 1`
var=`expr $vari \* $var1 / 2`
echo "Sum upto $vari numbers is : $var"
exit
done

OUTPUT:

administrator@linuxmint ~ $ sh sh2.sh 15
Sum upto 15 numbers is : 120
CHECK FOR THE EXISTENCE OF A FILE AND CHECK WHETHER IT IS
EXECUTABLE

SHELL PROGRAM:

if [ $# -lt 1 ]
then
echo "invalid usage"
exit
fi
if [ -x $1 ]
then
echo "$1 has execute permission"
else
echo "$1 does not have execute permission"
fi

OUTPUT:

administrator@linuxmint ~ $ ls
Desktop Downloads narayana Public sh2.sh Templates
Documents Music Pictures sh1.sh sh3.sh Videos
administrator@linuxmint ~ $ sh sh3.sh sh1.sh
sh1.sh does not have execute permission
administrator@linuxmint ~ $ sh sh3.sh narayana
narayana has execute permission
administrator@linuxmint ~ $ ls -l sh1.sh
-rw-r--r-- 1 administrator administrator 175 2011-04-07 11:17 sh1.sh
administrator@linuxmint ~ $ ls -l narayana
-rwxrwxrwx 1 administrator administrator 26 2011-04-07 11:13 narayana
administrator@linuxmint ~ $
A SHELL PROGRAM TO ILLUSTRATE THE USAGE OF TEST COMMAND IN FILE

SHELL PROGRAM:

if [ $# -lt 1 ]
then
echo "Invalid usage"
exit
fi
while [ ! -z "$1" ]
do
if [ -f $1 ]
then
echo "$1 is an ordinary file"
elif [ -d $1 ]
then
echo "$1 is a directory file"
else
echo "$1 does not exists"
fi
shift
done

OUTPUT:

administrator@linuxmint ~ $ ls
sh2.sh sh4.sh Videos
Pictures sh1.sh sh3.sh Templates
administrator@linuxmint ~ $ sh sh4.sh sh3.sh sh2.sh sh1.sh Music
sh3.sh is an ordinary file
sh2.sh is an ordinary file
sh1.sh is an ordinary file
Music is a directory file
administrator@linuxmint ~ $
INPUT A NUMBER AND A WORD AND THEN DISPLAY THE WORD THAT MANY
NUMBER OF TIMES

SHELL PROGRAM:

if [ $# -lt 1 ]
then
echo "usage $0 number word"
echo "Invalid usage"
exit
fi
cnt=1
while [ $cnt -le $1 ]
do
echo "Your word is $2"
cnt=`expr $cnt + 1`
done

OUTPUT:

administrator@linuxmint ~ $ sh sh4.sh 5 ECE


Your word is ECE
Your word is ECE
Your word is ECE
Your word is ECE
Your word is ECE
administrator@linuxmint ~ $

Вам также может понравиться