Академический Документы
Профессиональный Документы
Культура Документы
SHELL PROGRAM:
OUTPUT:
SHELL PROGRAM:
OUTPUT:
SHELL PROGRAM:
OUTPUT:
SHELL PROGRAM:
OUTPUT:
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:
OUTPUT:
7000
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:
OUTPUT:
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:
SHELL PROGRAM:
OUTPUT:
SHELL PROGRAM:
OUTPUT:
SHELL PROGRAM:
OUTPUT:
SHELL PROGRAM:
OUTPUT:
Enter a number
5
The factorial value is 120
ARMSTRONG NUMBER OR NOT
SHELL PROGRAM:
OUTPUT:
Enter a number
153
153 is an Armstrong number
FIBONACCI SERIES
SHELL PROGRAM:
OUTPUT:
SHELL PROGRAM:
OUTPUT:
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: