Unix Shell Programs
-
Upload
tanay-mondal -
Category
Documents
-
view
252 -
download
3
description
Transcript of Unix Shell Programs
B.SC 3RD Year UNIX Assignment (SHELL PROGRAMMING) Page:30---------------------------------------------------------------------------------------------------------------------------------------
Commands of LINUX-
1. mkdir ( This command make directory.
Example- $ mkdir IBR
2. rmdir (This command is used to remove directory.
Example- $ rmdir IBR
3. cd ( 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 .. (
Here .. represents the parents directory.
4. pwd ( This command (print working directory) displays the full path name of the current working directory.
Example- $ pwd
5. find ( 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
This command will recursively display all java files that are stored in the directory / home / IBR include all its subdirectory.
6. du ( 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 (This 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 (This 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 (
9. cp ( 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 ( 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 ( 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 ( 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 ( no. of words
-c ( no. of characters
13. cmp ( 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 ( This command displays the end of specified file. General format is,
tail n
+n ( The command will display from the n-th line to the end.
-n ( The command will display the last n lines of the specified file.
Example- $ tail + 4 employee. dat [ dat ( document file]
15. head ( This command displays the top of the specified file.
The General format is,
head -n < file >
Example- $ head 3 employee . da
16. ps ( This command is used to know which process are running at your terminal. The general format is, ps
Example- $ps
17. mail ( The command offers off-lone communications. The general format is ,
to send mail,
mail < user name >
< message > ctrl + del
18. date ( The command displays the system, date and time. The general format is,
Date + < format >
Example- (1) $ date
(2) $ date + % h (displays hour.
19. who ( This command is used to display the user who logged in the system currently. General format is, who
Example- $ who (
Tanima tty1
feb
19
10 : 17
Rituparna
tty2
feb
19
10 : 15
Dhruba
tty3
feb
19
10 : 18
Siddharthatty4
feb
19
10 : 21
Subhabratatty5
feb
19
10 : 19
20. who I am ( This command tells who you are. The general format is , who am I
Example- $ who am I (
Tanima tty1
feb
19
10 : 17
21. man ( 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 ( This command will display calendar for the specified month and year. The general format is,
cal [ < month >]
Example- (1) $ cal 1752
(2) $ cal 2 2003
23. tput ( This command with clear option can be used to clear the screen contain. The general format is, tput clear
Example- $ tput clear
24. expr ( This command is used to perform arithmetic operations on integer.
Example- $ x = 5 (
$ y = 2 (
Then (
$ expr $ x + $ y (
7
25. bc ( This command is used to perform arithmetic operations on integer as well as on float.
Example- (1) $ bc (
10+20 (
30
(press ctrl +D)
(2) $ bc (
Scale = 2 (
8 / 3 (
2.66
(3) $ bc (
ibace = 2 ([ i ( input]
101 ( 5
26. pipe ( 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 ( 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 (
Taniya
Tanu
Tani
$ sort emp. text (
[For ascending order]
Tani
Tania
Tanu
Or$ sort r emp. text ( [For descending order]
Tanu
Tania
Tani
28. grep ( This command, grep (global search for regular expression) is used to select data from a list. The general format is, grep [-option] pattern
Example- $ cat emp.text (
1005Tanima
Computer Science
28-01-2011
1006RituparnaMathematics
31-01-2011
1007Dhruba
Physics
27-01-2011
1008SiddharthaPhysics
17-01-2011
1009SvbhabrataStatistics
20-01-2011
$ grep Physics emp.text
1007Dhruba
Physics
27-01-2011
1008SiddharthaPhysics
17-01-2011
VIA editor( Visual editor)-
VIA editor is available in almost all version I UNIX :
ed, ex and vi
Modes of operation-
VI program has 3 modes of operation :
(1) command mode
(2) Insert mode
(3) ex command mode
$ vi
[ Now press enter and you will get a new window to write a program]
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
;;
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`
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
ASSIGNMENT-4Write 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-5Write a shell script to check prime number up to a given range.
PROGRAM CODE:
echo Please enter a rangeread 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
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-7Write 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
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
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 numberProgram 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 numbersProgram 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
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
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]}
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
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]}"
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 $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: "
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
2
5
Done
ASSIGNMENT-28Write 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
ASSIGNMENT-29Write 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-30Write 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-31Write a shell script to check prime number up to a given range.
PROGRAM CODE:
echo Please enter a rangeread 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-32Write 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-33Write 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-34Write 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-35Write 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 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:
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 numberProgram 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 numbersProgram 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-42Write 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
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"
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
execfile1
exec B
(2) - ne
A nu B
A B
(1) -eq
A eq B
A = B
Command
Meaning
Syntax
---------------------------------------------------------------------------------------------------------------------
SK SIR (MCA,ME, Phd(Persuing)):9831932385