1 / 55

Program1a

Program1a Write a non-recursive shell script which accepts any number of arguments and prints them in the reverse order (For example, if the script is named args , then executing args A B C should produce C B A on the standard output). if [ $# - eq 0 ] then echo "No arguments" exit fi

Download Presentation

Program1a

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Program1a Write a non-recursive shell script which accepts any number of arguments and prints them in the reverse order (For example, if the script is named args, then executing args A B C should produce C B A on the standard output). if [ $# -eq 0 ] then echo "No arguments" exit fi for i in $* do y=$i" "$y done echo $y

  2. Output $ sh 1a.sh 1 2 3 3 2 1

  3. Program1b Write a shell script that accepts two file names as arguments, checks if the permissions for these files are identical and if the permissions are identical, output common permissions and otherwise output each file name followed by its permissions. if [ $# -ne 2 ] then echo “Please enter two filenames as arguments" elif [ ! -e $1 -o ! -e $2 ] then echo "FIle does not exist" else p1=`ls -l $1 | cut –c 2-10` p2=`ls -l $2 | cut –c 2-10` if [ $p1 == $p2 ] then echo "File permissions are equal & is $p1" else echo "File permissions are not equal" echo “$1: $p1" echo “$2: $p2" fi fi

  4. Output $ sh 1b.sh a.txt b.txt File permissions are equal & is rwxrwxrwx

  5. Program2a Write a shell script that takes a valid directory name as an argument and recursively descends all the subdirectories, finds the maximum length of any file in that hierarchy and writes this maximum value to the standard output. if [ $# -ne 1 ] then echo “Please enter a Single Directory as argument" exit fi if [ ! -e $1 ] then echo "The given Directory does not exist" exit fi echo "The file with maximum length in $1 directory is" ls -lR $1 | grep “^-” | tr -s “ “ | cut -d " " -f 5,9 | sort –n | tail -n 1

  6. Output $ sh 2a.sh dir1 The file with maximum length in shell_script directory is 252 sort.c

  7. Program2b Write a shell script that accepts a path name and creates all the components in that path name as directories. For example, if the script is named mpc, then the command mpc a/b/c/d should create directories a, a/b, a/b/c, a/b/c/d. if [ $# -ne 1 ] then echo "Invalid number of arguments" else mkdir -p $1 fi

  8. Output $ sh 2b.sh a/b/c/d $ ls –R a a: b a/b: c a/b/c: d a/b/c/d:

  9. Program3a Write a shell script which accepts valid log-in names as arguments and prints their corresponding home directories, if no arguments are specified, print a suitable error message. clear if [ $# -eq 0 ] then echo "No command line argument passed" exit fi while [ $1 ] do cat /etc/passwd | cut -d ":" -f1 | grep "^$1" > temp ck=`cat temp` if [ "$ck" != "$1" ] then echo "ERROR:$1 is an invalid login name" else echo "Home Directory for $1 is" echo `cat /etc/passwd | grep "^$1" | cut -d ":" -f6` fi shift done

  10. Output $ sh 3a.sh student Home Directory for student is /home/student

  11. Program3b Write a shell script to implement terminal locking (similar to the lock command). It should prompt the user for a password. After accepting the password entered by the user, it must prompt again for the matching password as confirmation and if match occurs, it must lock the keyword until a matching password is entered again by the user. Note that the script must be written to disregard BREAK, control-D. No time limit need be implemented for the lock duration. stty –echo while true do clear echo "Enter the password" read pass1 echo "Re enter the password" read pass2 if [ “$pass1” = “$pass2” ] then echo "Terminal locked" echo "To unlock enter the password" pass1="" until [ "$pass1" = "$pass2" ] do read pass1 done echo "Terminal unlocked" stty echo exit else echo "Password mismatch retype it" fi done

  12. Output $ sh 3b.sh Enter the password Re enter the password Terminal locked To unlock enter the password Terminal unlocked

  13. Program4a Create a script file called file-properties that reads a file name entered and outputs it properties. echo "Enter a file name" read file if [ -f $file ] then set --`ls -l $file` echo “File permission : $1" echo “Number of links : $2" echo "User name : $3" echo “Group name : $4" echo “Filesize : $5 bytes" echo "Date of modification : $6 $7" echo "Time of modification : $8" echo "Name of file : $9" else echo "File does not exist" fi

  14. Output $ sh 4a.sh Enter a file name a.txt File permission : -rw-rw-r— Number of links : 1 User name : student Group name: mca File size : 24 bytes Date of modification : Dec 12 Time of modification : 12:45 Name of file : a.txt

  15. Program4b • Write a shell script that accepts one or more filenames as arguments and convert all of them to uppercase, provided they exist in current directory. • if [ $# -eq 0 ] • then • echo "No arguments" • exit • fi • for f in $* • do • if [ -e $f ] • then • cat $f | tr "[a-z]" "[A-Z]“ > tmp • mvtmp $f • else • echo "File $f does not exist" • fi • done

  16. Output $ cat a.txt Hi how are you? $ sh 4b.sh a.txt $ cat a.txt HI HOW ARE YOU?

  17. Program5a • Write a shell script that displays all the links to a file specified as the first argument to the script. The second argument, which is optional, can be used to specify in which the search is to begin. If this second argument is not present, the search is to begin in current working directory. In either case, the starting directory as well as all its subdirectories at all levels must be searched. The script need not include any error checking. • if [ $# -eq 0 ] • then • echo "No arguments: Enter either one(file) or two arguments(file and directory)" • exit • elif [ $# -eq 1 ] • then • dir="." • else • dir="$2" • fi • if [ ! -f $1 ] • then • echo "$1 is not a regular file" • exit • fi • ino=`ls -i "$1" | cut -d " " -f1` • echo "Hard Links of file $1 in directory $dir are" • find "$dir" -inum $ino -print • echo "Soft Links of file $1 in directory $dir are" • find "$dir" -lname $1 -print

  18. Output $ sh 5a.sh abc.txt Hard Links of file abc.txt in directory . is ./abc2.txt ./abc.txt Soft Links of file abc.txt in directory . Is ./slink.txt

  19. Program5b • Write a shell script that accepts a filename as argument and displays its creation time if file exists and if does not, send output error message. • if [ $# -eq 0 ] • then • echo "No arguments: Please enter exactly one argument" • exit • fi • if [ -f $1 ] • then • file=$1 • set -- `ls -l $1` • echo "Creation time of file $file : $6 $7 $8" • else • echo "File $1 does not exist" • fi

  20. Output $ sh 5b.sh abc.txt Creation time of file abc.txt: Dec 15 12:45

  21. Program6a • Write a shell script to display the calendar for current month with current date replaced by * or ** depending on whether the date has one digit or two digits. • set `date` • if [ $3 -le 9 ] • then • n=`cal | tail -n +3 | grep -n "$3"| cut -d ":" -f1 | head -n 1` • n=`expr $n + 2` • cal | sed "$n s/$3/*/" • else • cal | sed "s/$3/**/" • fi

  22. Output $ sh 6a.sh December 2011 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ** 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

  23. Program6b • Write a shell script to find the smallest of three numbers that are read from the keyboard. • echo "Enter the three numbers" • read a b c • if [ $a -le $b -a $a -le $c ] • then • echo "$a is Smallest" • elif [ $b -le $a -a $b -le $c ] • then • echo "$b is Smallest" • else • echo "$c is Smallest" • fi

  24. Output $ sh 6b.sh Enter the three numbers 3 4 3 is Smallest

  25. Program7a • Write a shell script using expr command to read in a string and display a suitable message if it does not have at least 10 characters. • echo "Enter a String" • read str • c=`expr "$str" : '.*'` • if [ $c -lt 10 ] • then • echo "The String $str has less than 10 characters" • else • echo "The String $str has $c characters" • fi

  26. Output $ sh 7a.sh Enter a String Shell Script The String Shell Script has 12 characters

  27. Program7b • Write a shell script to compute the sum of numbers passed to its argument on command line and display the result. • if [ $# -eq 0 ] • then • echo "No arguments passed" • exit • fi • sum=0 • for i in $* • do • sum=`expr $sum + $i` • done • echo "Sum of Numbers is : $sum"

  28. Output $ sh 7b.sh 5 7 3 Sum of numbers is : 15

  29. Program8a • Write a shell script that compute gross salary of an employee, according to rule given below. • If basic salary<15000, then HRA=10% of basic & DA=90% of basic. • If basic salary>=15000, then HRA=50% of basic & DA=98% of basic. • echo -e"Enter basic Salary: \c" • read basic • if [ $basic -lt 15000 ] • then • hra=`echo "scale=2; 10 * $basic / 100" | bc` • da=`echo "scale=2; 90 * $basic / 100" | bc` • else • hra=`echo "scale=2; 50 * $basic / 100" | bc` • da=`echo "scale=2; 98 * $basic / 100" | bc` • fi • gs=`echo "scale=2; $basic + $hra + $da" | bc` • echo "Basic Salary : Rs. $basic" • echo "HRA : Rs. $hra" • echo "DA : Rs. $da" • echo "Gross Salary : Rs. $gs"

  30. Output $ sh 8a.sh Enter basic Salary: 10000 Basic Salary : 10000 HRA : 1000.00 DA : 9000.00 Gross Salary: 20000.00

  31. Program8b • Write a shell script that deletes all lines containing a specific word in one or more files supplied as arguments to it. • if [ $# -eq 0 ] • then • echo "Please enter one or more filenames as argument" • exit • fi • echo -e "Enter the word to be searched in files: \c" • read word • for file in $* • do • sed "/$word/d" $file | tee tmp • mvtmp $file • done

  32. Output $ cat employee UNIX is a multiuser OS It is a Command-oriented OS It has the facility of pattern matching Shell is a part of UNIX system $ sh 8a.sh employee Enter the word to be searched in files: UNIX It is a Command-oriented OS It has the facility of pattern matching $cat employee It is a Command-oriented OS It has the facility of pattern matching

  33. Program9a • Write a shell script that when gets executed displays the message either “Good Morning” or “Good Afternoon” or “Good Evening” depending upon time at which the user logs in. • echo "Greetings of the day" • echo `date` • h=`date | cut -c 12-13` • if [ $h -ge 0 -a $h -lt 12 ] • then • echo "Good Morning" • elif [ $h -ge 12 -a $h -lt 18 ] • then • echo "Good Afternoon" • elif [ $h -ge 18 -a $h -lt 20 ] • then • echo "Good Evening" • else • echo "Good Night" • fi

  34. Output $ sh 9a.sh Greetings of the day Thu Dec 15 12:45 IST 2011 Good Afternoon

  35. Program9b • Write a shell script that accept a list of filenames as its arguments, counts and reports occurrence of each word that is present in the first argument file on other argument files. • if [ $# -lt 2 ] • then • echo "Please enter at least two arguments" • exit • fi • str=`cat $1 | tr '\n' ' '` • shift • for file in $* • do • echo "File $file: " • for a in $str • do • echo "Word=$a", Count=`cat $file | tr ' \t' '\n\n' | grep -c "$a"` • done > newfile • cat newfile | sort -u • done

  36. Output $ cat a.txt UNIX is command oriented $ cat b.txt Introduction to UNIX It has numerous command $cat c.txt UNIX is multiuser OS, UNIX is simple $ sh 9b.sh a.txt b.txt c.txt File b.txt: Word=UNIX, Count=1 Word=command, Count=1 Word=is, Count=0 Word=oriented, Count=0 File c.txt: Word=UNIX, Count=2 Word=command, Count=0 Word=is, Count=2 Word=oriented, Count=0

  37. Program10a • Write a shell script that determines the period for which a specified user is working on system. • echo -e "Enter the user name : \c" • read usr • tusr=`who | tr -s " " | cut -d " " -f1 | grep "$usr" | head -1` • if [ "$tusr" = "$usr" ] • then • tm=`who | tr -s " " | grep "^$usr" | head -1 | cut -d " " -f4` • uhr=`echo $tm | cut -d ":" -f1` • umin=`echo $tm | cut -d ":" -f2` • shr=`date "+%H"` • smin=`date "+%M"` • if [ $smin -lt $umin ] • then • shr=`expr $shr - 1` • smin=`expr $smin + 60` • fi • h=`expr $shr - $uhr` • m=`expr $smin - $umin` • echo "User $usr logged in for $h hours $m minutes" • else • echo "User $usr has not logged in" • fi

  38. Output $ sh 10a.sh Enter the user name : kumar User kumar logged in for 2 hours 25 minutes

  39. Program10b • Write a shell script that reports the logging in of a specified user within one minute after he/she logs in. The script automatically terminates if specified user does not log in during a specified period of time. • echo "Enter the user name" • read usr • a=`date "+%M"` • b=`expr $a + 1` • while [ $b -gt `date "+%M"` ] • do • who | grep "^$usr" > /dev/null • if [ $? -eq 0 ] • then • echo "User $usr has logged in" • exit • fi • c=`date “+%M”` • if [ $c –eq 59 –a `date "+%M"` -eq 0 ] • then • exit • fi • done • echo "User $usr has not logged in within one minute"

  40. Output $ sh 10b.sh Enter the user name kumar User kumar has not logged in within one minute

  41. Program11a • Write a shell script that accepts two integers as its arguments and computes the value of first number raised to the power of second number. • if [ $# -ne 2 ] • then • echo "Invalid number of arguments" • exit • fi • pwr=`echo "scale=8; $1^$2" | bc` • echo "pow($1, $2) = $pwr"

  42. Output $ sh 11a.sh 3 4 pow(3, 4) = 81

  43. Program11b • Write a shell script that accepts the file name, starting and ending line numbers as arguments and displays all the lines between the given line numbers. • if [ $# -ne 3 ] • then • echo "Invalid number of arguments" • exit • fi • c=`cat $1 | wc -l` • if [ $2 -le 0 -o $3 -le 0 -o $2 -gt $3 -o $3 -gt $c ] • then • echo "Invalid Input" • exit • fi • sed -n "$2, $3 p" $1

  44. Output $ cat a.txt Introduction to UNIX UNIX is multiuser OS It is Simple UNIX is Command-Oriented It has Pattern-matching facility $ sh 11b.sh a.txt 2 4 UNIX is multiuser OS It is Simple UNIX is Command-Oriented

  45. Program12a • Write a shell script that folds long lines into 40 columns. Thus any line that exceeds 40 characters must be broken after 40th, a “\” is to be appended as the indication of folding and the processing is to be continued with the residue. The input is to be supplied through a text file created by the user. • echo -e "Enter the text file : \c" • read text • n=`cat $text | wc -l` • count=1 • printf "" > newfile • while [ $count -le $n ] • do • length=`sed -n "$count p" $text | wc -c` • length=`expr $length - 1` • part=`expr $length / 40` • i=1 • l=1 • r=40 • while [ $i -le $part ] • do • temp=`sed -n "$count p" $text | cut -c $l-$r` • echo $temp\\ >> newfile • l=`expr $l + 40` • r=`expr $r + 40` • i=`expr $i + 1` • done • sed -n "$count p" $text | cut -c $l- >> newfile • count=`expr $count + 1` • done

  46. Output $ cat a.txt Write a shell script that folds long lines into 40 columns. Thus any line that exceeds 40 characters must be broken after 40th, a “\” is to be appended as the indication of folding and the processing is to be continued with the residue. The input is to be supplied through a text file created by the user. $ sh 12a.sh Enter the text file : a.txt $ cat newfile Write a shell script that folds long lin\ es into 40 columns. Thus any line that exceeds 40 characters\ must be broken after 40th, a “\” is to \ be appended as the indication of folding a\ nd the processing is to be continued wit\ h the residue. The input is to be supplied through a te\ xt file created by the user.

  47. Program12b • Write an awk script that accepts date argument in the form of mm-dd-yy and displays it in the form of day, month and year. The script should check the validity of the argument and in the case of error, displays a suitable message. • BEGIN { • FS="-" • f=1 • printf "Enter the date in (mm-dd-yy) format: " • getline < "/dev/tty" • if ( $3 % 4 != 0 && $1 == 2 && $2 > 28 ) • f=0 • else if ( $3 % 4 == 0 && $1 == 2 && $2 > 29 ) • f=0 • else if (( $1 == 1 || $1 == 3 || $1 == 5 || $1 == 7 || $1 == 8 || $1 == 10 || $1 == 12 ) && ( $2 > 31)) • f=0 • else if (( $1 == 4 || $1 == 6 || $1 == 9 || $1 == 11 ) && ( $2 > 30)) • f=0 • else if ( $1 < 1 || $2 < 1 || $3 < 1 || $1 > 12 || $3 > 99 ) • f=0 • if ( f == 0 ) • printf "You have entered an Invalid date\n" • else • printf "Date : %d\nMonth : %d\nYear : %d\n", $2, $1, $3 • }

  48. Output $ awk -f 12b.awk Enter the date in (mm-dd-yy) format: 12-15-11 Date: 15 Month: 12 Year: 11

  49. Program13a • Write an awk script to delete duplicated line from a text file. The order of the original Lines must remain unchanged. • BEGIN { • i=1 • } • { • flag=1 • for( j=1; j<=NR; j++ ) • { • if ( i != j && x[j] == $0 && flag ) • flag=0 • } • if ( flag ) • { • x[i]=$0 • printf "%s\n", x[i] > FILENAME • } • i++ • }

  50. Output $ cat file.txt Introduction to UNIX UNIX is multiuser OS It is Simple UNIX is multiuser OS UNIX is Command-Oriented It is Simple It has Pattern-matching facility $ awk -f 13a.awk file.txt $ cat file.txt Introduction to UNIX UNIX is multiuser OS It is Simple UNIX is Command-Oriented It has Pattern-matching facility

More Related