Unix Shell Programming Solved
February 23, 2017 | Author: Amandeep Singh | Category: N/A
Short Description
Unix Shell Programming Solved...
Description
1. Write a shell script to print the users logged in, the current date and the calendar for the year 2010. echo "Program to print user logged in, current date and calender" echo "Following are the current users\n `who`" echo "\nCurrent date is : `date`\n" set `date` echo "calendar of current year is \n `cal $6`" Program to shell script to check the present working directory, then make a directory on that location, then create 3 files in it and give executable permissions to them. echo "present working directory is `pwd`\n" if [ -d newdir ] then echo "newdir already exists" else mkdir newdir echo "newdir created" fi cd newdir echo "Current working director is `pwd`" for file in file1 file2 file3 do echo $file|tee $file done 3. shell script to take input for name, course and university and print it in the format – I [name] study in [course] from [university]. if [ $# -ne 3 ] then echo "Number of arguments required are three\n" echo "Enter your name\n" read name echo "Enter the course\n" read course echo "Enter the university\n" read university else name=$1 course=$2 university=$3 fi
echo "I $name study in $course from $university" 4. shell script which creates one hard link and two soft links of a file. echo "Enter name of the file\n" read file if [ -e $file ] then echo "Creating hard link\n" ln $file $file"link1" echo "Creating two soft links\n" ln -s $file $file"link2" ln -s $file $file"link3" ls -li else echo "file not present" fi 5. #shell script that accepts two filenames and your script should copy file1 to file2 and display both the files. (take input from keyboard and command line both). if [ $# -ne 2 ] then echo "Enter name of first file: " read file1 echo "Enter name of second file: " read file2 else file1=$1 file2=$2 fi cp $file1 $file2 8. #shell script that should accept the output of the current date and print the output in the form – 2009 Oct Tue echo "Current date is `date`" set `date` echo "$6 $2 $1" 9. #shell script that will display the files of the current directory and number of files through positional arguments
if [ -d $1 ] then echo "$1 is a directory" else echo "$1 is not present" fi cd $1 echo "Following are the files present in $1 :\n" ls -l echo "Number of file are `ls|wc -l`" 10. #shell script using if, copy source to target, if it copies then echo copy process successful else echo unable to copy echo "Enter the source file: " read sname echo "Enter the target program: " read tname cp $sname $tname 2> error if [ $? -eq 0 ] then echo "Copy successful" else echo "Unable to copy" fi 11. #shell script that will accept a number and then check if number is less than 10. Display the results accordingly echo "Enter the number: " read num if [ num -lt 10 ] then echo "$num is less than 10" else echo "$num is greater or equal to 10" fi 12. #If Basic Salary=1500, then hra=500, da=98% of Basic. # If the employee’s basic salary is entered through the keyboard, then write a shell script to find the employee’s gross salary. echo "Enter your basic salary" read salary if [ $salary -lt 1500 ] then hra=`expr $salary \* 10 / 100` da=`expr $salary \* 90 / 100` else
hra=500 da=`expr $salary \* 98 / 100` fi gross=`expr $salary + $hra + $da` echo "Gross salary is: $gross" 13. #shell script where distance between two cities is entered from keyboard in kms and then print that distance in meters and cms. echo "Enter the distance b/n two cities in kms: " read distance echo "Distance in mtrs is : `expr $distance \* 1000`\n" echo "Distance in cms is : `expr $distance \* 100000`" 14. #shell script that perform all the mathematical operations on two numbers. (take input from keyboard and command line both). if [ $# -ne 2 ] then echo "Enter the two numbers: " read num1 num2 else num1=$1 num2=$2 fi echo "Addition of numbers is `expr $num1 + $num2`" echo "Subtraction of numbers is `expr $num1 - $num2`" echo "Multiplication of numbers is `expr $num1 \* $num2`" echo "Division of numbers is `expr $num1 / $num2`" 15. #shell script which check whether the number is even or odd. echo "Enter the number: " read num if [ `expr $num % 2` -eq 0 ] then echo "$num is even number" else echo "$num is odd number" fi 16. #shell script which displays the result of student as follows: # If %age is less then 40 then FAIL. # If %age is 40-50 then Third Division. # If %age is 50-60 then Second Division. # If %age is 60-75 then First Division,
#
If above 75 then Distinction.
echo "Enter the marks of the student: " read marks if [ $marks -gt 75 ] then echo "Distinction" elif [ $marks -gt 59 ] then echo "First Division" elif [ $marks -gt 49 ] then echo "Second division" elif [ $marks -gt 39 ] then echo "Third division" else echo "Fail" fi 17.. #shell script which accepts the age of a person and checks: # If age is less than 10 yrs then is not allowed. # If age is 10-18 then allowed to swim in junior pool. # If age >18 then senior pool. # echo "Enter the age " read age if [ $age -gt 18 ] then echo "Senior pool allowed" elif [ $age -gt 9 ] then echo "Junior pool allowed" else echo "Not allowed" fi
18. #shell script that accepts a year and check whether it is a leap year or not. echo "Enter the year " read year if [ `expr $year % 400 ` -eq 0 ] then echo "$year is a leap year" elif [ `expr $year % 4` -eq 0 ] && [ `expr $year % 100` -ne 0 ] then echo "$year is a leap year" else echo "$year is not a leap year" fi
19. #shell script which calculate area and perimeter of a rectangle when length and breadth are entered into through keyboard and through command line if [ $# -eq 2 ] then length=$1 breadth=$2 else echo "Enter length and breadth" read length breadth fi area=`expr $length \* $breadth` perimeter=`expr 2 \* $length + 2 \* $breadth` echo "Area is $area\n" echo "Perimeter is $perimeter" 20. #shell script which calculate sum of digits of a five-digit number and also checks whether the number is a palindrome or not. echo "Enter the five digit number" read num num1=$num sum=0 while [ $num -gt 0 ] do n=`expr $num % 10` sum=`expr $sum + $n` num=`expr $num / 10` rev=$rev$n done echo "Sum of digits is $sum" echo "Reverse of number is $rev" if [ $rev -eq $num1 ] then echo "$num1 is a palindrome" else echo "$num1 is not a palindrome" fi 21. #Make a menu driven shell script with # Option 1: Creates a dir d1/d2/d3 where all dir doesn’t exist. # Option 2: Display some file f4 in the current dir. # Option 3: Check all the users who have logged in. #
choice="4" echo "Enter choice 1 to create a directory\n Enter choice 2 to display file in current directory\n Enter choice 3 to check all logged in users\n Enter choice 4 to exit" read choice while [ $choice != "4" ] do case $choice in 1) echo "Enter the directory to be created d1/d2/d3" read dir case $dir in d1|d2|d3) if [ -d $dir ] then echo "Directory $dir already present" else mkdir $dir fi ;; esac ;; 2) echo "Enter the file to be displayed" read filename if [ -e $filename ] then echo "File contents are \n`cat $filename`" else echo "File not present in directory" fi ;; 3) echo "Current users are `who`" ;; # *) $choice="y";; esac echo "Enter your choice " read choice done 22. #menu driven shell script with #Option 1: Check read permission. If it is there then display the file, if not then give the read permission. #Option 2: Check write permission. If it is there then append the file, if not then give the write permission. #Option 3: Check execute permission. If it is there then display the file, if not then say it is a text file. echo "Enter the filename : " read filename if [ ! -e $filename ] then echo "$filename not present" else echo "Enter choice 1 to check for read permission\n
Enter choice 2 to check write permission\n Enter choice 3 to check execute permission\n Enter choice 4 to exit" read choice while [ $choice != "4" ] do case $choice in 1) if [ -r $filename ] then echo "$filename is readable" else echo "$filename not readable" fi ;; 2) if [ -w $filename ] then echo "$filename is writable" echo "Enter text to be appended. To stop press CTRL + d " cat >> $filename else echo "$filename not writable" fi ;; 3) if [ -x $filename ] then echo "$filename is executable" else echo "$filename is not executable and a text file" fi ;; esac echo "Enter your choice " read choice donefi 23. #shell script to check whether a file entered from command line is an ordinary file or directory. If it is a dir, then check the required permissions and create a dir in it, and in that sub-dir, create a file. If it is a file, then append the file after checking the permissions. echo "Enter the filename : " read filename if [ -f $filename ] then echo "File is a regular file ." if [ -w $filename ] then echo "Enter text to be appended. Press CTRL + d to exit" cat >> $filename else echo "File not editable" fi else
if [ -d $filename ] then echo "File is a directory" if [ -w $filename ] then echo "Enter new file to be created in $filename" read f cd $filename touch $f else echo "Directory does not have write permission" fi fi fi 24. #shell script that uses for loop to display the names of files in the current directory which are only directory files for file in * do if [ -d $file ] then echo $file fi done 25. #shell script to check the count of total number of arguments entered if number of arguments is less than or equal to 5, then echo your name else echo error message, ie, invalid number of arguments. (Use while loop) echo $# while [ $# -gt 0 ] do echo "Name" shift done
26 #Write a shell script where day(In Capitals) is entered in the command line. Check the string and display: #a) If it is MON display Unix test, # If it is TUE display Computer Organization test, # If it is WED display System Programming test, # If it is THUR display Maths test, # If it is FRI display Aptitude test, # then exit. echo "Enter the day in capital letters, only first three letters : " read day case $day in MON) echo "Unix test" ;; TUE) echo "COA test" ;; WED) echo "System Programming test" ;; THUR) echo "Maths test" ;; FRI) echo "Aptitude test" ;; *) exit esac 26.b) #Write a shell script where day(In Capitals) is entered in the command line. Check the string and display: #b) If it is MON | mon display Unix test, # If it is TUE | tue display Computer Organization test, # If it is WED | wed display System Programming test, # If it is THUR | thur display Maths test, # If it is FRI | fri display Aptitude test, # then exit. # echo "Enter the day in capital letters, only first three letters : " read day case $day in MON|mon) echo "Unix test" ;; TUE|tue) echo "COA test" ;; WED|wed) echo "System Programming test" ;; THUR|thur) echo "Maths test" ;; FRI|fri) echo "Aptitude test" ;; *) exit esac 27) #Write table of 2, and 5 through two ways: #1) Take the number inside the script. #2) Through positional parameter. if [ $# -eq 1 ]
then num=$1 else echo "Enter the number 2 or 5 : " read num fi i=1 while [ $i -lt 11 ] do echo "$num * $i = `expr $num \* $i`" i=`expr $i + 1` done 28) #Write a shell script which accepts number of arguments. If number of arguments is equal to 5 then only it should displays your name five times (You can try this, first, by displaying your name once)? echo "Enter number of arguments : " read args while [ $args -gt 0 ] do echo "name" args=`expr $args - 1` done 29) #Write a shell script to search whether the username passed on command line has logged in or not, after each minute? If user has logged in display the message and exit otherwise display a message “ Not logged in Yet” (i.e. The process will go in background). who > userlist grep "$1" userlist while [ $? -ne 0 ] do sleep 60 echo "$1 has not logged in" who > userlist grep "$1" userlist done echo "$1 logged in" 30) #Write a shell script which scans your current directory and the files which end with .doc, rename those files to .txt. Suppose: the current directory named UNIX contains the file unix01doc then it should be renamed to unix01txt, similarly for all the files of that directory?
for file in *.doc do mv $file `basename $file doc`"txt" done 31) #Write a shell script that scans the current directory (or any path ) and the files ending with doc are to be moved to a new directory name "word assignment" and the file ending with .c (its dot c) are to be moved to a new directory named "c assignments"? for file in *.doc do flag=1 if [ $flag -eq 1 ]; then mkdir "word_assignment" flag=0 fi mv $file "word_assignment" done for file in *.c do flag=1 if [ $flag -eq 1 ]; then mkdir "c_assignment" flag=0 fi mv $file "c_assignment" done 32) #Write a shell script that scans the current directory & renames all the regular files & not the directory files to the name of the log name i.e. filename. student” ? cd "c_assignment" for file in * do if [ -d $file ] then echo "$file is a directory" elif [ -f $file ] && [ ! -d $file ] then mv $file $file".student" fi done
33) #Write a shell script that takes pattern and filename as command line arguments and displays the result appropriately i.e. pattern found/ pattern not found. pattern=$1 fname=$2 grep "$1" $2 if [ $? -eq 0 ] then echo "$pattern found in $fname" else echo "$pattern not found in $fname" fi 34) #Write a shell script where you check whether the name #a) begins with ‘A’ and ends with ‘t’. #b) begins with ‘S’ and ends with ‘T’. #c) any word with fixed length of four characters. # echo "Enter name\n" read name length=`expr "$name" : '.*'` echo "length is $length" case "$name" in A*t) echo "name starts with A and ends with t" ;; S*T) echo "name starts with S and ends with T" ;; *) if [ length -eq 4 ] then echo "length of $name is 4" fi esac 35) #Write a shell script where you can accept a word and echo the message whether the name begins with a vowel, consonant or a digit? echo "Enter name\n" read name case "$name" in [AaEeIiOoUu]*) echo "$name begins with a vowel" ;; [0-9]*) echo "$name begins with a digit" ;; [b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]*) echo "$name begins witha consonant" ;; Esac
36) #Write a shell script that computes the factorial of a given number. echo "Enter the number " read num prod=1 while [ $num -gt 0 ] do prod=` expr $prod \* $num ` num=` expr $num - 1 ` done echo "Factorial of $num is $prod"
View more...
Comments