2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix...

17
2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name of the command and executes it. Program :- echo "enter command name" read cmd $cmd Output :- enter command name cal February 2016 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 2) Write a shell script Which works like calculator and performs below operations Addition , Subtract ,Division ,Multiplication Program :- i) using if..elif statement echo " Enter one no." read n1 echo "Enter second no." read n2 echo "1.Addition" echo "2.Subtraction" echo "3.Multiplication" echo "4.Division" echo "Enter your choice" read ch if [ $ch = "1" ] then sum=`expr $n1 + $n2` echo "Sum ="$sum elif [ $ch = "2" ] then sum=`expr $n1 - $n2` echo "Sub = "$sum elif [ $ch = "3" ]

Transcript of 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix...

Page 1: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 1

1) Write a shell script to scans the name of the command and executes it. Program :- echo "enter command name"

read cmd $cmd

Output :- enter command name

cal February 2016

Su Mo Tu We Th Fr Sa

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29

2) Write a shell script Which works like calculator and performs below operations Addition , Subtract ,Division ,Multiplication

Program :- i) using if..elif statement echo " Enter one no."

read n1 echo "Enter second no." read n2 echo "1.Addition" echo "2.Subtraction" echo "3.Multiplication" echo "4.Division" echo "Enter your choice" read ch if [ $ch = "1" ] then sum=`expr $n1 + $n2` echo "Sum ="$sum elif [ $ch = "2" ] then sum=`expr $n1 - $n2` echo "Sub = "$sum elif [ $ch = "3" ]

Page 2: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 2

then sum=`expr $n1 \* $n2` echo "Mul = "$sum elif [ $ch = "4" ] then sum=`expr $n1 / $n2` echo "Div = "$sum fi

Output :- Enter one no.

32 Enter second no. 12 1.Addition 2.Subtraction 3.Multiplication 4.Division Enter your choice 2 Sub = 20

ii) using while loop and switch statement i="y"

while [ $i = "y" ] do echo " Enter one no." read n1 echo "Enter second no." read n2 echo "1.Addition" echo "2.Subtraction" echo "3.Multiplication" echo "4.Division" echo "Enter your choice" read ch case $ch in 1)sum=`expr $n1 + $n2` echo "Sum ="$sum;; 2)sum=`expr $n1 - $n2` echo "Sub = "$sum;; 3)sum=`expr $n1 \* $n2`

Page 3: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 3

echo "Mul = "$sum;; 4)sum=`expr $n1 / $n2` echo "Div = "$sum;; *)echo "Invalid choice";; esac echo "Do u want to continue ? y/n" read i if [ $i != "y" ] then exit fi done

Output :- Enter one no.

32 Enter second no. 22 1.Addition 2.Subtraction 3.Multiplication 4.Division Enter your choice 2 Sub = 10 Do u want to continue ? y/n N

3) Write a shell script to print the pyramid structure for the given number. Program :- echo "enter the number"

read n printf "\n" for((i=1;i<=$n;i++)) do for((j=1;j<=$i;j++)) do printf "$j" done printf "\n" done

Page 4: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 4

Output :- enter the number

4 1 12 123 1234

4) Write a shell script to find the largest among the 3 given numbers. Program :- clear

echo "Enter first number: " read a echo "Enter second number: " read b echo "Enter third number: " read c if [ $a -ge $b -a $a -ge $c ] then echo "$a is largest integer" elif [ $b -ge $a -a $b -ge $c ] then echo "$b is largest integer" elif [ $c -ge $a -a $c -ge $b ] then echo "$c is largest integer" fi

Output :- Enter first number:

22 Enter second number: 33 Enter third number: 42 44 is largest integer

5) Write a shell script to find factorial of given number n. Program :- clear

fact=1

Page 5: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 5

echo "Enter number to find factorial : " read n a=$n #if enter value less than 0 if [ $n -le 0 ] then echo "invalid number" exit fi #factorial logic while [ $n -ge 1 ] do fact=`expr $fact \* $n` n=`expr $n - 1` done echo "Factorial for $a is $fact"

Output :- Enter number to find factorial :

5 Factorial for 5 is 120

6) Write a shell script to print all prime numbers from 1 to n. Program :-

clear echo "enter the range"

read n echo "the prime no are:" m=2 while [ $m -le $n ] do i=2 flag=0 while [ $i -le `expr $m / 2` ] do if [ `expr $m % $i` -eq 0 ] then flag=1 break fi i=`expr $i + 1` done

Page 6: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 6

if [ $flag -eq 0 ] then echo $m fi m=`expr $m + 1` done

Output :- enter the range

10 the prime no are 2 3 5 7

7) Write a shell script toreverse a number supplied by a user. Program :- if [ $# -eq 1 ]

then if [ $1 -gt 0 ] then num=$1 sumi=0 while [ $num -ne 0 ] do lnum=`expr $num % 10` sumi=`expr $sumi \* 10 + $lnum` num=`expr $num / 10` done echo "Reverse of digits is $sumi of $1" else echo " Number is less than 0" fi else echo "Insert only one parameter " fi

Output :- bash pr81.sh 123

Reverse of digits is 321 of 123

Page 7: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 7

8) Write a shell script to find first n Fibonacci numbers like: 0 1, 1, 2, 3, 5, 13,… Program :- clear

echo "How many number of terms to be generated ?" read n x=0 y=1 i=2 echo "Fibonacci Series up to $n terms :" echo "$x" echo "$y" while [ $i -lt $n ] do i=`expr $i + 1 ` z=`expr $x + $y ` echo "$z" x=$y y=$z done

Output :- How many number of terms to be generated?

5 Fibonacci Series up to 5 terms : 0 1 1 2 3

9) Write a shell script to check whether the given number is Perfect or not. Program :- echo Enter a number

read no i=1 ans=0 while [ $i -le `expr $no / 2` ] do if [ `expr $no % $i` -eq 0 ] then ans=`expr $ans + $i` fi

Page 8: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 8

i=`expr $i + 1` done if [ $no -eq $ans ] then echo $no is perfect else echo $no is NOT perfect fi

Output :- Enter a number

6 6 is perfect Enter a number 10 10 is NOT perfect

10) Write a shell script which displays a list of all files in the current directory to which you have read, write and execute permissions

Program :- for File in *

do if [ -r $File -a -w $File -a -x $File ] then echo $File fi done

Output :- Desktop

Documents Downloads lab Music Pictures Public shellscript Templates Videos

Page 9: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 9

11) Write a shell script that deletes all the files in the current directory which are 0 bytes in length.

Program :- clear

find . -name "*" -size -1k –delete echo “files deleted”

Output :- files deleted

12) Write a shell script to check whether the given string is Palindrome or not.

Program :- clear

echo "Enter the string:" read str echo len=`echo $str | wc -c` len=`expr $len - 1` i=1 j=`expr $len / 2` while test $i -le $j do k=`echo $str | cut -c $i` l=`echo $str | cut -c $len` if test $k != $l then echo "String is not palindrome" exit fi i=`expr $i + 1` len=`expr $len - 1` done echo "String is palindrome"

Output :- Enter the string:

abba String is palindrome Enter the string: abc String is not palindrome

Page 10: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 10

13) Write a shell script to display the digits which are in odd position in a given 5 digit number

Program :- echo "Enter a 5 digit number"

read num n=1 while [ $n -le 5 ] do a=`echo $num | cut -c $n` echo $a n=`expr $n + 2` done

Output :- Enter a 5 digit number

12345 1 3 5

14) Write a shell script tocheck given year is leap year or not. Program :- clear

echo "enter any year" read num if [ `expr $num % 4` -eq 0 ] then if [ `expr $num % 100` -eq 0 -a `expr $num % 400` -ne 0 ] then echo "Not a leap year" else echo "Leap year " fi else echo "Not a leap year" fi

Output :- enter any year

2016 Leap year

Page 11: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 11

enter any year 2100 Not a leap year

15) Write a shell script tofind the value of one number raised to the power of another. Program :- echo "Input number"

read no echo "Input power" read power counter=0 ans=1 while [ $power -ne $counter ] do ans=`echo $ans \* $no | bc` counter=`echo $counter + 1 | bc` done echo "$no power of $power is $ans"

Output :- Input number

5 Input power 3 5 power of 3 is 125

16) Write a shell script to display the following details in a pay list Pay slip details, House rent allowance, Dearness allowance, Provident fund. HRA is to be calculated at the rate of 20% of basic, DA at the rate of 40% of basic and PF at the rate of 10% of basic.

Program :- i="y"

while [ $i = "y" ] do echo "Please enter your Basic:" read basic echo "PAY SLIP DETAILS" echo "1. HOUSE RENT ALLOWANCE" echo "2. DEARNESS ALLOWANCE" echo "3. PROVIDENT FUND" echo "your choice:" read ch case $ch in

Page 12: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 12

1) hra=`expr $basic \* 20 / 100` echo Your HOUSE RENT ALLOWANCE is Rs. $hra;; 2) da=`expr $basic \* 40 / 100` echo Your DEARNESS ALLOWANCE is Rs. $da;; 3) pf=`expr $basic \* 10 / 100` echo Your PPOVIDENT FUND is Rs. $pf;; *) echo "Not a valid choice";; esac echo "Do u want to continue ?" read i if [ $i != "y" ] then exit fi done

Output :- Please enter your Basic:

1000 PAY SLIP DETAILS 1. HOUSE RENT ALLOWANCE 2. DEARNESS ALLOWANCE 3. PROVIDENT FUND your choice: 1 Your HOUSE RENT ALLOWANCE is Rs. 200 Do u want to continue ?

17) Write a shell script to find sum of digits of a number.

Program :- clear

echo "enter the number" read n sum=0 a=$n while(($n >0)) do x=`expr $n % 10` sum=`expr $sum + $x` n=`expr $n / 10` done echo "the sum of $a is $sum"

Page 13: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 13

Output :- enter the number

3355 the sum of 3355 is 16

18) Write a shell script that greets the user by saying Good Morning, Good Afternoon, and Good Evening according to the system time.

Program :- clear

#hours=`date|cut -c 12-13` hours=`date +%H` if [ $hours -le 12 ] then echo "Good Morning" elif [ $hours -le 16 ] then echo "Good Afternoon" elif [ $hours -le 20 ] then echo "Good Evening" else echo "Good Night" fi

Output :- Good Afternoon

19) Write a shell script to generate mark sheet of a student. Take 3 subjects, calculate and display total marks, percentage and Class obtained by the student.

Program :- Clear

echo "Enter the five subject marks for the student" read s1 s2 s3 sum1=`expr $s1 + $s2 + $s3` echo "Sum of 5 subjects are: " $sum1 per=`expr $sum1 / 3` echo " Percentage: " $per if [ $per -ge 60 ] then echo "You get Distinction" elif [ $per -ge 50 ] then echo “You get First class―

Page 14: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 14

elif [ $per -ge 40 ] then echo "You get Second class" else echo "You get Fail" fi

Output :- Enter the five subject marks for the student

78 88 92 Sum of 5 subjects are: 258 Percentage: 86 You get Distinction

20) Write a shell script that finds total no. of users and finds out how many of them are currently logged in.

Program :- cat /etc/passwd>user.txt

set `wc -l user.txt` log=`who|wc -l` echo "There are $1 users in network " echo "There are $log user loged in right now"

Output :- There are 49 users in network

There are 2 user loged in right now 21) Write a shell script which checks whether a given user is valid or not

Program :- read user

echo echo "Validating the $user ..." if [ `grep -c $user /etc/passwd` -eq 0 ] then echo echo "ERROR : PLEASE ENTER A VALID USERNAME." echo "Exiting ..." else echo "Valid user " fi

Output :- abc

Validating the abc PLEASE ENTER A VALID USERNAME.

Page 15: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 15

Exiting ...

22) Write a shell script that check whether the scanned string is found in a file or not. Display appropriate message.

Program:- echo "enter file name"

read f echo "enter string" read a; b=`grep $a $f`; if [ $? -eq 0 ] then echo character found ; fi

Output :- enter file name

f1.txt enter string hello string found

23) Write a shell script to perform operations like compare strings, concatenate strings, find length of a string, occurrence of word in a string and reverse a string

Program:- clear

i="y" a=0 t=0 while [ $i = "y" ] do clear echo "1.Compare 2 strings :" echo "2.Concatanet string" echo "3.Find length of string" echo "4.Occurance of word" echo "5.Reverse of string" echo "6.Exit" echo "Enter your choice" read ch case $ch in 1)echo "Enter first String" read s1

Page 16: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 16

echo "Enter second string " read s2 if [ $s1 = $s2 ] then echo "Two strings are equal "else echo "Two strings are not equal" fi;; 2)echo "Enter one string " read s1 echo "Enter second string " read s2 echo $s1 $s2;; 3)echo "Enter any String" read s1 t=`echo $s1|wc -c` t=`expr $t - 1` echo "Length of "$s1" is "$t;; 4)echo "Enter any String " read s1 echo "Enter word u want to find occurance of:" read c1 t=`echo $s1|wc -c` t=`expr $t - 1` echo "length "$t while [ $t -ne 0 ] do temp=`echo $s1|cut -c $t` temp2=`echo $temp2 $temp` #echo $temp2 if [ $temp2 = $c1 ] then a=`expr $a + 1` t=`expr $t - 1` else t=`expr $t - 1` fi done echo "Occurance of "$c1" is "$a;; 5)echo "Enter any string :" read s1 t=`echo $s1|wc -c` t=`expr $t - 1`

Page 17: 2140702 Unix shell scripts - Top Engineering · PDF file2140702 – Operating System Unix shell scripts [Asst. Prof. Umesh H. Thoriya] Page 1 1) Write a shell script to scans the name

2140702 – Operating System Unix shell scripts

[Asst. Prof. Umesh H. Thoriya] Page 17

echo "length "$t while [ $t -ne 0 ] do temp=`echo $s1|cut -c $t` echo $temp temp2=`echo $temp2 $temp` t=`expr $t - 1` done echo $temp2;; 6)exit;; *)echo "Invalid choice";; esac echo "Do u want to continue ?" read i if [ $i != "y" ] then exit fi done

24) Write a Shell Script that Accept filename and displays last modification time if file exists, otherwise display appropriate message.

Program:- echo -n "Enter name of the file:"

read filename if [ -f $filename ] then echo 'Last modification time is '`ls -lt $filename | tr -s " " "," | cut -d "," -f7` else echo "File not exist" fi

Output:- Enter name of the file:

demo.txt 'Last modification time is 08:05