USN: SUBJECTCODE:13MCA17



1a. Write a shell script that takes a valid directory name as an argument and recursively descend all the sub-directories, finds the maximum length of any file in that hierarchy and writes this maximum value to the standard outputSOURCE CODEecho "Enter directory name"read dir1if [ ! -d $dir1 ];thenecho "Directory not found"exitfilarge=0;for file1 in `find $dir1 -type f`dosize1=`stat -c %s $file1`echo "size of the file $file1 is $size1"if [ $size1 -gt $large ];thenlarge=$size1lar_file=$file1fidoneecho "Largest file is:$lar_file"echo "And its size is:$large"Output 1:[reddy@localhost ~]$ sh 1a.shenter the valid directory name:ram enter a valid directory name!!!!Output 2:[reddy@localhost ~]$ sh 1a.shenter the valid directory name:kanasuthe file kanasu/1a.sh size is 392the file kanasu/file2 size is 49the file kanasu/file1 size is 49the file kanasu/file3 size is 43the file kanasu/file4 size is 20the large size of file kanasu/file4 is 392 1b. 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.SOURCE CODEif [ $# -lt 1 ];thenecho "NO arguments passed"exitelseecho $1 | tr '/' ' '>tmp_filefor i in `cat tmp_file`domkdir $icd $idoneecho "All directories have been created"echo "To view all directories execute ls -R command"fiOutput1:[reddy@localhost kanasu]$ sh 1b.shNO arguments passedOutput2:[reddy@localhost kanasu]$ sh 1b.sh a/b/c/dAll directories have been createdTo view all directories execute ls -R command[reddy@localhost kanasu]$ ls -R.:1a.sh 1b.sh a file1 file2 file3 file4 tmp_file./a:b./a/b:c./a/b/c:d./a/b/c/d:2a. 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.SOURCE CODEif [ $# -eq 0 ];thenecho "No arguments passed"exitfiif [ $# -lt 2 ] || [ $# -gt 2 ];thenecho "Please enter proper input"exitfiif [ ! -e $1 ] || [ ! -e $2 ];thenecho "File does not exists"exitfip1=`ls -l $1 | cut -d " " -f1`p2=`ls -l $2 | cut -d " " -f1`if [ $p1 == $p2 ];thenecho "Permissions of both the files are identical"echo "Identical permissions are:$p1"elseecho "Permissions of the files are not identical"echo "Permissions of file one are:$p1"echo "Permissions of file two are:$p2"fiOutput1:[reddy@localhost kanasu]$ sh 2a.shNo arguments passedOutput2:[reddy@localhost kanasu]$ sh 2a.sh file1 file2Permissions of both the files are identicalIdentical permissions are:-rw-rw-r--.Output3:[reddy@localhost kanasu]$ sh 2a.sh file1 file5Permissions of the files are not identicalPermissions of file one are:-rw-rw-r--.Permissions of file two are:-rwxrwxrwx.2b. Write a shell script which accepts valid log-in names as arguments and prints their corresponding home directories,if no argumrnts are specified,print a suitable error message.SOURCE CODEif [ $# -eq 0 ];thenecho "No argument in command line"exitelsefor file in $*doif grep $file /etc/passwdthenx=`grep $file /etc/passwd | cut -d ":" -f 6`echo "The valid username is :$x"elseecho "You have not entered a valid username"fidonefiOutput1:[reddy@localhost kanasu]$ sh 2b.shNo argument in command lineOutput2:[reddy@localhost kanasu]$ sh 2b.sh kanasuYou have not entered a valid usernameOutput3:[reddy@localhost kanasu]$ sh 2b.sh reddyreddy:x:500:500:Hanumantha Reddy:/home/reddy:/bin/bashThe valid username is :/home/reddy3a. Create a script file called file-properties that reads a file name entered and outputs its properties.SOURCE CODEecho "Enter a file:"read f1if [ ! -e $f1 ];thenecho "File not exist"exitelsea=`ls -l $f1 | cut -d " " -f1`b=`ls -l $f1 | cut -d " " -f2`c=`ls -l $f1 | cut -d " " -f3`d=`ls -l $f1 | cut -d " " -f4`e=`ls -l $f1 | cut -d " " -f5`f=`ls -l $f1 | cut -d " " -f 6-8`g=`ls -l $f1 | cut -d " " -f9`echo "File permissions are:$a"echo "File link number is/are:$b"echo "File owner is:$c"echo "File group owner is:$d"echo "File size is:$e"echo "File modification date is:$f"echo "File name is:$g"fiOutput1: [reddy@localhost kanasu]$ sh 3a.shEnter a file:file1File permissions are:-rw-rw-r--.File link number is/are:1File owner is:reddyFile group owner is:reddyFile size is:49File modification date is:Dec 25 14:59File name is:file1Output2:[reddy@localhost kanasu]$ sh 3a.shEnter a file:output1File not exist3b. 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 for 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.SOURCE CODEstty -echoecho "Enter the Password:"read pass1echo "Confirm the Password:"read pass2if [ $pass1 == $pass2 ];thenecho "The terminal is locked"while truedoecho "Enter the Password:"read pass3if [ $pass3 == $pass2 ];thenecho "The terminal is Unlocked"stty echoexitelseecho "Try again with correct Password"fidoneelseecho "Both the passwords are not matching"fiOutput1:[reddy@localhost kanasu]$ sh 3b.shEnter the Password:Confirm the Password:The terminal is lockedEnter the Password:The terminal is UnlockedOutput2: [reddy@localhost kanasu]$ sh 3b.shEnter the Password:Confirm the Password:The terminal is lockedEnter the Password:Try again with correct PasswordEnter the Password:The terminal is UnlockedOutput3: [reddy@localhost kanasu]$ sh 3b.shEnter the Password:Confirm the Password:Both the passwords are not matching4a. Write a shell script that accept one or more filenames as argument and convert them to uppercase, provided they exist in current directory.SOURCE CODEif [ $# -eq 0 ];thenecho "No arguments passed in command line"exitelsefor arg in $*doif [ -f $arg ];thennewfile=`echo $arg | tr "[a-z]" "[A-Z]"`if [ -f $newfile ];thenecho "$arg file already existS"elseecho "$arg converted into uppercase as:$newfile"fielseecho "$arg file does not exists"fidonefiOutput1: [reddy@localhost kanasu]$ sh 4a.shNo arguments passed in command lineOutput2:[reddy@localhost kanasu]$ sh 4a.sh file1 file2file1 converted into uppercase as:FILE1file2 converted into uppercase as:FILE2Output3:[reddy@localhost kanasu]$ sh 4a.sh file1 file2 file3 file4file1 converted into uppercase as:FILE1file2 converted into uppercase as:FILE2file3 converted into uppercase as:FILE3file4 converted into uppercase as:FILE44b. 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 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.SOURCE CODEif [ $# -eq 2 ]then if [ -d $2 ] then cd $2 n=0 x=`ls -il $1|cut -d " " -f1` for link in `find . -inum $x` do echo "$link" ((n=n+1)) done else echo "$2 is not a directory" fi echo "In `pwd` directory the number of links $n"elif [ $# -eq 1 ]then n=0 x=`ls -il $1|cut -d " " -f1` for link in `find . -inum $x` do echo "$link" done fiOutput1: [reddy@localhost kanasu]$ sh 4b.sh file1./file1Output2:[reddy@localhost kanasu]$ ln file1 temp[reddy@localhost kanasu]$ sh 4b.sh file1./file1./temp5a. 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.SOURCE CODEif [ $# -eq 0 ];thenecho "No arguments passed"exitfiif [ ! -e $1 ];thenecho "File does not exists"exitelsex=`ls -lu $1 | cut -d " " -f 5-8`echo "File creation time is:$x"fiOutput1:[reddy@localhost kanasu]$ sh 5a.sh No arguments passedOutput2:[reddy@localhost kanasu]$ sh 5a.sh file6File does not existsOutput3:[reddy@localhost kanasu]$ sh 5a.sh file1File creation time is:49 Dec 25 15:385b. 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.SOURCE CODEa=`date +%e`if [ $a -lt 10 ];then cal | sed "s/$a/*/"else cal | sed "s/$a/**/"fiOutput1:[reddy@localhost kanasu]$ sh 5b.sh December 2013 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 **15 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31Output2:[reddy@localhost kanasu]$ sh 5b.sh December 2013 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 * 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 316a. Write a shell script to find a file/s that matches a pattern given as command line argument in the home directory, display the contents of the file and copy the file into the directory ~/mydirSOURCE CODEd=`ls -x $* | tr -s "" " "`echo " The Contents of file are "for x in $ddocat $xcp $x ~/mydirecho " $x Copied "done Output: [reddy@localhost kanasu]$ sh 6a.sh reddy reddy1 The Contents of file are i am from belgaum reddy Copied i am hanumantha reddy reddy1 Copied [reddy@localhost kanasu]$ vi reddy[reddy@localhost kanasu]$ vi reddy16b. Write a shell script to list all the files in a directory whose filename is at least 10 characters.(use expr command to check the length).SOURCE CODEfor x in ‘ls’dolen=`expr length $x`if [ $len –ge 10 ] ; thenecho “ expr length $x “ $xfidoneOutput1:[reddy@localhost kanasu]$ sh 6b.shNo arguments in command lineOutput2:[reddy@localhost kanasu]$ sh 6a.sh file1File exist:file1Content of the copied file are ReddyManjunathOutput3:[reddy@localhost kanasu]$ sh 6a.sh file9File does not exists7a. 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.SOURCE CODEtime=`who am I | tr -s ‘ ‘ | cut -d ” ” -f 4 | cut -c 1,2`if [ $time -le 12 ] ; thenecho “Good Morning $LOGNAME”elif [ $time -gt 12 -a $time -lt 16 ] ; thenecho “Good Afternoon $LOGNAME”elseecho “Good Evenini $LOGNAME”fiOutput1: [reddy@localhost kanasu]$ sh 7a.sh“Good Evenining reddy”7b. Write a shell script that accept a list of filenames as its argument, count and reportOccurrence of each word that is present in the first argument file on other argument Files.SOURCE CODEif [ $# -lt 2 ] then echo "Invalid number of arguments.Enter atleast two arguments" exit fi str=` cat $1 | tr '\n' ' ' ` set $* shift for i in $* do echo "file name" $i echo "---------" for a in $str do echo "word=$a", count=` grep -c "$a" $i ` done doneOutput1: [reddy@localhost kanasu]$ sh 7b.sh aa bbfile name bb---------word=a, count=2word=b, count=2word=c, count=2word=d, count=2word=a, count=2word=b, count=2word=c, count=2word=d, count=2Output2:[reddy@localhost kanasu]$ sh 7b.sh x x1file name x1---------word=a, count=1word=b, count=1word=c, count=1word=d, count=18a. Write a shell script that determine the period for which a specified user is working on system and display appropriate message.SOURCE CODEecho "Enter Login name of the user"read name# get name and timing of logged in user from who cmduserinfo=`who | grep -i "$name" | grep "pts"` if [ $? -ne 0 ];then echo "$name is not Logged in" exitfihrs=`echo "$userinfo" | tr -s " " | cut -c ‘24-25’`min=`echo "$userinfo" | tr -s " " | cut -c ‘27-28’`curhr=`date | tr -s " " | cut -c 12-13` #get current time(hrs) curmin=`date | tr -s " " | cut -c 15-16` #get Cur time(min)hour=`expr $curhr - $hrs`minutes=`expr $curmin - $min`echo "$name is working since $hour hours and $minutes minutes"Output1:[reddy@localhost kanasu]$ sh 8a.shEnter Login name of the userreddyreddy is working since 2 hours and 17 minutesOutput2: [reddy@localhost kanasu]$ sh 8a.shEnter Login name of the userreddyreddy is working since 2 hours and 19 minutes8b. 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.SOURCE CODEecho "enter the login name of the user"read nameperiod=0until who | grep -w "$name"dosleep 5 #Pause the script for 5 secperiod=`expr $period + 1`if [ $period -gt 1 ];thenecho "$name has not login since 1 minute"exitfidoneecho "$name has now logged in "echo "enter the login name of the user"read nameperiod=0until who | grep -w "$name"dosleep 5 #Pause the script for 5 secperiod=`expr $period + 1`if [ $period -gt 1 ];thenecho "$name has not login since 1 minute"exitfidoneecho "$name has now logged in "Output1:[reddy@localhost kanasu]$ sh 8b.shenter the login name of the userreddyreddy tty1 2013-12-26 19:32 (:0)reddy pts/0 2013-12-26 19:33 (:0.0)reddy has now logged in enter the login name of the userreddyreddy tty1 2013-12-26 19:32 (:0)reddy pts/0 2013-12-26 19:33 (:0.0)reddy has now logged inOutput2:[reddy@localhost kanasu]$ sh 8b.shenter the login name of the userabcdabcd has not login since 1 minute9a. Write a shell script that accepts the file name, starting and ending line number as an argument and display all the lines between the given line number.SOURCECODEif [ $# -lt 3 ]then echo "The number of arguments in command line is less than 3" echo -e "Enter three arguments in the form of 'filename startline endline'\n"exitelse sed -n "$2,$3p" $1 echo "This is the section between lines $2 to $3 of the file:$1"fiOutput1:[reddy@localhost kanasu]$ sh 9a.sh repeat 1 4eatsatransatThis is the section between lines 1 to 4 of the file:repeatOutput2:[reddy@localhost kanasu]$ sh 9a.sh repeat 1 4The number of arguments in command line is less than 3Enter three arguments in the form of 'filename startline endline'9b. 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.SOURCECODEecho "Input Filename"read fileif [ ! -f $file ];thenecho "File not exists"exitfifor ((i=1;i<8;i++))do x=`sed -n "$i"p $file | wc -c` if [ $x -gt 40 ]; then z=`sed -n "$i"p $file | fold -w 40` echo "$z\\" else a=`sed -n "$i"p $file` echo "$a" fidoneOutput1:[reddy@localhost kanasu]$ sh 9b.shInput Filenameaaabcdabc[reddy@localhost kanasu]$ cat aaabcdabcd10a. Write an awk script that accepts date argument in the form of dd-mm-yy and displays it in the form if month, day and year. The script should check the validity of the argument and in the case of error, display a suitable message.SOURCECODEBEGIN { printf("enter date in the form(mm-dd-yy) : ") getline str < "/dev/tty" split(str,arr,"-") if(arr[1]<=0||arr[1]>12) printf("\n Invalid Month\n ") elseif(arr[2]<=0 || arr[2]>31) printf("\n Invalid date\n ") else system(" date +%A%t%B%t%Y -d" arr[3] "-" arr[1] "-" arr[2]) }Output1:[reddy@localhost kanasu]$ awk -f 10a.awkenter date in the form(mm-dd-yy) : 12-12-2013ThursdayDecember2013Output2:[reddy@localhost kanasu]$ awk -f 10a.awkenter date in the form(mm-dd-yy) : 06-22-1992MondayJune199210b. Write an awk script to delete duplicated line from a text file. The order of the original lines must remain unchanged.SOURCE CODEBEGIN {print "Program Start"}{if (data[$0]++==0)lines[++count]=$0}END {for ( i=1; i<count; i++)print lines[i]print "Program End"}Output1:[reddy@localhost kanasu]$ awk -f 10b.awk aaProgram StartabcProgram EndOutput2:[reddy@localhost kanasu]$ awk -f 10b.awk bbProgram Starta bcdefProgram End11a. 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.Electrical34Mechanical67Electrical80Computer Science43Mechanical65Civil98Computer Science64SOURCE CODEBEGIN {fs=" "printf "The student book details are"}{tot+=$2books[$1]=books[$1]+$2}END {printf "\n Sub name \t no. of books sold \n"for (i in books)printf "%s \t %d \n" , i , books[i]printf "Total books sold=%d\n" , tot }Output1:[reddy@localhost kanasu]$ awk -f 11a.awk book_detailsThe student book details are Sub name no. of books sold Civil 98 Computer_Science 107 Mechanical 132 Electrical 114 Total books sold=45111b. 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 basicIf basic salary is>=10000 then HRA=20% of basic & DA=50% of basicSOURCE CODEBEGIN {print "\n\t Salary statement of Employees for the month of May 2008\n"print "Sl.No.","NAME","\t\t","Designation","\t","Basic","\t","DA","\t","HRA","\t","Gross"slno=1}{if( $5 > 10000){da=0.45*$5hra=0.15*$5}else{da=0.50*$5hra=0.20*$5}printf("%2d\t%-15s%-12s%8d %8.2f %8.2f% 8.2f\n",slno++,$2,$3,$5,da,hra,$5+hra+da)}END {printf ("\nProgram End\n")}Output1:[reddy@localhost kanasu]$ cat > emp.lst1 John SoftwareEngineer 12-09-1985 200002 Mike HardwareEngineer 03-11-1993 180003 Rahul Mechanical 11-12-2011 13000[reddy@localhost kanasu]$ awk -f 11b.awk emp.lst Salary statement of Employees for the month of May 2008Sl.No. NAME Designation Basic DA HRA Gross 1John SoftwareEngineer 20000 9000.00 3000.00 32000.00 2Mike Hardware Engineer 18000 8100.00 2700.00 28800.00 3Rahul Mechanical 13000 5850.00 1950.00 20800.00Program End ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download