echo The distance in feets is $feet feets
$sh prg64
Input distance in kilometers
2
The distance in meters is 2000 meters
The distance in centimeters is 200000 cm
The distance in inches is 787401 inches
The distance in feets is 65616 feets
65. To display date and time in different formats by using positional parameters.
$vi prg65
clear
#Date in desired format
set ‘date’ #Setting positional parameters through date command
echo $3 $2 $6
echo $4
echo $2 $3 $6
echo $2 $6 $3
$sh prg65
30 Apr 2008
16:51:58
Apr 30 2008
Apr 2008 30
66. Moving shell files from PWD to specified directory.
$vi prg66
if [ $# -lt 1 ]
then
echo Improper Usage : $0 Pathname
fi
mv *.sh $1
echo All files are moved in the $1 directory
ls $1
$sh prg66 abc
All files are moved in the abc directory
a.sh
b.sh
$sh prg66
Improper Usage : p1 Pathname
67. To print all the files and total number of files in given directory.
$vi prg67
clear
if [ $# -lt 1 ]
then
echo Improper Usage : $0 pathname
fi
oldifs=$ifs
ifs=/
for arg in $*
do
if [ -d $arg ]
then
cd $arg
echo Present directory
echo $arg
echo Files in the directory :
ls
echo total number of files in this directory :
echo ‘ls | wc -w’
else
if [ -f $arg ]
then
echo $arg is a file
exit
fi
fi
done
ifs=$oldifs
$sh prg67
Improper Usage : p1 pathname
$sh prg67 /root
96
Present directory
/root
Files in the directory :
a aaa.c abc2 b c ddd ddd1
total files in this directory :
9
$sh prg67 abc
abc is a file
exit
Desktop p1
68. To sort strings.
$vi prg68
clear
echo Type string 1.
cat >> srt1
echo Type string 2.
cat>> str2
echo Type string 3.
cat>> str3
echo sorted strings are
sort str1 str2 str 3
$sh prg68
Type string 1.
abc
Type string 2.
xyz
Type string 3.
mnop
sorted strings are
abc
mnop
xyz
69. To find binary equivalent of a decimal number.
$vi prg69
clear
echo Enter a number
read a
pow=1
sol=0
while [ $a -gt 0 ]
do
x=‘expr $a % 2’
inter=‘expr $x \* $pow’
sol=‘expr $sol + $inter’
pow=‘expr $pow \* 10’
a=‘expr $a / 2’
done
echo $sol
$sh prg69
enter a number
12
1100
$sh prg69
Enter a number
102
1100110
$sh prg69
Enter a number
2984
101110101000
99
70. To calculate simple interest.
$vi prg70
#Calculate a simple interest
clear
echo Enter values of Principle, Time (in yrs), and rate
read p n r
si=‘expr $p \* $n \* $r / 100’
echo Simple Interest=Rs.
$si
$sh prg70
Enter values of Principle, Time (in yrs), and rate
2500 3 25
Simple Interest=Rs. 1875
71. If the sides of a triangle are denoted by a, b and c then area of the triangle is given by
area = Square root of (s(s-a)(s-b)(s-c))
where, s = (a+b+c)/2
$vi prg71
clear
echo Enter sides of a triangle
read a b c
s=‘expr \( $a + $b + $c \) / 2’
area=‘expr \( $s \* \( $s - $a \) \* \( $s - $b \) \* \( $s - $c \) \)’
area=‘echo sqrt \( $area \) | bc’
echo Area of the triangle is $area
$sh prg71
Enter sides of a triangle
60 70 50
Area of the triangle is 1469
101
72. Program to display system date in format MM/DD/YY & system time in format hrs:mins:secs.
$vi prg72
clear
echo The current system date in required format is :
date +%D
echo The current system time in required format is :
date +%T
$sh prg72
The current system date in required format is :
04/05/08 // Means 5
th
April 2008
The current system time in required format is :
10:26:47 // Means 10 hrs 26 mins 47 secs
73. Program to say hello to the user.
$vi prg73
clear
echo Enter your Name
read name
echo Hello $name
$sh prg73
Enter your Name Charles Babbage Hello Charles Babbage Enter your Name Dennis Ritchie Hello Dennis Ritchie
74. Program to display a message using switch case.
$vi prg74
clear
echo Enter a number between 1 and 3
read num
case $num in
1) echo You have Entered 1 ;;
2) echo You have Entered 2 ;;
3) echo You have Entered 3 ;;
*) echo Please enter some value between 1 & 3 ;;
esac
$sh prg74
Enter a number between 1 and 3
3
You have Entered 3
$sh prg74
Enter a number between 1 and 3
2
You have Entered 2
75. Write a menu driven program which has following option-(
) Factorial of a number
(b)
Prime or not
(c)
Odd or even
(d)
Exit
$vi prg75
clear
ch=y