{"id":2178,"date":"2017-12-07T12:35:30","date_gmt":"2017-12-07T12:35:30","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2178"},"modified":"2020-01-09T09:45:38","modified_gmt":"2020-01-09T09:45:38","slug":"questions-to-test-your-linux-shell-script","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/questions-to-test-your-linux-shell-script\/","title":{"rendered":"Questions to test your linux shell Script"},"content":{"rendered":"<p><strong>rajeshkumar created the topic: Questions to test your linux shell Script<\/strong><\/p>\n<p><a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=427&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=427&#038;Itemid=442<\/a><br \/>\n<a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=426&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=426&#038;Itemid=442<\/a><br \/>\n<a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=425&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=425&#038;Itemid=442<\/a><br \/>\n<a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=424&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=424&#038;Itemid=442<\/a><br \/>\n<a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=423&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=423&#038;Itemid=442<\/a><br \/>\n<a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=422&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=422&#038;Itemid=442<\/a><br \/>\n<a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=419&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=419&#038;Itemid=442<\/a><br \/>\n<a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=421&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=421&#038;Itemid=442<\/a><br \/>\n<a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=420&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=420&#038;Itemid=442<\/a><br \/>\n<a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=418&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=418&#038;Itemid=442<\/a><br \/>\n<a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=431&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=431&#038;Itemid=442<\/a><br \/>\n<a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=430&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=430&#038;Itemid=442<\/a><br \/>\n<a href=\"http:\/\/www.scmgalaxy.com\/index.php?option=com_k...34&#038;id=428&#038;Itemid=442\" target=\"_blank\" rel=\"noopener\">www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=428&#038;Itemid=442<\/a><br \/>\nRegards,<br \/>\nRajesh Kumar<br \/>\nTwitt me @<a href=\"http:\/\/twitter.com\/RajeshKumarIn\" target=\"_blank\" rel=\"noopener\"> twitter.com\/RajeshKumarIn<\/a><\/p>\n<p><strong>sribhavani_u replied the topic: Questions to test your linux shell Script<\/strong><br \/>\n#assignment question1<br \/>\n#You have a number of C programs that certain comment lines at the beginning of each program. The lines begin with \/* followed by the first line of comment, butthe terminator line has *\/ as the only characters in the line. Remove these comments from all files.<br \/>\nfor file in *.c<br \/>\ndo<br \/>\necho &#8220;file name is $file&#8221;<br \/>\nif [ -e $file ]<br \/>\nthen<br \/>\n`sed &#8216;s\/[\/**\/]\/\/g&#8217; $file > newfile`<br \/>\nmv newfile $file<br \/>\nfi<br \/>\ndone<br \/>\n=======================================================================<br \/>\n#assignment question1<br \/>\n#You have a number of C programs that certain comment lines at the beginning of each program. The lines begin with \/* followed by the first line of comment, butthe terminator line has *\/ as the only characters in the line. Remove these comments from all files.<br \/>\nfor file in test123.c<br \/>\ndo<br \/>\necho &#8220;file name is $file&#8221;<br \/>\nnooflines=`wc -l $file | awk &#8216; { print $1 } &#8216;`<br \/>\ncat $file | grep -n &#8220;^\/\\*&#8221; | awk -F&#8221;:&#8221; &#8216; { print $1 } &#8216; | while read strtlineno<br \/>\ndo<br \/>\ntotlines=`wc -l $file | awk &#8216; { print $1 } &#8216;`<br \/>\nlet taillines=$nooflines-$strtlineno<br \/>\nendlineno=`tail -$taillines $file | grep -n &#8220;^\\*\/&#8221; | awk -F&#8221;:&#8221; &#8216; { print $1 } &#8216; | head -1`<br \/>\nlet endlineno=$strtlineno+$endlineno<br \/>\nlet difflines=$nooflines-$totlines<br \/>\nlet strtlineno=$strtlineno-$difflines<br \/>\nlet endlineno=$endlineno-$difflines<br \/>\necho &#8220;$strtlineno $endlineno&#8221;<br \/>\nendlineno=`echo $endlineno\\d`<br \/>\nsed -i $strtlineno,$endlineno $file<br \/>\ndone<br \/>\ndone<br \/>\n====================================================================<br \/>\n# assingment question2<br \/>\n#Write a script that compares two direcotries bar1 and bar2 (supplied as arguments) and copies or overwrites to bar1 from bar2 every file that is (i) not present in bar1 or (ii) newer than its namespace in bar1. (HINT: Use the find command)<\/p>\n<p>BAR1=\/root\/test\/bar1\/<br \/>\nBAR2=\/root\/test\/bar2\/<br \/>\nBAR1_MOD=\/root\/test\/bar1\/<\/p>\n<p>cd $BAR2<\/p>\n<p>find . -type f | while read filename<br \/>\ndo<\/p>\n<p>newfile=false<br \/>\nmodified=false<br \/>\nif [ ! -e &#8220;$BAR1$filename&#8221; ]; then<br \/>\nnewfile=true<br \/>\necho &#8220;ADD $filename&#8221;<br \/>\nelif ! cmp $filename $BAR1$filename &#038;>\/dev\/null; then<br \/>\nmodified=true<br \/>\necho &#8220;MOD $filename&#8221;<br \/>\nfi<\/p>\n<p>if $newfile || $modified; then<\/p>\n<p>#massage the filepath to not include leading .\/<br \/>\nfilepath=$BAR1_MOD$(echo $filename | cut -c3-)<\/p>\n<p>#create folder for it if it doesnt exist<br \/>\ndestfolder=$(echo $filepath | sed -e &#8216;s\/\\\/[^\\\/]*$\/\\\/\/&#8217;)<br \/>\nmkdir -p $destfolder<\/p>\n<p>#copy new\/modified file to the upgrade folder<br \/>\ncp $filename $filepath<br \/>\nfi<br \/>\ndone<br \/>\n====================================================================<br \/>\n#assingment question 3<br \/>\n#Add the statement #inlclude <stdio.h> at the beginning of every C source file in the current directory containing printf or fprintf, if it does not already have it included.<br \/>\negrep -l &#8220;printf|fprintf&#8221; *.c > new<br \/>\nfor filename in `cat new`<br \/>\ndo<br \/>\nif [ `grep -c &#8220;#include<stdio.h>&#8221; $filename` -eq 0 ]; then<br \/>\nsed &#8216;1i\\#include<stdio.h>\\&#8217; $filename > new1<br \/>\n`cat new1>$filename`<br \/>\nrm new1<br \/>\nfi<br \/>\ndone<br \/>\nrm new<br \/>\n====================================================================<br \/>\n#assingment question4<br \/>\n#!\/bin\/bash<br \/>\n#Find out the pathname of the Korn shell on your machine and then change the interpreter line in all shell script in the current directory that show a differentpathname for ksh<br \/>\ndest=`which ksh`<br \/>\ngrep -l &#8220;^#!.*ksh&#8221; *.shh | while read filename<br \/>\ndo<br \/>\ncat $filename | grep &#8220;^#!.*ksh&#8221; | while read src<br \/>\ndo<br \/>\nsed -i &#8220;s|$src|$dest|g&#8221; $filename<br \/>\ndone<br \/>\ndone<br \/>\n=======================================================================<br \/>\n# assingment question 5<br \/>\n#!\/bin\/bash<br \/>\n#Write a script that displays a special listing showing the (i) permission (ii) size (iii) filename (iv) last modification time (v) last access time of filenames supplied as arguments. Provide suitable headers using the printf command.<br \/>\nif [ $# -eq 0 ]<br \/>\nthen<br \/>\necho &#8220;pass the file name&#8221;<br \/>\nexit<br \/>\nfi<\/p>\n<p>divider==================================================<br \/>\ndivider=$divider$divider$divider$divider<\/p>\n<p>header=&#8221;\\n %-15s %15s %15s %40s %40s\\n&#8221;<\/p>\n<p>format=&#8221; %-15s %15d %15s %40s %40s \\n&#8221;<\/p>\n<p>width=75<\/p>\n<p>printf &#8220;$header&#8221; &#8220;PERMISSION&#8221; &#8220;SIZE&#8221; &#8220;FILENAME&#8221; &#8220;LASTMODTIME&#8221; &#8220;LASTACCESSTIME&#8221;<\/p>\n<p>printf &#8220;%$width.${width}s\\n&#8221; &#8220;$divider&#8221;<\/p>\n<p>while [ $# -ne 0 ]<br \/>\ndo<br \/>\nabc=`stat &#8211;format=%A&#8221; &#8220;%s&#8221; &#8220;%n&#8221; &#8220;\\&#8221;%y\\&#8221;&#8221; &#8220;\\&#8221;%x\\&#8221; $1 `<br \/>\nshift<br \/>\nprintf &#8220;$format&#8221; \\<br \/>\n$abc<br \/>\ndone<br \/>\n======================================================================<br \/>\n# assingment question 6<br \/>\n#You are moving files to handled which accepts only 8+3 type filesname. Produce a list of those files in your current directory that fail in this test<br \/>\n#find . -type f ! -name &#8220;????????.???&#8221;<br \/>\nfind . -type f -printf &#8220;%f\\n&#8221; | while read filename<br \/>\ndo<br \/>\nbname=`echo $filename | awk -F&#8221;.&#8221; &#8216; { print $1}&#8217; | wc -m`<br \/>\nename=`echo $filename | awk -F&#8221;.&#8221; &#8216; { print $2}&#8217; | wc -m`<br \/>\nif [ $bname != 9 ] || [ $ename != 4 ]; then<br \/>\necho &#8220;$filename&#8221;<br \/>\nfi<br \/>\ndone<br \/>\n=======================================================================<br \/>\n#assingment question7<br \/>\n#Expand the scope of the script in 16.12 (Test Your Understanding) to perform search recursively.<br \/>\n======================================================================<br \/>\n#assingment question 8<br \/>\n#Display a process in the system every 30 seconds five times using a(i) while loop (ii) for loop. What is the unusual feature of the for loop?<\/p>\n<p>#using while loop<br \/>\n#i=5<br \/>\n#while [ $i -gt 0 ]<br \/>\n#do<br \/>\n#echo &#8220;============&#8221;<br \/>\n#ps<br \/>\n#echo &#8220;===========&#8221;<br \/>\n#sleep 30<br \/>\n#i=$i-1<br \/>\n#done<br \/>\n#using for loop<br \/>\nfor i in 1 2 3 4 5<br \/>\ndo<br \/>\necho &#8220;*******&#8221;<br \/>\nps<br \/>\necho &#8220;*******&#8221;<br \/>\nsleep 30<br \/>\ndone<\/p>\n<p>#for loop can not check for condition<br \/>\n=====================================================================<br \/>\n#assignment question 9<br \/>\n#Write a script that behaves both in interactive and noninteractive mode. When no arguments are supplied, it picks up each C program from the current directory and lists the first 10 lines. it then prompts for deletion of the file. if the user supplies arguments with the script, then it works on those files only.<br \/>\nargs=$#<\/p>\n<p>if [ $args -eq 0 ]<br \/>\nthen<br \/>\nfor i in $(find . -name &#8220;*.shh&#8221;)<br \/>\ndo<br \/>\necho &#8220;$(sed -n &#8216;1,10p&#8217; $i)&#8221;<br \/>\nrm -i $i<br \/>\ndone<br \/>\nelse<br \/>\nfor i in $*<br \/>\ndo<br \/>\nif [ ! -f $i ]<br \/>\nthen<br \/>\necho &#8220;File $i does not exist&#8221;<br \/>\nelse<br \/>\necho &#8220;$(sed -n &#8216;1,10p&#8217; $i)&#8221;<br \/>\nrm -i $i<br \/>\nfi<br \/>\ndone<br \/>\nfi<br \/>\n=====================================================================<br \/>\n# assingment question 10<br \/>\n#Write a script that lists files by modification time when called with lm and by access time when called with la. By default, the script should show the listing of all files in the current directory.<\/p>\n<p># Listing files by modification time and access time<br \/>\ns=$1<br \/>\nif test &#8220;$s&#8221; == &#8220;lm&#8221;<br \/>\nthen<br \/>\nls -lt<br \/>\nelif test &#8220;$s&#8221; == &#8220;la&#8221;<br \/>\nthen<br \/>\nls -lu<br \/>\nelse<br \/>\nls -l<br \/>\nfi<br \/>\n====================================================================<br \/>\n# assingment question 11<br \/>\n#Assume that you have a number of files, downloaded from the internet, in the \/home\/kumar\/download directory. The table of contents (TOC) is available in the file TOC_Download.txt in the form filename:description. The script should check each file in the download directory that does not have a description in the TOC file and prompt the user for the description. The TOC should be updated to maintain the list in sorted condition. The script must be immune to signals.<br \/>\n#!\/bin\/ksh<br \/>\ntrap &#8221; INT TERM QUIT<br \/>\nTOC=\/root\/test\/download\/contents.txt<br \/>\nfind \/root\/test\/download -type f | grep -v $TOC | awk -F\/ &#8216;{ print $NF }&#8217; | sort -u | while read fname<br \/>\ndo<br \/>\nif [ `cat $TOC | grep -c &#8220;$fname&#8221;` == 1 ]; then<br \/>\necho &#8220;file description exists for $fname&#8221;<br \/>\nelse<br \/>\necho &#8220;update description for $fname: &#8221;<br \/>\nread desc <\/dev\/tty\necho \"$fname=$desc\" >> $TOC<br \/>\nsort -o \/tmp\/assing11.txt $TOC<br \/>\ncat \/tmp\/assing11.txt > $TOC<br \/>\nrm -f \/tmp\/assing11.txt<br \/>\nfi<br \/>\ndone<br \/>\n===================================================================<br \/>\n#assingment question 12<br \/>\n#Devise a script that creates a lock file which prevents more than one user from running it. The lock file must be removed before script termination or if the user presses the interrrupt key.<br \/>\nsetEnv()<br \/>\n{<br \/>\nLOCK=\/tmp\/mylock<br \/>\n}<br \/>\ncheckLock()<br \/>\n{<br \/>\nif [ -f $LOCK ]; then<br \/>\necho &#8220;Someone is running the script, Hence exiting&#8230;&#8221;<br \/>\nexit 1;<br \/>\nfi<br \/>\n}<br \/>\ncreateLock()<br \/>\n{<br \/>\necho &#8220;Creating lock $LOCK&#8221;<br \/>\ntouch $LOCK<br \/>\n}<br \/>\ndoWork()<br \/>\n{<br \/>\necho &#8220;Working&#8221;<br \/>\nsleep 10<br \/>\n}<br \/>\nremoveLock()<br \/>\n{<br \/>\necho &#8220;Removing lock $LOCK&#8221;<br \/>\nrm -f $LOCK<br \/>\n}<br \/>\ncapkill()<br \/>\n{<br \/>\nremoveLock<br \/>\nexit 1<br \/>\n}<br \/>\n########<br \/>\n# MAIN #<br \/>\n########<br \/>\ntrap capkill INT TERM QUIT<br \/>\nsetEnv<br \/>\ncheckLock<br \/>\ncreateLock<br \/>\ndoWork<br \/>\nremoveLock<br \/>\n===================================================================<br \/>\n# assingment qestion 13<br \/>\n#Write a shell script that uses find to look for a file and echo a suitable message if the file is not found. you must not store the find output in a file.<br \/>\necho &#8220;give the name of the file to be searched&#8221;<br \/>\nread file<br \/>\nif [ ` find . -type f -iname $file` ];<br \/>\nthen<br \/>\necho &#8220;$file exists&#8221;<br \/>\nelse<br \/>\necho &#8220;File \\&#8221;&#8221;$file&#8221;\\&#8221; does not exist.&#8221;<br \/>\nfi<\/p>\n<p><strong>chaitu137 replied the topic: Questions to test your linux shell Script<\/strong><br \/>\nSolution to Question 01<\/p>\n<p>#!\/bin\/sh<\/p>\n<p>sed -i &#8216;\/\\*\/{D}&#8217; *.c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>rajeshkumar created the topic: Questions to test your linux shell Script www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=427&#038;Itemid=442 www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=426&#038;Itemid=442 www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=425&#038;Itemid=442 www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=424&#038;Itemid=442 www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=423&#038;Itemid=442 www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=422&#038;Itemid=442 www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=419&#038;Itemid=442 www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=421&#038;Itemid=442 www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=420&#038;Itemid=442 www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=418&#038;Itemid=442 www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=431&#038;Itemid=442 www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=430&#038;Itemid=442 www.scmgalaxy.com\/index.php?option=com_k&#8230;34&#038;id=428&#038;Itemid=442 Regards, Rajesh Kumar Twitt me @&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[454],"tags":[278],"class_list":["post-2178","post","type-post","status-publish","format-standard","hentry","category-shell-script","tag-script"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=2178"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2178\/revisions"}],"predecessor-version":[{"id":2179,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2178\/revisions\/2179"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}