Anda di halaman 1dari 28

UNIX Programming Laboratory

Subject Code: 10MCA17

Unix Programming Laboratory

10MCA17

1. a. 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 rags, then executing args A B C should produce C B A on the standard output).

if [ $# -eq 0 ] then echo "Argument not found" exit else i=`echo $*|wc -w` while [ $i -gt 0 ] do s=`echo $*|cut -d " " -f $i` temp=`echo $temp $s` i=`expr $i - 1` done echo "Total number of arguments : $#" echo "Arguments list : $*" echo "Reversed list : $temp" fi

Mr. Shylesh B C

Unix Programming Laboratory

10MCA17

b. 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 [ $# -eq 0 ] then echo "No arguments" exit elif [ $# -lt 2 ] then echo "Only one argument" exit else f1=`ls -l $1|cut -c '2-10'` f2=`ls -l $2|cut -c '2-10'` if [ "$f1" = "$f2" ] then echo "File permission are identical" echo "The permissions are : $f1" else echo "File permission are not identical" echo "The permission of first file is f1 : $f1" echo "The permission of the second file is f2 : $f2" fi fi

Mr. Shylesh B C

Unix Programming Laboratory

10MCA17

2. a. Write a shell script that takes a valid directory name as an argument and recursively descend all the subdirectories, finds the maximum length of any file in that hierarchy and writes this maximum value to the standard output.

echo "Enter Directory name" read dir if [ ! -d $dir ] then echo "Invalid directory" exit fi large=0 for file in `find $dir -type f` do size=`stat -c %s $file` echo "size of $file is $size" if [ $size -gt $large ] then large=$size fi done echo "File with Maximum size is $large"

Mr. Shylesh B C

Unix Programming Laboratory

10MCA17

b. 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 [ $# -eq 0 ] then echo "Argument not found" exit else temp=$IFS IFS=/ c=0 for i in $* do if [ -d $i ] then cd $i else mkdir $i c=`expr $c + 1` cd $i fi done IFS=$temp echo "$c directories created" fi

Mr. Shylesh B C

Unix Programming Laboratory

10MCA17

3. a. 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.

if [ $# -eq 0 ] then echo "No arguments" else for name in $* do if grep $name /etc/passwd >/dev/null then echo "Login name:$name" hdir=`grep $name /etc/passwd | cut -d":" -f6` echo "Home Directory :$hdir" else echo "$name is not valid Login name" fi done fi

Mr. Shylesh B C

Unix Programming Laboratory

10MCA17

b. Write 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 echo "Enter password : " read pass1 echo "Confirm password :" read pass2 if [ "$pass1" = "$pass2" ] then echo "Terminal is locked" trap 1 2 15 while true do echo "Enter password" read pass3 if [ "$pass3" = "$pass2" ] then echo "Terminal Unlocked" stty echo exit else echo "Try again" fi done else echo "password do not match" stty echo fi

Mr. Shylesh B C

Unix Programming Laboratory


4. a. 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 [ ! -e $file ] then echo "File does not exit." exit else ftype=`ls -l $file|cut -c 1` fper=`ls -l $file|cut -c 2-10` fowner=`ls -l $file|tr -s |cut -d " " -f3` fsize=`ls -l $file|tr -s |cut -d " " -f5` fdate=`ls -l $file|tr -s |cut -d " " -f6` ftime=`ls -l $file|tr -s |cut -d " " -f7` fname=`ls -l $file|tr -s |cut -d " " -f8` fi echo "The file type is : $ftype" echo "The file permission is : $fper" echo "The file owner is : $fowner" echo "The file size is : $fsize" echo "The file date is : $fdate" echo "The file time is : $ftime" echo "The file name is : $fname"

10MCA17

Mr. Shylesh B C

Unix Programming Laboratory

10MCA17

b. Write a shell script that accept one or more filenames as argument and convert all of them to uppercase, provided they exist in current directory. if [ $# -eq 0 ] then echo "No Arguments" exit else for file in $* do if [ -e $file ] then fname=`echo $file|tr [a-z] [A-Z]` echo "The $file is converted to $fname " else echo "The directory $file does not exit" fi done fi

Mr. Shylesh B C

Unix Programming Laboratory

10MCA17

5. a. 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" exit fi if [ $# -eq 2 ] then dir=$2 else dir=`pwd` fi inode=`stat -c %i $1` count=0 for link in `find $dir -inum $inode` do echo $link count=`expr $count + 1` done if [ $count -eq 0 ] then echo "$1 has no link in the directory $dir" else echo "$1 has $count links in the directory $dir" fi

Mr. Shylesh B C

10

Unix Programming Laboratory

10MCA17

b. Write a shell script that accepts as filename as argument and display its creation time if file exist and if it does not send output error message. if [ $# -eq 0 ] then echo "No Arguments" exit else for file in $* do if [ -f $file ] then fdate=`ls -l $file|tr -s |cut -d " " -f6` ftime=`ls -l $file|tr -s |cut -d " " -f7` else echo "$file does not exit" fi done fi echo "File modified on $fdate at $ftime"

Mr. Shylesh B C

11

Unix Programming Laboratory

10MCA17

6. a. 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. dt=`date +%d` cal > f3.lst if [ $dt -lt 10 ] then dt=`echo $dt|cut -c 2` ln=`sed -n 3,$p f3.lst | nl | grep "$dt" | head -1 | cut -f1` ln=`expr $ln + 2` sed $ln's/'$dt'/*/' f3.lst else sed 's/'$dt'/**/' f3.lst fi

Mr. Shylesh B C

12

Unix Programming Laboratory


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

10MCA17

Mr. Shylesh B C

13

Unix Programming Laboratory

10MCA17

7. a. 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 the string" read str if [ -z str ] then echo "Null character" else len=`expr "$str" : '.*'` if [ $len -ge 10 ] then echo "$str has $len character" else echo "$str has less than 10 character" fi fi

Mr. Shylesh B C

14

Unix Programming Laboratory

10MCA17

b. Write a shell script to compute the sum of number passed to it as argument on command line and display the result. if [ $# -eq 0 ] then echo "No Arguments" exit else echo "To compute the sum of : $*" num=$1 sum=0 while [ $num -gt 0 ] do rem=`expr $num % 10` num=`expr $num / 10` sum=`expr $rem + $sum` done echo "The result is $sum" fi

Mr. Shylesh B C

15

Unix Programming Laboratory


8. a. Write a shell script that compute gross salary of an employee, accordingly to rule given below. If basic salary is < 15000 then HRA=10% of basic 7 DA=90% of basic. If basic salary is >=15000 then HRA=500 of basic & DA=98% of basic. echo "Enter the basic salary :" read basic if [ $basic -lt 15000 ] then hra=`expr $basic \* 10 / 100` da=`expr $basic \* 90 / 100` else hra=`expr $basic \* 50 / 100` da=`expr $basic \* 98 / 100` fi gross=`expr $basic + $hra + $da` echo "--------------------------------" echo " SALARY " echo "--------------------------------" echo "Basic salary is $basic" echo "HRA is $hra" echo "DA is $da" echo "--------------------------------" echo "Gross salary is $gross" echo "--------------------------------"

10MCA17

Mr. Shylesh B C

16

Unix Programming Laboratory

10MCA17

b. Write a shell script that delete all lines containing a specific word in one or more file supplied as argument to it. if [ $# -eq 0 ] then echo "No arguments" else pattern=$1 shift for fname in $* do if [ -f $fname ] then echo "Deleting $pattern from $fname" sed '/'$pattern'/d' $fname else echo "$fname not found" fi done fi

Mr. Shylesh B C

17

Unix Programming Laboratory

10MCA17

9. a. Write a shell script that gets executed displays the message either Good Morning or Good Afternoon or Good Evening depending upon time at which the user logs in. time=`who am I | tr -s ' ' | cut -d " " -f 4 | cut -c 1,2` if [ $time -le 12 ] then echo "Good Morning $LOGNAME" elif [ $time -gt 12 -a $time -lt 16 ] then echo "Good Afternoon $LOGNAME" else echo "Good Evening $LOGNAME" fi

Mr. Shylesh B C

18

Unix Programming Laboratory

10MCA17

b. Write a shell script that accept a list of filenames as its argument, count and report occurrence of each word that is present in the first argument file on other argument files. if [ $# -eq 0 ] then echo "No Arguments" elif [ $# -eq 1 ] then echo "Only one Arguments" else pat=$1 if [ ! -e $1 ] then echo "$1 does not exist" exit fi shift for file in $* do if [ -e $fie ] then echo $file for pattern in `cat $pat` do echo "$pattern occurs `grep -c "$pattern" $file` times" done else echo "$file does not exist" fi done fi

Mr. Shylesh B C

19

Unix Programming Laboratory


10. a. Write a shell script that determine the period for which a specified user is working on system. if [ $# -eq 0 ] then echo "No Arguments" exit fi if who | grep $1 then uhtime=`who | grep $1 tr -s ' ' | cut -d " " -f4 | cut -c 1,2` umtime=`who | grep $1 tr -s ' ' | cut -d " " -f4 | cut -c 4,5` hh=`$uhtime | cut -d " " -f1` mm=`$umtime | cut -d " " -f1` shtime=`date +%H` smtime=`date +%M` timeh=`expr $shtime $hh` timem=`expr $smtime $mm` echo "System time is $shtime hr and $smtime min" echo "Time in which is working $timeh hr and $timem min" else echo "Not Valid login name" fi

10MCA17

Mr. Shylesh B C

20

Unix Programming Laboratory

10MCA17

b. Write a shell script that reports the logging in of a specified user within one minute after he/she log in. The script automatically terminate if specified user does not log in during a specified period of time. interval=5 name=$1 who | awk '{printf $1}' | grep "$name" > /dev/null if test $? = 0 then loggedin=true echo "$name logged in" else loggedin=false echo "$name not logged in" fi sleep $interval while true do who | awk '{printf $1}' | grep "$name" > /dev/null if test $? = 0 then if loggedin=false then loggedin=true echo "$name logged in" exit fi else if loggedin=true then loggedin=false echo "$name not logged in" exit fi fi done

Mr. Shylesh B C

21

Unix Programming Laboratory

10MCA17

11. a. Write a shell script that accepts two integers as its argument and compute the value of first number raised to the power of second number. x=$1 y=$2 z=$x i=1 if [ $# -eq 0 ] then echo "No Arguments" else while [ $i -lt $y ] do z=`expr $z \* $x` i=`expr $i + 1` done echo "Value of 1st number raised to the power of 2nd number $x^$y : $z" fi

Mr. Shylesh B C

22

Unix Programming Laboratory

10MCA17

b. Write a shell script that accept the file name, starting and ending line number as an argument and display all the lines between the given line number. if [ $# -eq 0 ] then echo "No argument" elif [ $# -eq 1 ] then echo "Only one argument" elif [ $# -eq 2 ] then echo "only two argument" else if [ ! -e $1 ] then echo "File does not exist" else sed -n $2','$3'p' $1 fi fi

Mr. Shylesh B C

23

Unix Programming Laboratory

10MCA17

12. a. 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. # for the purpose of easy execution we have taken limit as 10 not as 40 i=1 while [ $i le `wc -l < temp` ] do x=`tail +$i temp | head -1` l=`expr "$x" : ".*"` if [ $l le 10 ] #if the length of the line <=10 then send directly to output file then echo $x >> temp1 else while [ `expr "$x" : ".*" ` -ne 0 ] do y=`echo $x | cut -c 1-10` echo $y "\\" >> temp1 x=`echo "$x" | cut -c 10-` done fi i=`expr $i + 1` done

Mr. Shylesh B C

24

Unix Programming Laboratory

10MCA17

b. Write an awk script that accepts date argument in the form of mm-dd-yy and displays it in the form if day, month, and year. The script should check the validity of the argument and in the case of error, display a suitable message.

Mr. Shylesh B C

25

Unix Programming Laboratory

10MCA17

13. a. Write an awk script to delete duplicated line from a text file. The order of the original lines must remain unchanged.

Mr. Shylesh B C

26

Unix Programming Laboratory

10MCA17

b. Write an awk script to find out total number of books sold in each discipline as well as total book sold using associate array down table as given below. i. Electrical 34 , ii. Mechanical 67, iii. Electrical 80, iv. Computer Science 43, v. Mechanical 65 , vi. Civil 198 vii. Computer Science 64

Mr. Shylesh B C

27

Unix Programming Laboratory


14. Write an awk script to compute gross salary of an employee accordingly to rule given below. If basic salary is < 10000 then HRA=15% of basic & DA=45% of basic. If basic salary is >=10000 then HRA=20% of basic & DA=50% of basic.

10MCA17

Mr. Shylesh B C

28

Anda mungkin juga menyukai