Vous êtes sur la page 1sur 30

B.

SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:1
--------------------------------------------------------------------------------------------------------------------------------------Commands of LINUX1. mkdir a This command make directory.
Example- $ mkdir IBR
2. rmdir aThis command is used to remove directory.
Example- $ rmdir IBR
3. cd a This command is used to change the current working directory.
$ cd / home / bin (file name)
This directory / home /bin becomes as a current working directory.
$ cd .. 8
Here .. represents the parents directory.
4. pwd a This command (print working directory) displays the full path name of the current working directory.
Example- $ pwd
5. find a This command recursively examine the specified directory to look for file matching some of the attribute and takes some specified action on
this file.
Example- $ find / home / IBR
-name *. Java
print
This command will recursively display all java files that are stored in the directory / home / IBR include all its subdirectory.
6. du a This command (disk usage ) reports the disk space that consume by the file in a specified directory.
Example- $ du-a
This displays count for all files, not just directories.
7. df aThis command (disk free) reports the available free space on the mounted file system.
Example- $ df-m
This displays the size of mega bite.
8. cat aThis command (catenated) is used to display the contain of specify files.
This command can be used with redirection operator (>) to create new file.
Example- $cat abc
This will display the contain of the file abc.
$cat >X. text
Type the text
Press ctrl +d 8
9. cp a This command (copy) is used to copy the contain of one file into another.
Example- $ cp abc.txt
def.txt
The content of abc.txt is copied into def.txt
10. rm a This command (remove) is used to remove (delete) a file from a specified directory.
Example- $ rm f abc.txt
Remove with protected file also without prompting.
11. mv a This command (move) is used to rename the specific directory or file.
Example- $ mv a.txt b.txt
This file a.txt is renamed to b.txt
12. wc a This command (word count) is used to display the number of lines, words and characters of information stored on the specified files.
Example- $ wc l a.c
Display the no of lines in the file a.c.
-w a no. of words
-c a no. of characters
13. cmp a the cmp (compare) is used to compare two files. General format is ,
cmp < file 1 > < file 2>
Example- $ cmp file 1. text file 2. text
14. tail a This command displays the end of specified file. General format is,
tail n <file>
+n a The command will display from the n-th line to the end.
-n a The command will display the last n lines of the specified file.
Example- $ tail + 4 employee. dat
[ dat a document file]
15. head a This command displays the top of the specified file.
The General format is,
head -n < file >
Example- $ head 3 employee . da
16. ps a This command is used to know which process are running at your terminal. The general format is, ps
Example- $ps
17. mail a The command offers off-lone communications. The general format is ,
to send mail,
mail < user name >
< message > ctrl + del
18. date a The command displays the system, date and time. The general format is,

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:2
--------------------------------------------------------------------------------------------------------------------------------------Date + < format >
Example- (1) $ date
(2) $ date + % h adisplays hour.
19. who a This command is used to display the user who logged in the system currently. General format is, who
Example- $ who 8
Tanima
tty1
feb
19
10 : 17
Rituparna
tty2
feb
19
10 : 15
Dhruba
tty3
feb
19
10 : 18
Siddhartha
tty4
feb
19
10 : 21
Subhabrata
tty5
feb
19
10 : 19
20. who I am a This command tells who you are. The general format is , who am I
Example- $ who am I 8
Tanima tty1
feb
19
10 : 17
21. man a The man (manual) command displays the syntax and details uses of the command. The general format is, man < LINUX command >
Example- (1) $ man wc
(2)\\] $ man man
22. cal a This command will display calendar for the specified month and year. The general format is,
cal [ < month >] <year>
Example- (1) $ cal 1752
(2) $ cal 2 2003
23. tput a This command with clear option can be used to clear the screen contain. The general format is, tput clear
Example- $ tput clear
24. expr a This command is used to perform arithmetic operations on integer.
Example- $ x = 5 8
$y=2 8
Then 8
$ expr $ x + $ y 8
7
25. bc a This command is used to perform arithmetic operations on integer as well as on float.
Example- (1) $ bc 8
10+20 8
30
(press ctrl +D)
(2) $ bc 8
Scale = 2 8
8/38
2.66
(3) $ bc 8
ibace = 2 8 [ i a input]
101 8
5
26. pipe a It is a mechanism in which the output of one command can be redirected as input to another command. The general command is,
command 1 command 2
Example- $ ls more
The input of the ls is send to the more command as input. So the directory listening of the current directory is displayed page by page.
Example- $ who wc-l
27. sort a This command sorts the contains of a given file based on ASCII value of characters. The general format is,
Sort [option] < file name >
Example- $ cat emp.text 8
Taniya
Tanu
Tani
$ sort emp. text 8
[For ascending order]
Tani
Tania
Tanu
Or
$ sort r emp. text 8
[For descending order]
Tanu
Tania
Tani
28. grep a This command, grep (global search for regular expression) is used to select data from a list. The general format is, grep [-option] pattern
<file name>

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:3
--------------------------------------------------------------------------------------------------------------------------------------Example- $ cat emp.text 8
1005
Tanima
Computer Science
1006
RituparnaMathematics
1007
Dhruba
Physics
1008
Siddhartha
Physics
1009
Svbhabrata
Statistics

28-01-2011
31-01-2011
27-01-2011
17-01-2011
20-01-2011

$ grep Physics emp.text


1007
Dhruba
Physics
1008
Siddhartha
Physics
VIA editor( Visual editor)VIA editor is available in almost all version I UNIX :
ed, ex and vi
Modes of operationVI program has 3 modes of operation :
(1) command mode
(2) Insert mode
(3) ex command mode
$ vi <program name >
[ Now press enter and you will get a new window to write a program]

27-01-2011
17-01-2011

Command

Syntax

Meaning

(1) -eq
(2) - ne
(3) -gt
(4) -lt
(5) -ge
(6) -lb
(7) !
(8) -a
(9) -o

A eq B
A nu B
A gt B
A lt B
A ge B
A le B
!A
A a B
A o B

A=B
AB
A>B
A<B
AB
AB
NOT A
A AND B
A OR B

ASSIGNMENT-1
Write a shell script which performs the following operation: Add, Sub, Multiplication and Division and MODULUS
PROGRAM CODE:
echo ePlease enter the first number \n
read a
echo ePlease enter second number \n
read b
echo main menu
echo 1. Addition
echo 2. Subtraction
echo 3. Multiplication
echo 4. Division
echo 5. Modulus
echo 6. Exit
echo ePlease enter your choice
read choice
case $choice in
1. c=`expr $a + $b`
echo performing the addition
echo the result is $c

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:4
--------------------------------------------------------------------------------------------------------------------------------------;;
2.c =`expr $a - $b`
echo performing the subtraction
echo the result is $c
;;
3. c=`expr $a \* $b`
echo performing the multiplication
echo the result is $c
;;
4.c=`expr $a / $b`
echo performing the division
echo the result is $c
;;
5.c=`expr $a % $b`
echo performing the modulus
echo the result is $c
;;
6. echo exit
;;
esac
OUTPUT:
[user@localhost user]$ vi menu.sh
[user@localhost user]$ sh menu.sh
Please enter the first number
35
Please enter second number
21
main menu
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
Please enter your choice
2
performing the subtraction
the result is 14
ASSIGNMENT-2
Write a shell script to print the nth Fibonacci series.
PROGRAM CODE:
tput clear
echo Enter the value of n
read n
a=0
b=1
echo $a
echo $b
i=3
while [ $i le $n ]
do
c=`expr $a + $b`

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:5
--------------------------------------------------------------------------------------------------------------------------------------echo $c
a=$b
b=$c
i=`expr $i + 1`
done
OUTPUT:
[user@localhost user]$ vi fibonacci.sh
[user@localhost user]$ sh fibonacci.sh
Enter the value of n 10
0
1
1
2
3
5
8
13
21
34
ASSIGNMENT-3
Write a shell script to print the Square root of a number.
PROGRAM CODE:
echo Enter the number
read n
result = `echo scale=4; ( sqrt ( $n )) |bc l`
echo The square root of the number is $result
OUTPUT:
[user@localhost user]$ vi sqrt.sh
[user@localhost user]$ sh sqrt.sh
Enter the number 16
The square root of the number is 4

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:6
--------------------------------------------------------------------------------------------------------------------------------------ASSIGNMENT-4
Write a shell script to reverse a number and check whether the number is palindrome or not.
PROGRAM CODE:
echo Please enter a number
read n
p=n
while [ $n ne 0 ]
do
d=`expr $n % 10`
rev=`expr $rev \* 10 + $d`
n=`expr $n / 10`
done
if test $n eq $p ; then
echo The number is palindrome
else
echo The number is not palindrome
OUTPUT:
[user@localhost user]$ vi palindrome.sh
[user@localhost user]$ sh palindrome.sh
Please enter a number 121
The number is palindrome
[user@localhost user]$ vi palindrome.sh
[user@localhost user]$ sh palindrome.sh
Please enter a number 234
The number is not palindrome
ASSIGNMENT-5
Write a shell script to check prime number up to a given range.
PROGRAM CODE:
echo Please enter a range
read n
i=2
while [ $i le $n ];
do
j=2
flag=0
while [ $j lt $i a $flag eq 0 ];
do
r=` expr $i % $j `
if [ $r eq 0 ]; then
flag=1
fi
j=`expr $j + 1`
done
if [ $flag eq 0]; then
echo the prime number $j
fi
i=`expr $i + 1`
done
OUTPUT:
[user@localhost user]$ vi prime.sh
[user@localhost user]$ sh prime.sh
Please enter a range 13
the prime number 2
the prime number 3
the prime number 5
the prime number 7
the prime number 11
the prime number 13
ASSIGNMENT-6

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:7
--------------------------------------------------------------------------------------------------------------------------------------Write a shell script to check a given number is Perfect or not.
PROGRAM CODE:
echo Please enter a number
read n
sum=0
while [ $i lt $n ]; do
d=`expr $n % $i`
if [ $d eq 0 ]; then
sum=`expr $sum + $i`
fi
i=`expr $i + 1`
done
if [ $sum eq $n ]; then
echo This is a perfect number
else
echo This is not perfect number
fi
done
OUTPUT:
[user@localhost user]$ vi perfect.sh
[user@localhost user]$ sh perfect.sh
Please enter a number 6
This is a perfect number
[user@localhost user]$ vi perfect.sh
[user@localhost user]$ sh perfect.sh
Please enter a number 29
This is not perfect number
ASSIGNMENT-7
Write a shell script to check a given year is Leap- year or not.
PROGRAM CODE:
echo Please enter the year
read y
a= `expr $y % 100`
b= `expr $y % 400`
c= `expr $y % 4`
if [ $b eq 0 ] ; then
if[ $a eq 0 ] ; then
echo The year is leap-year
else
echo The year is not leap-year
fi
else
if [ $c eq 0 ] ; then
echo The year is leap-year
else
echo The year is not leap-year
done
OUTPUT:
[user@localhost user]$ vi leap.sh
[user@localhost user]$ sh leap.sh
Please enter the year 2012
The year is leap-year
[user@localhost user]$ vi leap.sh
[user@localhost user]$ sh leap.sh
Please enter the year 1999
The year is not leap-year
ASSIGNMENT-8

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:8
--------------------------------------------------------------------------------------------------------------------------------------Write a shell script to reverse a number.
PROGRAM CODE:
echo Please enter the number
read n
s=0
while [ $n ne 0 ] ;
do
r=`expr $n % 10`
s=`expr $s \* 10`
s=`expr $s + $r`
done
echo the reverse number is $s
OUTPUT:
[user@localhost user]$ vi reverse.sh
[user@localhost user]$ sh reverse.sh
Please enter the number 543
the reverse number is 345
ASSIGNMENT-9
Write a shell script to reverse a string and check whether the string is palindrome or not.
PROGRAM CODE:
j=1
rstr=
echo e\n\n\t enter the string
read string
while _test_1
do
i=`echo $string | cut c $j`
if[-z $i];then
break
else
rstr=$i$rstr
j=`expr $j + 1`
fi
done
if test $rstr=$string; then
echo the string is palindrome
else
echo the string is not palindrome
fi
OUTPUT:
[user@localhost user]$ vi strpalin.sh
[user@localhost user]$ sh strpalin.sh
enter the string
madam
the string is palindrome
[user@localhost user]$ vi strpalin.sh
[user@localhost user]$ sh strpalin.sh
enter the string
chair
the string is not palindrome
ASSIGNMENT 10:
Write a shell script to calculate the gross salary of ten workers based on some criteria, Salary should be input.
Program Code:
Echo Enter salary for 10 workers
Sum=0
a=1

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:9
--------------------------------------------------------------------------------------------------------------------------------------while [ $a le 10 ]
do
read sal
sum=`expr $sal + $sum`
a=`expr^$a = 1`
done
echo gross salary for 10 workers is $sum
OUTPUT:
[user@localhost user]$vi gross.sh
[user@localhost user]$sh gross.sh
Enter the salary of 10 workers
2000
7000
76000
3400
1200
7000
4500
3400
5699
8900
gross salary of 10 workers is 119099
ASSIGNMENT 11:
Write a shell script to convert a given string from lower case to upper case.
Program Code:
clear
echo n e \n\n\t\t Please enter the string..
read str
echo e \n\n\t\t Show the upper string is $ustr.
ustr= echo $str|tr [a-z] [A-Z]
echo e \n\n\t\t End of the program.
OUTPUT:
[user@localhost user]$vi string.sh
[user@localhost user]$sh string.sh
Please enter a string.school
So the upper case string is SCHOOL.
End the program.
ASSIGNMENT 12:
Write a shell script to append a line of text to a text file.
The line of text is to be provided as command line argument.
Program Code:
echo Give the file to append the text:
read aa
echo $* >> $aa
OUTPUT:
[user@localhost user]$ cat >pp.txt
The world is beautiful
[user@localhost user]$vi append.sh
[user@localhost user]$sh append.sh kolkata is the capital of West
Bengal
Give the filename to append the text: pp.txt
[user@localhost user] $ cat pp.txt
The world is beautiful
kolkata is the capital of West Bengal
ASSIGNMENT 13:
Write a shell script to find the digital root of a number

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:10
--------------------------------------------------------------------------------------------------------------------------------------Program Code:
echo enter a number
read a
sum=0
while [ $a gt 9 ] ;
do
while [ $a gt 0 ] ;
do
g = `expr $a % 10`
a = `expr $a / 10`
sum = ` expr $sum + $g`
done
if [ $sum le 9 ] ;
then
break
else
a = $sum
sum = 0
fi
done
a = $sum
echo digital root is : $sum
OUTPUT:
[user@localhost user]$vi digi.sh
[user@localhost user]$sh digi.sh
enter a number 435
digital root is 3
ASSIGNMENT 14:
Write a shell script to find the sum of n numbers
Program Code:
echo e enter the number of numbers :
read n
r=0
i=1
while [$n ge 1] ;
do
echo enter number $i
read no
r = ` expr $r + $no `
n = ` expr $n 1 `
i = ` expr $i + 1 `
done
echo result is $r
OUTPUT:
[user@localhost user]$vi sum.sh
[user@localhost user]$sh sum.sh
enter the number of numbers 4
enter number 2
enter number 5
enter number 7
enter number 4
result is 18
ASSIGNMENT-15
Write a shell script for checking a files read, write and execute permission.
Program Code:
echo enter the filename
read f

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:11
--------------------------------------------------------------------------------------------------------------------------------------if [ -e $f ] ; then
if [ -f $f ] ; then
echo $f is an ordinary file
p= `ls l $f | cut c2-2`
if[ &p = r ] ; then
echo $f has read permission
fi
p= `ls l $f | cut c3-3`
if[ &p = w ] ; then
echo $f has write permission
fi
p= `ls l $f | cut c4-4`
if[ &p = x ] ; then
echo $f has execute permission
fi
elif [ -d $f ] ; then
echo $f is a directory
elif [-c $f ] ; then
echo $f is a character special file
elif [ -b $f ] ; then
echo $f is a block special file
fi
else
echo file does not exist
fi
OUTPUT:
[user@localhost user]$vi file.sh
[user@localhost user]$sh file.sh
enter the filename
file.txt
file is an ordinary file
file has read permission
file has write permission
Program 16
Write a Shell script to apply binary search method on virtual array.
Solution:
#binary search over an array.....
clear
echo -en "\n\nEnter the total no. of elements: "
read n
m=0
while [ $m -lt $n ] ; do
# let k=m+1
echo -en "\n\nEnter the element $m: "
read a[$m]
m=`expr $m + 1`
done
echo -en "\n\nEnter the element to be searched: "
read sn
beg=0
end=`expr $n - 1`
while [ $beg -le $end ] ; do
mid=`expr $beg + $end`
mid=`expr $mid / 2`
if [ $sn -eq ${a[$mid]} ] ; then
break
elif [ $sn -lt ${a[$mid]} ] ; then

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:12
--------------------------------------------------------------------------------------------------------------------------------------end=`expr $mid - 1`
else
beg=`expr $mid + 1`
fi
done
if [ $beg -le $end ] ; then
mid=`expr $mid + 1`
echo -e "\n\nElement found at: $mid"
else
echo -e "\n\nElement is not found !!!!!"
fi
echo -e "\n\nEnd of program ......."
Output:
Enter the total no. of elements: 4
Enter the element 1:7
Enter the element 2:14
Enter the element 3:2
Enter the element 4:10
Enter the element to be searched:2
Element found at:3
End of program .......
Program 17.
Write a Shell script to apply selection sorting method on virtual array.
Solution:
#Selection_sort over an array......
clear
echo -en "\n\nEnter the total no. of elements: "
read n
m=0
while [ $m -lt $n ] ; do
k=`expr $m + 1`
echo -en "\n\nEnter element no. $k: "
read a[$m]
m=`expr $m + 1`
done
echo -e "\n\nYour entered sequence is:-->"
m=0
while [ $m -lt $n ] ; do
k=`expr $m + 1`
echo -en "\n\nThe element $k is: ${a[$m]} "
m=`expr $m + 1`
done
i=0
while [ $i -lt $n ] ; do
min=${a[$i]}
pos=$i
j=`expr $i + 1`
while [ $j -lt $n ] ; do
if [ ${a[$j]} -lt $min ] ; then
min=${a[$j]}
pos=$j
fi
j=`expr $j + 1`
done
t=${a[$i]}

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:13
--------------------------------------------------------------------------------------------------------------------------------------a[$i]=$min
a[$pos]=$t
i=`expr $i + 1`
done
echo -e "\n\nSorted sequence:-->"
m=0
while [ $m -lt $n ] ; do
k=`expr $m + 1`
echo -en "\n\nThe element $k is: ${a[$m]} "
m=`expr $m + 1`
done
echo -e "\n\nEnd of program...."
Output:
Enter the total no. of elements:4
Enter element no. 1:2
Enter element no. 2:7
Enter element no. 3:1
Enter element no. 4:5
Your entered sequence is:-->
The element 1 is:2
The element 2 is:7
The element 3 is:1
The element 4 is:5
Sorted sequence:-->
The element 1 is:1
The element 2 is:2
The element 3 is:5
The element 4 is:7
End of program....
Program 18.
Write a Shell script to apply insertion sorting method on virtual array.
Solution:
#insertion_sort over an array......
clear
echo -en "\n\nEnter the total no. of elements: "
read n
m=0
while [ $m -lt $n ] ; do
k=`expr $m + 1`
echo -en "\n\nEnter element no. $k: "
read a[$m]
m=`expr $m + 1`
done
echo -e "\n\nYour entered sequence is:-->"
m=0
while [ $m -lt $n ] ; do
k=`expr $m + 1`
echo -en "\n\nThe element $k is: ${a[$m]} "
m=`expr $m + 1`
done
i=1
while [ $i -lt $n ] ; do
temp=${a[$i]}
j=$i
while [ $j -gt 0 ] ; do
h=`expr $j - 1`
if [ $temp -lt ${a[$h]} ] ; then
a[$j]=${a[$h]}
else

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:14
--------------------------------------------------------------------------------------------------------------------------------------break
fi
j=`expr $j - 1`
done
i=`expr $i + 1`
a[$j]=$temp
done
echo -e "\n\nSorted sequence:-->"
m=0
while [ $m -lt $n ] ; do
k=`expr $m + 1`
echo -en "\n\nThe element $k is: ${a[$m]} "
m=`expr $m + 1`
done
echo -e "\n\nEnd of program...."
Output:
Enter the total no. of elements:4
Enter element no. 1:21
Enter element no. 2:70
Enter element no. 3:17
Enter element no. 4:52
Your entered sequence is:-->
The element 1 is:21
The element 2 is:70
The element 3 is:17
The element 4 is:52
Sorted sequence:-->
The element 1 is:17
The element 2 is:21
The element 3 is:52
The element 4 is:70
End of program....
Program 19.
Write a Shell script that will print Fibonacci series up to a user-given range in reverse order.
Solution:
#Fibonacci series printing in reverse order
i=0
a[$i]=0
i=`expr $i + 1`
a[$i]=1
echo -en "\n\nEnter an integer: "
read n
j=2
s=0
t=1
sum=0
while [ $j -lt $n ] ; do
sum=`expr $s + $t`
i=`expr $i + 1`
a[$i]=$sum
s=$t
t=$sum
j=`expr $j + 1`
done
echo -e "\n\nThe Fibonacci series in reverse order is: "
while [ $i -ge 0 ] ; do
echo -e "\n ${a[$i]}"

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:15
--------------------------------------------------------------------------------------------------------------------------------------i=`expr $i - 1`
done
Output:
Enter an integer:5
The Fibonacci series in reverse order is:
3
2
1
1
0
Program 20.
Write a Shell script that will simulate the WC command.
Solution:
terminal=`tty`
l=0
w=0
c1=0
fnc=$1
if [ $# -eq 2 ] ; then
exec<$2
while read line
do
#echo $line
set $line
d=`expr "$line" : '.*'`
c1=`expr $c1 + $d`
w=`expr $w + $#`
i=`expr $i + 1`
done
case $fnc in
"-l")
echo -e "\n\nTotal no. of lines: $i"
;;
"-w")
echo -e "\n\nTotal no. of words: $w"
;;
"-c")
echo -e "\n\nTotal no. of charecters: $c1"
;;
*)
echo -e "\n\nIn-valid parameter passing...."
;;
esac
exec<$terminal
else
echo -e "\n\nParameter mismatch...."
fi
echo -e "\n\nEnd of program"
Output:
$ wc1.sh l f1.txt
2
$ wc1.sh w f1.txt
20

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:16
--------------------------------------------------------------------------------------------------------------------------------------$ wc1.sh c f1.txt
78
Program 21.
Write down a shell script that will show the all possible combinations from an user-given 4 letter string..
Solution:
clear
echo -en "\n\nEnter the string (4 letter) of your choice: "
read str
j=1
while [ 1 ] ; do
i=`echo $str|cut -c $j`
if [ -z $i ] ; then
break
else
a[$j]=$i
j=`expr $j + 1`
fi
done
i=1
while [ $i -le 4 ] ; do
j=1
while [ $j -le 4 ] ; do
if [ $i -ne $j ] ; then
k=1
while [ $k -le 4 ] ; do
if [ $k -ne $j -a $k -ne $i ] ; then
l=1
while [ $l -le 4 ] ; do
if [ $l -ne $k -a $l -ne $j -a $l -ne $i ] ; then
echo -n ${a[$i]}
echo -n ${a[$j]}
echo -n ${a[$k]}
echo -n ${a[$l]}
echo -e "\n"
fi
l=`expr $l + 1`
done
fi
k=`expr $k + 1`
done
fi
j=`expr $j + 1`
done
i=`expr $i + 1`
done
echo -e "\n\nEnd of program...."
Output:
Enter the string (4 letter) of your choice: 1234
1234
1243
1324
1342
1423
1432
2143
2134

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:17
--------------------------------------------------------------------------------------------------------------------------------------2314
2341
2413
2431
3421
3412
3124
3142
3214
3241
4312
4321
4213
4231
4132
4123
End of program....
Program 22.
Write down a shell script that will test an integer is divisible by 11 or not .(testing will be done by not dividing).
Solution:
j=1
echo -en "\n\nEnter a number: "
read no
oddsum=0
evnsum=0
while [ 1 ] ; do
i=`echo $no|cut -c $j`
if [ -z $i ] ; then
break
else
d=`expr $j % 2`
if [ $d -eq 1 ] ; then
oddsum=`expr $oddsum + $i`
else
evnsum=`expr $evnsum + $i`
fi
j=`expr $j + 1`
fi
done
if [ $evnsum -eq $oddsum ] ; then
echo -e "\n\nYour entered number is divisible by 11.."
else
echo -e "\n\nYour entered number is not divisible by 11.."
fi
Output:
Enter a number: 121
Your entered number is divisible by 11..
Program 23.
Write a shell script that have the capability in changing the extensions of group of files.(e.g. The user must be able to convert
all .txt extensions to .doc with the help of the script).the source and the target extensions should be passed as arguments.
Solution:
#clear

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:18
--------------------------------------------------------------------------------------------------------------------------------------if [ $# -ge 2 ] ; then
j=0
f1="*."$1
f2=$2
for i in $f1 ; do
r=`echo $i|cut -d "." -f 1`
r=$r"."$f2
mv $i $r
j=`expr $j + 1`
done
echo -e "\n\n\t\t$j files are converted successfully...."
else
echo -e "\n\nParameter insufficient...."
fi
echo -e "\n\nEnd of program..."
Output:
$ Convert.sh txt sh
21 files are converted successfully....
End of program...
Program 24.
Write a shell script that will append a line that will given by the user as command line arguments to a user given file.
Solution:
echo -e "Enter the file name: "
read f
i=1
for i in $@
do
rec=`echo $rec $i`
done
echo $rec >> $f
echo -e "\n\nContents of file $f after appending a text line argument...\n\n"
cat $f
Output:
$ appnd_line.sh I am a boy
Enter the file name: f1.txt
Contents of file $f after appending a text line argument...
I am a student of B.Sc 3rd year.
I have honors in computer science.
I am a boy.
$
Program 25.
Write a shell script that will print the Lucas sequence ( 1 , 3 , 4 , 7 , 11 , 18 , 29) up to n where n will be given by the user.
Solution:
a=1
b=3
echo -en "\n\nEnter the upper limit: "

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:19
--------------------------------------------------------------------------------------------------------------------------------------read n
n=`expr $n - 2`
echo -e "\n\nThe corresponding Lucas sequence is: \n"
i=1
sum=0
echo $a
echo $b
while [ $i -le $n ] ; do
sum=`expr $a + $b`
echo $sum
a=$b
b=$sum
i=`expr $i + 1`
done
Output:
Enter the upper limit: 7
The corresponding Lucas sequence is:
1
3
4
7
11
18
29
Program 26.
Write a shell script to find out if any passwords are missing in the /etc/passwd file.
Solution:
terminal=`tty`
flag=0
exec</etc/passwd
while read line
do
i=`echo $line|cut -d ":" -f2`
if [ -z $i ] ; then
j=`echo $line|cut -d ":" -f1`
echo -en "\n\nPassword missing for the user: $j."
flag=1
fi
done
if [ $flag -ne 1 ] ; then
echo -e "\n\nNo such user name is found...."
fi
exec<$terminal
Output:
$ pswd.sh
No such user name is found....
$
Program 27.
Write a shell script to find out the prime factors of any user given integers.
Solution:
clear
echo -e -n "Enter any integer: "
read n
i=2
j=1
while [ $i -le $n ] ; do
k=`expr $n % $i`
if [ $k -eq 0 ] ; then
a[$j]=$i

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:20
--------------------------------------------------------------------------------------------------------------------------------------j=`expr $j + 1`
fi
i=`expr $i + 1`
done
i=1
echo -en "\n\nPrime factors of $n are:->\n"
while [ $i -lt $j ] ; do
l=`expr "sqrt(${a[$i]})"|bc`
d=2
flag=0
while [ $d -le $l -a $flag -eq 0 ]
do
r=`expr ${a[$i]} % $d`
if [ $r -eq 0 ]
then
flag=1
fi
d=`expr $d + 1`
done
if [ $flag -eq 0 ] ; then
echo -e "\n\n${a[$i]}"
fi
i=`expr $i + 1`
done
echo -e "\n\nDone"
Output:
Enter any integer: 20
Prime factors of are:->
2
5
Done
ASSIGNMENT-28
Write a shell script to print the nth Fibonacci series.
PROGRAM CODE:
tput clear
echo Enter the value of n
read n
a=0
b=1
echo $a
echo $b
i=3
while [ $i le $n ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1`
done

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:21
--------------------------------------------------------------------------------------------------------------------------------------ASSIGNMENT-29
Write a shell script to print the Square root of a number.
PROGRAM CODE:
echo Enter the number
read n
result = `echo scale=4; ( sqrt ( $n )) |bc l`
echo The square root of the number is $result
OUTPUT:
[user@localhost user]$ vi sqrt.sh
[user@localhost user]$ sh sqrt.sh
Enter the number 16
The square root of the number is 4
ASSIGNMENT-30
Write a shell script to reverse a number and check whether the number is palindrome or not.
PROGRAM CODE:
echo Please enter a number
read n
p=n
while [ $n ne 0 ]
do
d=`expr $n % 10`
rev=`expr $rev \* 10 + $d`
n=`expr $n / 10`
done
if test $n eq $p ; then
echo The number is palindrome
else
echo The number is not palindrome
OUTPUT:
[user@localhost user]$ vi palindrome.sh
[user@localhost user]$ sh palindrome.sh
Please enter a number 121
The number is palindrome
[user@localhost user]$ vi palindrome.sh
[user@localhost user]$ sh palindrome.sh
Please enter a number 234
The number is not palindrome
ASSIGNMENT-31
Write a shell script to check prime number up to a given range.
PROGRAM CODE:
echo Please enter a range
read n
i=2
while [ $i le $n ];
do
j=2
flag=0
while [ $j lt $i a $flag eq 0 ];
do
r=` expr $i % $j `
if [ $r eq 0 ]; then
flag=1
fi
j=`expr $j + 1`
done

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:22
--------------------------------------------------------------------------------------------------------------------------------------if [ $flag eq 0]; then
echo the prime number $j
fi
i=`expr $i + 1`
done
OUTPUT:
[user@localhost user]$ vi prime.sh
[user@localhost user]$ sh prime.sh
Please enter a range 13
the prime number 2
the prime number 3
the prime number 5
the prime number 7
the prime number 11
the prime number 13
ASSIGNMENT-32
Write a shell script to check a given number is Perfect or not.
PROGRAM CODE:
echo Please enter a number
read n
sum=0
while [ $i lt $n ]; do
d=`expr $n % $i`
if [ $d eq 0 ]; then
sum=`expr $sum + $i`
fi
i=`expr $i + 1`
done
if [ $sum eq $n ]; then
echo This is a perfect number
else
echo This is not perfect number
fi
done
OUTPUT:
[user@localhost user]$ vi perfect.sh
[user@localhost user]$ sh perfect.sh
Please enter a number 6
This is a perfect number
[user@localhost user]$ vi perfect.sh
[user@localhost user]$ sh perfect.sh
Please enter a number 29
This is not perfect number
ASSIGNMENT-33
Write a shell script to check a given year is Leap- year or not.
PROGRAM CODE:
echo Please enter the year
read y
a= `expr $y % 100`
b= `expr $y % 400`
c= `expr $y % 4`
if [ $b eq 0 ] ; then
if[ $a eq 0 ] ; then
echo The year is leap-year
else
echo The year is not leap-year
fi
else

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:23
--------------------------------------------------------------------------------------------------------------------------------------if [ $c eq 0 ] ; then
echo The year is leap-year
else
echo The year is not leap-year
done
OUTPUT:
[user@localhost user]$ vi leap.sh
[user@localhost user]$ sh leap.sh
Please enter the year 2012
The year is leap-year
[user@localhost user]$ vi leap.sh
[user@localhost user]$ sh leap.sh
Please enter the year 1999
The year is not leap-year
ASSIGNMENT-34
Write a shell script to reverse a number.
PROGRAM CODE:
echo Please enter the number
read n
s=0
while [ $n ne 0 ] ;
do
r=`expr $n % 10`
s=`expr $s \* 10`
s=`expr $s + $r`
done
echo the reverse number is $s
OUTPUT:
[user@localhost user]$ vi reverse.sh
[user@localhost user]$ sh reverse.sh
Please enter the number 543
the reverse number is 345
ASSIGNMENT-35
Write a shell script to reverse a string and check whether the string is palindrome or not.
PROGRAM CODE:
j=1
rstr=
echo e\n\n\t enter the string
read string
while _test_1
do
i=`echo $string | cut c $j`
if[-z $i];then
break
else
rstr=$i$rstr
j=`expr $j + 1`
fi
done
if test $rstr=$string; then
echo the string is palindrome
else
echo the string is not palindrome
fi
OUTPUT:
[user@localhost user]$ vi strpalin.sh

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:24
--------------------------------------------------------------------------------------------------------------------------------------[user@localhost user]$ sh strpalin.sh
enter the string
madam
the string is palindrome
[user@localhost user]$ vi strpalin.sh
[user@localhost user]$ sh strpalin.sh
enter the string
chair
the string is not palindrome
ASSIGNMENT 36:
Write a shell script to calculate the gross salary of ten workers based on some criteria, Salary should be input.
Program Code:
Echo Enter salary for 10 workers
Sum=0
a=1
while [ $a le 10 ]
do
read sal
sum=`expr $sal + $sum`
a=`expr^$a = 1`
done
echo gross salary for 10 workers is $sum
OUTPUT:
[user@localhost user]$vi gross.sh
[user@localhost user]$sh gross.sh
Enter the salary of 10 workers
2000
7000
76000
3400
1200
7000
4500
3400
5699
8900
gross salary of 10 workers is 119099
ASSIGNMENT 38:
Write a shell script to convert a given string from lower case to upper case.
Program Code:
clear
echo n e \n\n\t\t Please enter the string..
read str
echo e \n\n\t\t Show the upper string is $ustr.
ustr= echo $str|tr [a-z] [A-Z]
echo e \n\n\t\t End of the program.
OUTPUT:
[user@localhost user]$vi string.sh
[user@localhost user]$sh string.sh
Please enter a string.school
So the upper case string is SCHOOL.
End the program.
ASSIGNMENT 40:
Write a shell script to append a line of text to a text file.
The line of text is to be provided as command line argument.
Program Code:
echo Give the file to append the text:

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:25
--------------------------------------------------------------------------------------------------------------------------------------read aa
echo $* >> $aa
OUTPUT:
[user@localhost user]$ cat >pp.txt
The world is beautiful
[user@localhost user]$vi append.sh
[user@localhost user]$sh append.sh kolkata is the capital of West
Bengal
Give the filename to append the text: pp.txt
[user@localhost user] $ cat pp.txt
The world is beautiful
kolkata is the capital of West Bengal
ASSIGNMENT 41:
Write a shell script to find the digital root of a number
Program Code:
echo enter a number
read a
sum=0
while [ $a gt 9 ] ;
do
while [ $a gt 0 ] ;
do
g = `expr $a % 10`
a = `expr $a / 10`
sum = ` expr $sum + $g`
done
if [ $sum le 9 ] ;
then
break
else
a = $sum
sum = 0
fi
done
a = $sum
echo digital root is : $sum
OUTPUT:
[user@localhost user]$vi digi.sh
[user@localhost user]$sh digi.sh
enter a number 435
digital root is 3
ASSIGNMENT 42:
Write a shell script to find the sum of n numbers
Program Code:
echo e enter the number of numbers :
read n
r=0
i=1
while [$n ge 1] ;
do
echo enter number $i
read no
r = ` expr $r + $no `
n = ` expr $n 1 `
i = ` expr $i + 1 `
done

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:26
--------------------------------------------------------------------------------------------------------------------------------------echo result is $r
OUTPUT:
[user@localhost user]$vi sum.sh
[user@localhost user]$sh sum.sh
enter the number of numbers 4
enter number 2
enter number 5
enter number 7
enter number 4
result is 18
ASSIGNMENT-42
Write a shell script for checking a files read, write and execute permission.
Program Code:
echo enter the filename
read f
if [ -e $f ] ; then
if [ -f $f ] ; then
echo $f is an ordinary file
p= `ls l $f | cut c2-2`
if[ &p = r ] ; then
echo $f has read permission
fi
p= `ls l $f | cut c3-3`
if[ &p = w ] ; then
echo $f has write permission
fi
p= `ls l $f | cut c4-4`
if[ &p = x ] ; then
echo $f has execute permission
fi
elif [ -d $f ] ; then
echo $f is a directory
elif [-c $f ] ; then
echo $f is a character special file
elif [ -b $f ] ; then
echo $f is a block special file
fi
else
echo file does not exist
fi
OUTPUT:
[user@localhost user]$vi file.sh
[user@localhost user]$sh file.sh
enter the filename
file.txt
file is an ordinary file
file has read permission
file has write permission
ASSIGNMENT-43
#any base to any base convertion
echo -en "\n\nEnter the Input Base: "
read i
echo -en "\n\nEnter the Output Base: "
read o
echo "obase=$o">temp

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:27
--------------------------------------------------------------------------------------------------------------------------------------echo "ibase=$i">>temp
echo -en "\n\nNow enter the number: "
read n
n=`echo $n|tr '[a-f]' '[A-F]'`
echo "$n">>temp
echo -e "\n\nThe converted number is: \c"
cat temp2
rm temp
rm temp2
echo -e "\n\n"
ASSIGNMENT-44
#twos complemented addition of two user given binary numbers.
clear
echo -en "\n\nEnter the first binary number: "
read m
echo -en "\n\nEnter the first binary number: "
read n
j=1
k=""
while [ 1 ] ; do
i=`echo $n|cut -c $j`
if [ -z $i ] ; then
break
fi
if [ $i -eq 1 ] ; then
i=0
else
i=1
fi
k=$k$i
j=`expr $j + 1`
done
j=`expr $j - 1`
v=1
while [ 1 ] ; do
i=`echo $m|cut -c $v`
if [ -z $i ] ; then
break
fi
v=`expr $v + 1`
done
o=0
v=`expr $v - 1`
if [ $j -eq $v ] ; then
h=$j
fi
if [ $j -gt $v ] ; then
h=$j
fi
if [ $j -lt $v ] ; then
h=$v
fi
#echo -e "\n\nthe first number: $m"
#echo -e "\n\nthe 1's complemented second number: $k"
#echo -en "\nh=$h"
#echo -en "\nv=$v"
#echo -en "\nj=$j"

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:28
--------------------------------------------------------------------------------------------------------------------------------------sum=""
cy=0
if [ $h -gt $v ] ; then
c=`expr $h - $v`
a=1
while [ $a -le $c ] ; do
m=$o$m
a=`expr $a + 1`
done
fi
if [ $h -gt $j ] ; then
#echo -e "\ni'm in 2nd if"
c=`expr $h - $j`
#echo -e "\n\nc=$c"
a=1
while [ $a -le $c ] ; do
# echo -e "\n\nk=$k"
k=$o$k
a=`expr $a + 1`
done
fi
j=$h
echo -e "\n\nThe first number: $m"
echo -e "\n\nThe 1's complemented second number: $k"
#echo -e "\n\nj=$j"
while [ $j -gt 0 ] ; do
i=`echo $m|cut -c $j`
n=`echo $k|cut -c $j`
#echo -e "\nI=$i\nN=$n"
if [ -z $i ] ; then
i=0
fi
if [ -z $n ] ; then
n=0
fi
t=`expr $i + $n + $cy`
if [ $t -eq 2 ] ; then
t=0
cy=1
fi
if [ $t -eq 3 ] ; then
t=1
cy=1
fi
if [ $t -eq 1 ] ; then
t=1
cy=0
fi
sum=$t$sum
j=`expr $j - 1`
done
sum=$cy$sum
echo -en "\n\nThe sum is: $sum\n\n"
ASSIGNMENT-45
#simulation of cal command that will display calender when more than 1 monthes will be entered..
eval
echo \$$#>file1

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:29
--------------------------------------------------------------------------------------------------------------------------------------exec<file1
read x
while [ $# -gt 1 ] ; do
cal $1 $x
shift
done
rm file1
echo -e "\nEnd of program"
Assignment 45
#simulation of cal command that will display calender when more than 1 monthes will be entered..
eval
echo \$$#>file1
exec<file1
read x
while [ $# -gt 1 ] ; do
cal $1 $x
shift
done
rm file1
echo -e "\nEnd of program"
Quadratic Equation
source :
clear
echo Enter the values of a,b,c in a.x^2 + b.x + c = 0
echo -n "a = "
read a
echo -n "b = "
read b
echo -n "c = "
read c
echo The given quadratic equation becomes $a.x^2 + $b.x + $c = 0
p=`echo "scale=4;(($b * $b) - (4 * $a * $c))"|bc`
if [ $p -lt 0 ]
then
echo The roots are imaginary...
p=`echo "scale=4;($p * (-1))"|bc`
q=`echo "scale=4;((-1) * $b / (2 * $a))"|bc`
r=`echo "scale=4;(sqrt($p) / (2 * $a))"|bc`
echo "The roots are ($q) + i.($r) and ($q) - i.($r)..."
else
if [ $p -gt 0 ]
then
echo The roots are distinct...
r1=`echo "scale=4;(((-1) * $b + sqrt($p)) / (2 * $a))"|bc`
r2=`echo "scale=4;(((-1) * $b - sqrt($p)) / (2 * $a))"|bc`
echo The roots are $r1 and $r2...
else
echo The roots are equal...
r=`echo "scale=4;((-1) * $b / (2 * $a))"|bc`

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385

B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING)


Page:30
--------------------------------------------------------------------------------------------------------------------------------------echo The root is $r...
fi
fi
output 1:
---------Enter the values of a,b,c in a.x^2 + b.x + c = 0
a=2
b = -9
c=2
The given quadretic equation becomes 2.x^2 + -9.x + 2 = 0
The roots are distinct...
The roots are 4.2655 and .2344...
output 2:
----------Enter the values of a,b,c in a.x^2 + b.x + c = 0
a=5
b=1
c=5
The given quadretic equation becomes 5.x^2 + 1.x + 5 = 0
The roots are imaginary...
The roots are (-.1000) + i.(.9949) and (-.1000) i.(.9949)...
output 3:
----------Enter the values of a,b,c in a.x^2 + b.x + c = 0
a=3
b=6
c=3
The given quadretic equation becomes 3.x^2 + 6.x + 3 = 0
The roots are equal...
The root is -1.0000...

--------------------------------------------------------------------------------------------------------------------SK SIR (MCA,ME, Phd(Persuing)):9831932385