Você está na página 1de 37

1.

Write a Shell Script which creates the following menu and prompts for choice from user and runs the chosen command. a. Today's date b. Process of user c. List of files d. Quit to U !" echo #nter your $hoice echo 1. %ate echo &. Process of User echo '. List of (iles echo ). #*it read ch case +,ch+ in 1-date .. &-PS .. '-LS .. )-e*it .. /- echo in0alid $hoice esac

&. Write a Shell Script which will redirect the output of the date command without the time into a file. echo #nter the file name read file a1 2date3cut 4b14115 &64&72 echo , a3tee 4a ,file echo +8n+ ,file successfully created echo +8n+ +content of file is9+ 2cat ,file2

'. Write a Shell Script that accepts a filename as a command line argument and finds out if its a regular file or a directory. !f its a regular file5 then performs 0arious tests to see if it is readable5 writeable5 e*ecutable etc. if : 4d ,1 ; . then echo +!t is a %irectory+ fi if : 4f ,1 ; . then echo +!t is a (ile+ fi if : < 4r ,1 ; . then echo +(ile is not readable+ else echo +(ile is =eadable+ fi if : 4w ,1 ; . then echo +(ile is Writable+ else echo +(ile is not Writable+ fi if : 4* ,1 ; . then echo +(ile is #*ecutable+ else echo +(ile !s not #*ecutable+ fi

). Write a Shell Script that computes the factorial of a gi0en number echo +#nter any no for (actorial+ read no a1,no i11. while test ,no 4ge 1 do i12e*pr ,i 8/ ,no2 no12e*pr ,no 4 12 done echo +(actorial of + ,a + is +,i

6. Write a Shell Script that wor>s li>e a calendar reminding the user of certain things depending on the day of the wee>. a12date ?@A2 echo +8n+ Today is ,a echo Bour tas>s for today is as follows case ,a in Conday- echo $ome late in class .. Tuesday- echo Dust two lectures and then masti .. Wednesday- echo Eoring Lectures .. Thursday- echo Bupiiieeee (oriegn Language Lecture .. (riday- echo Last Wor>ing %ay .. Saturday- echo Co0ie .. Sunday- echo Sleeping .. #sac

F. Write a Shell Script which ta>es a command line argument of Gms and by default con0erts that number into meters. Also pro0ide options to con0ert >m to dm and >m to cm. echo +#nter >ms+ read no a1,no i11 p11HHH i12e*pr ,a 8/ ,p2 echo +!n meters 1 + ,i echo +#nter choice for operation 1 for dm or & for cm+ read op case ,op in 1c12e*pr ,a 8/ 1HHHH2 echo +!n dm+ ,c .. &d12e*pr ,a 8/ 1HHHHH2 echo +!n cm+ ,d .. esac

I. Write a Shell Script that performs a count4down either from 1H Jdefault- or from the 0alue that is entered by the user. a11H while test ,a 4ge 1 do echo ,a sleep 6 a12e*pr ,a 4 12 done

7. !f a number is input through the >eyboard5 WASS to calculate sum of its digits. echo +#nter the number 1+ read no a1,no e1H while test ,a 4ge 1 do r12e*pr ,a 8@ 1H2 a12e*pr ,a 8K 1H2 e12e*pr ,e ? ,r2 done echo ,e

L. Write a Shell Script using for loop5 which displays the message +Welcome to the U !" System+ #cho displayin a message (orJJi1H.iM6.i??-do echo welcome to uni* done

1H. WAP to calculate and print the first m (ibonacci numbers. echo +#nter any no+ read no n1141 n&11 i11 echo +The fibonacci series is+ while test ,i 4le ,no do n'12e*pr ,n1 ? ,n&2 echo ,n' n11,n& n&1,n' i12e*pr ,i ? 12 done

11. WASS to compute the N$% and L$C of two numbers. echo +#nter the first number 9+ read a echo +#nter the second number 9 + read b if : ,a 4gt ,b ; then num1,a den1,b else num1,b den1,a fi r12e*pr ,num @ ,den2 while : ,r 4ne H ; do num1,den den1,r r12e*pr ,num @ ,den2 done gcd1,den lcm12e*pr ,a 8/ ,b K ,gcd2 echo + The L$C of ,a and ,b is 9 ,lcm+ echo + The N$% of ,a and ,b is 9 ,gcd+

1&. Two numbers are entered through the >eyboard. WAP to find the 0alue of one number raised to the power of another. #cho +#nter the no.+ read no echo +#nter it's power+ read pw i11 c11 while test ,i 4le ,pw do c12e*pr ,c 8/ ,no2 i12e*pr ,i ? 12 done echo +the power +,pw+ of +,no+ is +,c

1'. WAP to generate all combinations of 15 & and ' using for loop. for i in 1 & ' do for O in 1 & ' do for > in 1 & ' do echo ,i,O,> done done done

1). WASS that repeatedly as>s the user repeatedly for the P ame of the !nstitutionQ until the user gi0es the correct answer. echo + ame of the !nstituition+ read str if test ,str 1 aiit then echo + ame of instituition is aiit+ else echo +Wrong name+ fi

16. Write a Shell Script that changes the e*tension of a group of files from t*t to doc for file in /.t*t do leftname12basename ,file t*t2 m0 ,file ,RleftnameSdoc done

1F. Write a Shell Script that recei0es two filenames as arguments. !t should chec> whether content of the two files is same or not. !f they are same5 second file should be deleted if : 4f ,1 4a 4f ,& ; then if diff ,1 ,& then cat ,1 echo +8n+ cat ,& echo contents are same5 second file is being deleted rm ,& else echo contents are different fi else echo +file does not e*ist+ fi

1I. Write a Shell Script Jusing while loop- to e*ecute endlessly Juntil terminated by user- a loop which displays contents of current directory5 dis> space status5 sleep for 'H seconds and display the users currently logged in on the screen. char1PyQ while : ,char1+y+ ; do ls df 4h sleep & echo +want to continue T+ read char done

17. Write a Shell Script to change the filename of all files in a directory from lower4case to upper4case. for i in / do m0 ,i 2echo ,i3tr +:9lower9;+ +:9upper9;+2 done

1L. Write a Shell Script that e*amines each file in the current directory. (iles whose names end in old are mo0ed to a directory named old files and files whose names end in .c are mo0ed to directory named cprograms. for i in /.old do m0 ,i oldfiles done for i in /.c do m0 ,i cprograms done

Ques: Write a Shell Script which searches all files in the given directory (to be taken as

command line argument) for the file having the title (to be taken as command line argument), as the first line in the file and display the contents of the searched file and then, print the size, where !ile is small"sized if total no# of lines is $%& !ile is medium"sized if total no# of lines between %&'(&& )lse, file is large"sized# *ode: if [ -d $1 ] then echo It is a directory... cd $1 for i in * do if [ -d $i ] then echo "'$i' is a directory..." else if [ `cat $i|wc -l` -lt 50 ] then echo "'$i' is small si ed..." elif [ `cat $i|wc -l` -!t 100 ] then echo "'$i' is lar!e si ed..." else echo "'$i' is medi"m si ed..." fi fi done else echo "'$1' is not a directory..." fi

Ques: Write a shell script which reports names and sizes of all files in a directory (directory would be supplied as an argument to the shell script) whose size is e+ceeding (&&& bytes# ,he filenames should be printed in descending order of their sizes# ,he total number of such files should also be reported *ode: if [ # -d $1 ] then echo "'$1' is not a directory..." elif [ `e$%r $&` -e' 0 ] then echo "())*)...# +no ar!"ements %assed," else echo "-----.I/(0 +e$ceedin! 1000,-----" echo " " cd $1 ls -l|!re% [1-1][0-1][0-1][0-1].|c"t -d " " -f 521|sort -! -r echo "----- ------------" echo "3*34/5 `ls -l|!re% -c [1-1][0-1][0-1][0-1].`" fi

Ques: Write a Shell Script for renaming each file in the directory such that it will have the current shell -./ as an e+tension# ,he shell script should ensure that the directories do not get renamed ### *ode: echo "(nter 6irectory- " read 6I) if [ # -d $6I) ] then echo "$6I) is not a directory..." else cd $6I) echo "-----7*83(830 *. $6I)-----" ls -l for i in * do if [ -f $i ] then m9 $i $i.`echo $$` fi done echo "-----7*83(830 *. $6I) +new,-----" ls -l fi

Ques: Write a Shell Script that will receive any number of filenames as arguments# ,he shell script should check whether such files already e+ist# .f they do, then it should be reported# ,he files that do not e+ist should be created in a sub"directory called mydir# ,he shell script should first check whether the sub"directory mydir e+ists in the current directory# .f it doesn0t e+ist, then it should be created# .f mydir already e+ists, then it should be reported along with the number of files that are currently present in mydir # *ode: for i in $* do if [ -e $i ] then if [ -f $i ] then echo "'$i' e$ists...+file," elif [ -d $i ] then echo "'$i' e$ists...+directory," fi else echo "'$i' doesnot e$ists..." echo .ile created- `date| c"t -c 1-102:0-:;` < $i echo "'$i' created..." if [ # -e =>6I) ] then echo '=>6I)' doesnot e$ist... m?dir =>6I) echo 7reated '=>6I)'... fi m9 $i @home@aman@=>6I)@$i echo "'$i' mo9ed to =>6I)..." fi done

Ques: Write a shell script receives even number of filenames# Suppose four filenames are supplied, then the first file should get copied into second file, the third file should get copied into fourth and so on# .f odd number of filenames is supplied then no copying should take place and an error message should be displayed *ode: if [ `e$%r $& A :` -ne 0 ] then echo "())*)...# +odd n"mBer of files %assed," elif [ `e$%r $&` -e' 0 ] then echo "())*)...# +no files %assed," else co"nt50 for i in $* do co"nt5`e$%r $co"nt C 1` if [ $co"nt -e' 1 ] then file15$i fi if [ $co"nt -e' : ] then file:5$i echo $file1 co%ied to $file: c% $file1 $file: co"nt50 fi done fi

Ques: Write a shell script to identify all zero"byte files in the current directory and delete them# 1efore proceeding with deletion, the shell script should get a confirmation from the user *ode: echo 4// .I/(0 ls for i in * do if [ -f $i -a # -s $i ] then rm -i $i fi done echo "4// .I/(0 +em%ty files remo9ed," l

Ques: Write a shell script that prompts the user for the password# ,he user has ma+imum of 2 attempts# .f the user enters the correct password, the message 3*orrect -assword4 is displayed else the message 3Wrong -assword4 *ode: co"nt51 while [ $co"nt -le D ] do echo (nter Easswordread %ass if [ $%ass 55 "a?F:F" ] then echo 7*))(73 E400G*)6... e$it else if [ `e$%r D - $co"nt` 55 0 ] then echo "G)*8H E400G*)6 +8o more attem%t a9ailaBle,..." e$it fi echo "G)*8H E400G*)6 +`e$%r D - $co"nt` attem%+s, left,..." co"nt5`e$%r $co"nt C 1` fi done

Ques: Write a Shell Script that takes a search string and filename from the terminal ' displays the results *ode: echo "(nter file- " read .I/( echo "(nter Eattern +to search,- " read ch if [ # -f $.I/( ] then echo "'$.I/(' is not a file..." else if [ `!re% -c $ch $.I/(` -!t 0 ] then echo "Eattern '$ch' is fo"nd in '$.I/(' '`!re% -c $ch $.I/(` time+s, ' " fi fi

Ques: Write a Shell Script that takes pattern and filename as command line arguments and displays the results appropriately i#e# pattern found5pattern not found *ode: if [ `e$%r $&` -e' 0 ] then echo "())*)...# +no ar!"ments %assed," elif [ `e$%r $&` -e' 1 ] then echo "())*)...# +file no %assed," else if [ # -f $: ] then echo "'$:' is not a file..." else if [ `!re% -c $1 $:` -!t 0 ] then echo "Eattern '$1' is fo"nd in '$:' '`!re% -c $1 $:` time+s,' " fi fi fi

Ques: Write a Shell Script that accepts only three arguments from the command line# ,he first argument is the pattern string, the second argument is the filename in which the pattern is to be searches and the third argument is the filename in which the result is to be stored# Write a Shell Script that accepts both filename and a set of patterns as positional parameters to a script### *ode: if [ `e$%r $&` -!t D ] then echo "())*)...# +e$tra ar!"ments %assed," elif [ `e$%r $&` -lt D ] then echo "())*)...# +)e'"ired ar!"ment+s, not %assed," else if [ # -f $: ] then echo "'$:' is not a file..." if [ # -f $D ] then echo "'$D' is also not a file..." fi elif [ # -f $D ] then echo "'$D' is not a file..." else if [ `!re% -c $1 $:` -!t 0 ] then echo "Eattern '$1' is fo"nd in '$:' '`!re% -c $1 $:` time+s,'" < $D else echo "Eattern '$1' is not fo"nd in '$:'" < $D fi fi fi

Você também pode gostar