Sei sulla pagina 1di 34

Practical-3

Aim : Write a shell script that text a name string from user and print it with proper title. clear echo Enter your name : read nam echo Welcome $nam !!

Output: Enter your name: Pranav Welcome Pranav !!

Practical-4
Aim : Write a shell script that performs simple arithmetic operation on given input. clear echo Enter first no : read a echo Enter second no : read b echo 1. Addition echo 2. Subtraction echo 3. Multiplication echo 4. Division echo 5. Modulo operation echo 6. Exit echo Enter your choice : read ch case $ch in 1) echo Answer = ` expr $a + $b`;; 2) echo Answer = ` expr $a - $b`;; 3) echo Answer = ` expr $a \* $b`;; 4) echo Answer = ` expr $a / $b`;; 5) echo Answer = ` expr $a % $b`;; 6) exit esac

Output: Enter first no: 45 Enter second no: 9 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulo operation 6. Exit Enter your choice : 4 Answer=5

Practical-5
Aim: Write a shell script to wish Good morning, Good afternoon and Good night as per current system time. clear date>dt tm= `cut c 12-13 dt` echo $tm if [ $tm -lt 12 ] then echo Good Morning !! elif [ $tm -gt 12 a $tm -lt 16 ] then echo Good Afternoon !! elif [ $tm gt 16 a $tm -lt 20 ] then echo Good Evening !! else echo Good Night !! fi

Output: Good Afternoon

Practical-6
Aim: Write a shell script to generate fibonacci number from 1 to n. clear echo Enter the number upto which you want series : read n n1=0 n2=1 echo $n1 echo $n2 for ((i=0;i<n;i++)) do n3=` expr $n1 + $n2` echo $n3 n1=$n2 n2=$n3 done

Output: Enter the number upto which you want series : 5 0 1 1 2 3 5

Practical-7
Aim: Write a shell script to generate prime numbers from 1 to n, where n is any positive integer number entered by user.

clear echo "Enter the number upto which you want prime numbers: " read n for ((i=1;i<=n;i++)) do flag=0 for ((j=2;j<i;j++)) do if [ `expr $i % $j` -eq 0 ] then flag=1 fi done if [ $flag -eq 0 ] then echo $i fi done

Output: Enter the number upto which you want prime numbers: 10 1 2 3 5 7

Practical-8
Aim: Write a shell script to print and add all even numbers between given range. clear echo Enter Starting no: read n1 echo Enter Ending no: read n2 sum=0 for ((i=n1;i<=n2;i++)) do if [ ` expr $i % 2` -eq 0 ] then echo $i sum= ` expr $sum + $i` fi done echo Sum : $sum

Output: Enter Starting no: 2

Enter Ending no: 8 2 4 6 8 Sum : 20

Practical-9
Aim: Write a shell script that reverses the number. clear echo Enter any number: read n sum=0 while [ $n gt 0 ] do a=` expr $n % 10` sum=` expr $sum \* 10 + $a` n=` expr $n / 10` done echo Reverse no : $sum

Output: Enter any number: 435 Reverse no : 534

Practical-10
Aim: Write a shell script to find factorial of a given number.

clear echo Enter the number: read n fact=1 for ((i=1;i<=n;i++)) do fact=` expr $fact \* $i` done echo Factorial : $fact

Output: Enter the number: 4 Factorial : 24

Practical-11
Aim: Write a shell script to check whether the string is palindrome. clear echo "Enter any string: " read a b=$a d=` expr $a | wc -c` c=` expr $d - 1`

while [ $c -gt 0 ] do n=` expr $a | cut -c $c` r=$r$n c=` expr $c - 1` done echo $r if [ $b == $r ] then echo "String is palindrome" else echo "Not palindrome string" fi

Output: Enter any string: nayan nayan String is palindrome.

Practical-12
Aim: Write a shell script that reverses the string. clear echo "Enter any string: " read a b=$a d=` expr $a | wc -c` c=` expr $d - 1`

while [ $c -gt 0 ] do n=` expr $a | cut -c $c` r=$r$n c=` expr $c - 1` done echo Reverse string: $r

Output: Enter any string: laptop Reverse string: potpal

Practical-13
Aim: Write a shell script for generating mark sheet, calculate grade also. clear echo "Enter no of students : " read ns for ((i=1 ; i<=ns ; i++ )) do echo "" echo "Enter roll no of student $i : " read rn[i] echo "Enter subject1 marks : " read s1[i] echo "Enter subject2 marks : " read s2[i] echo "Enter subject3 marks : " read s3[i] done for ((i=1 ; i<=ns ; i++)) do total[i]=` expr ${s1[i]} + ${s2[i]} + ${s3[i]} ` avg[i]=` expr ${total[i]} / 3 ` if [ $avg[i] -ge 70 ]

then grd[i]="DISTINCTION" elif [ $avg[i] -ge 60 -a $avg[i] -le 69 ] then grd[i]="FIRSTCLASS" elif [ $avg[i] -ge 50 -a $avg[i] -le 59 ] then grd[i]="SECONDCLASS" elif [ $avg[i] -ge 40 -a $avg[i] -le 49 ] then grd[i]="PASSCLASS" else grd[i]="FAIL" fi done for ((i=1 ; i<=ns ; i++)) do echo "" echo "-------------------------------------------------------------------" echo "Roll no : " ${rn[i]} echo "-------------------------------------------------------------------" echo " echo " echo " echo " Subject 1 Subject 2 Subject 3 Total " ${s1[i]} " ${s2[i]} " ${s3[i]} " ${total[i]}

echo "-------------------------------------------------------------------"

echo "-------------------------------------------------------------------" echo " echo " echo "" done Percentage : Grade : " ${avg[i]} " ${grd[i]} echo "-------------------------------------------------------------------" echo "-------------------------------------------------------------------"

Output: Enter no of student: 1 Enter roll no for student1: 21 Enter marks for subject1: 90 Enter marks for subject2: 91 Enter marks for subject3: 92

------------------------------------------------------------------Roll no: 21 ------------------------------------------------------------------Subject 1 90 Subject 2 91 Subject 3 92 ------------------------------------------------------------------Total 276 ------------------------------------------------------------------Percentage: 91 ------------------------------------------------------------------Grade: Distinction -------------------------------------------------------------------

Practical-14
Aim: write a shell script that evaluates the following series

1+1/1+1/2+1/3++1/n.

clear echo "Enter no upto which series to be printed" read n sum=1 for ((i=1;i<=n;i++)) do sum=`echo $sum + 1/$i | bc -l` done echo $sum

Output: Enter no upto which series to be printed: 2 2.5

Practical-15
Aim: Write a shell script for calendar for current month & year also for range of months. clear cal echo "" echo "" echo "Enter year" read y echo "Enter starting month" read m1 echo "Enter ending month" read m2 for ((i=m1;i<=m2;i++)) do cal $i $y done

Output: Enter year: 2009 Enter starting month: 2 Enter ending month: 3 Note: It will show February 2009 and March 2009 calendar.

Practical-16
Write a shell script to display the following menu. It should take appropriate action when an option is selected. 1) Files 1. List files 2. Make a file 3. delete a file 4. Copy a file 2) Directory 1. Make a directory 2. Display current directory 3. Change current directory 4. Remove current directory 3) Quit

clear echo "1. FILE" echo "2. DIRECTORY" echo "3. QUIT" echo "" echo -n "Enter your choice : " read ch case $ch in 1) echo "" echo "1. LIST FILES" echo "2. MAKE FILE" echo "3. DELETE FILE" echo "4. COPY FILE" echo ""

echo -n "Enter your choice : " read ch1 case $ch1 in 1) echo"" ls;; 2) echo "" echo -n "Enter file name : " read fn cat>$fn echo "File successfully created" echo "" ls;; 3) echo "" echo -n "Enter file name to delete : " read fn rm -r $fn echo "" echo "File successfully deleted" echo "" ls;; 4) echo "" echo -n "Enter file name from you want to copy : " read f1 echo -n "Enter file name to you want to copy : " read f2 cp $f1 $f2 echo "" echo "File successfully copied";; esac;;

2) echo "" echo "1. MAKE DIRECTORY" echo "2. DISPLAY CURRENT DIRECTORY" echo "3. CHANGE DIRECTORY" echo "4. RENAME DIRECTORY" echo "" echo -n "Enter your choice : " read ch2 case $ch2 in 1) echo "" echo -n "Enter the name for directory : " read dn mkdir $dn echo "" echo "Directory successfully created" echo "" ls;; 2) echo -n "Current directory : " pwd echo "";; 3) echo "" echo -n "Current directory : " pwd echo -n "Enter the name of directory you want to go to : " read dn cd $dn echo "Directory has been changed succesfully" echo -n "Curren directory : "

pwd echo "";; 4) echo "" echo -n "Enter old name : " read od echo -n "Enter new name : " read nd mv $od $nd echo "Directory successfully renamed" echo "";; esac;; 3) exit;; esac Output: 1. FILE 2. DIRECTORY 3. QUIT Enter your choice: 1 1. LIST FILES 2. MAKE FILE 3. DELETE FILE 4. COPY FILE Enter your choice: 1 All files in current directory are listed here.

Practical-17
Write a shell script to check whether the entered number is Armstrong number or not using the command line argument. clear no=$1 n=$1 while [ $n -gt 0 ] do a=` expr $n % 10` a3=` expr $a \* $a \* $a` temp=` expr $temp + $a3` n=` expr $n / 10` done if [ $temp -eq $no ] then echo "It is Armstrong number" else echo "It is not Armstrong number" fi

Output: >sh Armstrong.sh 159 It is not Armstrong number

Practical-18
Aim: Write a shell script to count number of characters, words & lines.

clear echo -n "Enter file name : " read fn

c=` wc -c<$fn ` w=` wc -w<$fn ` l=` wc -l<$fn ` echo "No. of characters : " $c echo "No. of Words : " $w echo "No. of Lines : " $l

Output: Enter file name: temp.sh No. of characters: 359 No. of words: 45 No. of lines: 6

Practical-19
Aim: Write a shell script to convert uppercase to lowercase latter for a given file.

clear echo "Enter file name : " read fn a=` tr '[A-Z]' '[a-z]' < $fn` echo $a

Output: Enter File name: temp.sh ALL THE CONTENT IS CONVERTED INTO CAPITAL LETTERS.

Practical-20
Aim: Write a shell script that prints only given no. of lines from top & bottom of a file.

clear echo -n "Enter file name : " read fn echo -n "No. of line : " read n t=` head -$n < $fn` b=` tail -$n < $fn` echo "Top : " echo $t echo "Bottom : " echo $b

Output: Enter file name: temp.sh No. of line: 3 Note: 3 lines from top and 3 lines from bottom are displayed here.

Practical-21
Aim: Write a shell script for comparision between two strings whether both the strings are equal or not.

clear echo "Enter String 1 : " read str1 echo "Enter String 2 : " read str2 s1=` expr $str1 | wc -c` s1=` expr $s1 - 1` s2=` expr $str2 | wc -c` s2=` expr $s2 - 1` flag=0 if [ $s1 -ne $s2 ] then echo "Strings are different" else for (( i=1 ; i<=s1 ; i++ )) do sc1=` expr $str1 | cut -c $i` sc2=` expr $str2 | cut -c $i` if [ $sc1 != $sc2 ]

then flag=1 break fi done if [ $flag -eq 0 ] then echo "Strings are equal" else echo "Strings are different" fi fi

Output: Enter string 1: Maulik Enter string 2: Dantara Strings are different.

Practical-22
Aim: Write a shell script that counts occurrence of a word in file. clear echo -n "Enter file name : " read fn echo -n "Enter the pattern : " read pt c=`grep -io $pt $fn | wc -w` echo "No. of occurrence : " $c

Output: Enter file name: temp.sh Enter the pattern: file No. of occurrence: 4

Practical-23
Aim: Write a shell script which display full names like January if we enter jan, janu, janua, and so on clear echo "Enter initials of month :" read mon case $mon in ja*) echo "Month is January.";; f*) echo "Month is February.";; mar*) echo "Month is March.";; ap*) echo "Month is April.";; may*) echo "Month is May.";; Jun*) echo "Month is June.";; jul*) echo "Month is July.";; au*) echo "Month is August.";; s*) echo "Month is September.";; o*) echo "Month is October.";; n*) echo "Month is November.";; d*) echo "Month is December.";; *) echo "Invalid month";; esac

Ouput: Enter initials of month: Jan Month is January.

Practical-24
Aim: Write a shell script that takes a command line argument & report whether it is a directory, a file or some else.

clear if test -f $1 then echo "it is a file" elif test -d $1 then echo "it is a directory" fi

Output: >sh checkfile.sh temp It is a directory.

Practical-25

Aim: Write a shell script to accept two filenames, if both files exist then contents of first file should appended to second. If second file does not exist then create it with the contents of first file. clear echo "Enter first file name :" read f1 echo "Enter second file name :" read f2 if test -f $f1 then echo "File $f1 is already exist" else echo "File created successfully" cat>$f1 fi if test -f $f2 then echo "File $f2 is alreadt exist" else echo "File created successfully" cat>$f2 fi cat<$f1>>$f2

Output: Enter first file name: tmp1.sh Enter second file name: tmp2.sh File tmp1 is already exist. File tmp2 created successfully. Note: All the content of tmp1 is copied to tmp2.

Potrebbero piacerti anche