Вы находитесь на странице: 1из 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 UNIX echo Enter your Choice echo 1. Date echo 2. Process of User echo 3. List of Files echo 4. Exit read ch case "$ch" in 1)date ;; 2)PS ;; 3)LS ;; 4)exit ;; *) echo invalid Choice esac

2. Write a Shell Script which will redirect the output of the date command without the time into a file. echo Enter the file name read file a= `date|cut -b1-11, 25-28` echo $ a|tee -a $file echo "\n" $file successfully created echo "\n" "content of file is:" `cat $file`

3. Write a Shell Script that accepts a filename as a command line argument and finds out if its a regular file or a directory. If its a regular file, then performs various tests to see if it is readable, writeable, executable etc. if [ -d $1 ] ; then echo "It is a Directory" fi if [ -f $1 ] ; then echo "It is a File" fi if [ ! -r $1 ] ; then echo "File is not readable" else echo "File is Readable" fi if [ -w $1 ] ; then echo "File is Writable" else echo "File is not Writable" fi if [ -x $1 ] ; then echo "File is Executable" else echo "File Is not Executable" fi

4. Write a Shell Script that computes the factorial of a given number echo "Enter any no for Factorial" read no a=$no i=1; while test $no -ge 1 do i=`expr $i \* $no` no=`expr $no - 1` done echo "Factorial of " $a " is "$i

5. Write a Shell Script that works like a calendar reminding the user of certain things depending on the day of the week. a=`date +%A` echo "\n" Today is $a echo Your tasks for today is as follows case $a in Monday) echo Come late in class ;; Tuesday) echo Just two lectures and then masti ;; Wednesday) echo Boring Lectures ;; Thursday) echo Yupiiieeee Foriegn Language Lecture ;; Friday) echo Last Working Day ;; Saturday) echo Movie ;; Sunday) echo Sleeping ;; Esac

6. Write a Shell Script which takes a command line argument of Kms and by default converts that number into meters. Also provide options to convert km to dm and km to cm. echo "Enter kms" read no a=$no i=1 p=1000 i=`expr $a \* $p` echo "In meters = " $i echo "Enter choice for operation 1 for dm or 2 for cm" read op case $op in 1) c=`expr $a \* 10000` echo "In dm" $c ;; 2) d=`expr $a \* 100000` echo "In cm" $d ;; esac

7. Write a Shell Script that performs a count-down either from 10 (default) or from the value that is entered by the user. a=10 while test $a -ge 1 do echo $a sleep 5 a=`expr $a - 1` done

8. If a number is input through the keyboard, WASS to calculate sum of its digits. echo "Enter the number =" read no a=$no e=0 while test $a -ge 1 do r=`expr $a \% 10` a=`expr $a \/ 10` e=`expr $e + $r` done echo $e

9. Write a Shell Script using for loop, which displays the message "Welcome to the UNIX System" Echo displayin a message For((i=0;i<5;i++)) do echo welcome to unix done

10. WAP to calculate and print the first m Fibonacci numbers. echo "Enter any no" read no n1=-1 n2=1 i=1 echo "The fibonacci series is" while test $i -le $no do n3=`expr $n1 + $n2` echo $n3 n1=$n2 n2=$n3 i=`expr $i + 1` done

11. WASS to compute the GCD and LCM of two numbers. echo "Enter the first number :" read a echo "Enter the second number : " read b if [ $a -gt $b ] then num=$a den=$b else num=$b den=$a fi r=`expr $num % $den` while [ $r -ne 0 ] do num=$den den=$r r=`expr $num % $den` done gcd=$den lcm=`expr $a \* $b / $gcd` echo " The LCM of $a and $b is : $lcm" echo " The GCD of $a and $b is : $gcd"

12. Two numbers are entered through the keyboard. WAP to find the value of one number raised to the power of another. Echo "Enter the no." read no echo "Enter it's power" read pw i=1 c=1 while test $i -le $pw do c=`expr $c \* $no` i=`expr $i + 1` done echo "the power "$pw" of "$no" is "$c

13. WAP to generate all combinations of 1, 2 and 3 using for loop. for i in 1 2 3 do for j in 1 2 3 do for k in 1 2 3 do echo $i$j$k done done done

14. WASS that repeatedly asks the user repeatedly for the Name of the Institution until the user gives the correct answer. echo "Name of the Instituition" read str if test $str = aiit then echo "Name of instituition is aiit" else echo "Wrong name" fi

15. Write a Shell Script that changes the extension of a group of files from txt to doc for file in *.txt do leftname=`basename $file txt` mv $file ${leftname}doc done

16. Write a Shell Script that receives two filenames as arguments. It should check whether content of the two files is same or not. If they are same, second file should be deleted if [ -f $1 -a -f $2 ] then if diff $1 $2 then cat $1 echo "\n" cat $2 echo contents are same, second file is being deleted rm $2 else echo contents are different fi else echo "file does not exist" fi

17. Write a Shell Script (using while loop) to execute endlessly (until terminated by user) a loop which displays contents of current directory, disk space status, sleep for 30 seconds and display the users currently logged in on the screen. char=y while [ $char="y" ] do ls df -h sleep 2 echo "want to continue ?" read char done

18. Write a Shell Script to change the filename of all files in a directory from lower-case to upper-case. for i in * do mv $i `echo $i|tr "[:lower:]" "[:upper:]"` done

19. Write a Shell Script that examines each file in the current directory. Files whose names end in old are moved to a directory named old files and files whose names end in .c are moved to directory named cprograms. for i in *.old do mv $i oldfiles done for i in *.c do mv $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 File is small-sized if total no. of lines is <50 File is medium-sized if total no. of lines between 50&100 Else, file is large-sized. Code: 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 sized..." elif [ `cat $i|wc -l` -gt 100 ] then echo "'$i' is large sized..." else echo "'$i' is medium sized..." 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 exceeding 1000 bytes. The filenames should be printed in descending order of their sizes. The total number of such files should also be reported Code: if [ ! -d $1 ] then echo "'$1' is not a directory..." elif [ `expr $#` -eq 0 ] then echo "ERROR...! (no arguements passed)" else echo ":::::FILES (exceeding 1000):::::" echo " " cd $1 ls -l|grep [1-9][0-9][0-9][0-9].|cut -d " " -f 5,9|sort -g -r echo "----- ------------" echo "TOTAL= `ls -l|grep -c [1-9][0-9][0-9][0-9].`" fi

Ques: Write a Shell Script for renaming each file in the directory such that it will have the current shell PID as an extension. The shell script should ensure that the directories do not get renamed... Code: echo "Enter Directory- " read DIR if [ ! -d $DIR ] then echo "$DIR is not a directory..." else cd $DIR echo ":::::CONTENTS OF $DIR:::::" ls -l for i in * do if [ -f $i ] then mv $i $i.`echo $$` fi done echo ":::::CONTENTS OF $DIR (new):::::" ls -l fi

Ques: Write a Shell Script that will receive any number of filenames as arguments. The shell script should check whether such files already exist. If they do, then it should be reported. The files that do not exist should be created in a sub-directory called mydir. The shell script should first check whether the sub-directory mydir exists in the current directory. If it doesnt exist, then it should be created. If mydir already exists, then it should be reported along with the number of files that are currently present in mydir. Code: for i in $* do if [ -e $i ] then if [ -f $i ] then echo "'$i' exists...(file)" elif [ -d $i ] then echo "'$i' exists...(directory)" fi else echo "'$i' doesnot exists..." echo File created: `date| cut -c 1-10,20-28` > $i echo "'$i' created..." if [ ! -e MYDIR ] then echo 'MYDIR' doesnot exist... mkdir MYDIR echo Created 'MYDIR'... fi mv $i /home/aman/MYDIR/$i echo "'$i' moved to MYDIR..." 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. If odd number of filenames is supplied then no copying should take place and an error message should be displayed Code: if [ `expr $# % 2` -ne 0 ] then echo "ERROR...! (odd number of files passed)" elif [ `expr $#` -eq 0 ] then echo "ERROR...! (no files passed)" else count=0 for i in $* do count=`expr $count + 1` if [ $count -eq 1 ] then file1=$i fi if [ $count -eq 2 ] then file2=$i echo $file1 copied to $file2 cp $file1 $file2 count=0 fi done fi

Ques: Write a shell script to identify all zero-byte files in the current directory and delete them. Before proceeding with deletion, the shell script should get a confirmation from the user Code: echo ALL FILES ls for i in * do if [ -f $i -a ! -s $i ] then rm -i $i fi done echo "ALL FILES (empty files removed)" l

Ques: Write a shell script that prompts the user for the password. The user has maximum of 3 attempts. If the user enters the correct password, the message Correct Password is displayed else the message Wrong Password Code: count=1 while [ $count -le 3 ] do echo Enter Passwordread pass if [ $pass == "ak727" ] then echo CORRECT PASSWORD... exit else if [ `expr 3 - $count` == 0 ] then echo "WRONG PASSWORD (No more attempt available)..." exit fi echo "WRONG PASSWORD (`expr 3 - $count` attemp(s) left)..." count=`expr $count + 1` fi done

Ques: Write a Shell Script that takes a search string and filename from the terminal & displays the results Code: echo "Enter file- " read FILE echo "Enter Pattern (to search)- " read ch if [ ! -f $FILE ] then echo "'$FILE' is not a file..." else if [ `grep -c $ch $FILE` -gt 0 ] then echo "Pattern '$ch' is found in '$FILE' '`grep -c $ch $FILE` 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 found/pattern not found Code: if [ `expr $#` -eq 0 ] then echo "ERROR...! (no arguments passed)" elif [ `expr $#` -eq 1 ] then echo "ERROR...! (file no passed)" else if [ ! -f $2 ] then echo "'$2' is not a file..." else if [ `grep -c $1 $2` -gt 0 ] then echo "Pattern '$1' is found in '$2' '`grep -c $1 $2` time(s)' " fi fi fi

Ques: Write a Shell Script that accepts only three arguments from the command line. The 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... Code: if [ `expr $#` -gt 3 ] then echo "ERROR...! (extra arguments passed)" elif [ `expr $#` -lt 3 ] then echo "ERROR...! (Required argument(s) not passed)" else if [ ! -f $2 ] then echo "'$2' is not a file..." if [ ! -f $3 ] then echo "'$3' is also not a file..." fi elif [ ! -f $3 ] then echo "'$3' is not a file..." else if [ `grep -c $1 $2` -gt 0 ] then echo "Pattern '$1' is found in '$2' '`grep -c $1 $2` time(s)'" > $3 else echo "Pattern '$1' is not found in '$2'" > $3 fi fi fi

Вам также может понравиться