{"id":4568,"date":"2015-04-23T16:43:29","date_gmt":"2015-04-23T08:43:29","guid":{"rendered":"http:\/\/rmohan.com\/?p=4568"},"modified":"2015-04-23T16:43:29","modified_gmt":"2015-04-23T08:43:29","slug":"shell-scripting-reference","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=4568","title":{"rendered":"SHELL SCRIPTING Reference"},"content":{"rendered":"<p><strong>SHELL SCRIPTING:<\/strong><\/p>\n<p>-shell is a command line interpreter<\/p>\n<p>-it is interface b\/w user &amp; kernel<\/p>\n<p>&#8211; it takes commands form the user and excute them<\/p>\n<p>Shell\u00a0 are different types:<\/p>\n<p>Shell\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 developed\u00a0\u00a0 shellprompt\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 executetioncommand<\/p>\n<p>Bourne \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SteveBorn\u00a0\u00a0 $\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sh<\/p>\n<p>Corn\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 devidCorn\u00a0\u00a0 $\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ksh<\/p>\n<p>Cshell\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 billjoy\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 %\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 csh<\/p>\n<p>Bash\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 born\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sh(or)bsh<\/p>\n<p>Zsh\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 panel\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 zsh<\/p>\n<p>Shell variables:<\/p>\n<p>There are two types of shell variables.<\/p>\n<p>Unix Defined or system defined<\/p>\n<p>User Defined<\/p>\n<p>Unix Defined:-these are standed variable which are always accessable.the shell provide the values for these variables. These variables are usually used by the system we can change the variables as per over preference an custamize the system environment.<\/p>\n<p>$ set<\/p>\n<p>HOME=\/user\/pavan<\/p>\n<p>HZ=100<\/p>\n<p>IFS=space or tab or new line<\/p>\n<p>LOGNAME=pavan<\/p>\n<p>MAIL=usr\/sped\/mail\/pavan<\/p>\n<p>MAILCHEECK=600sec<\/p>\n<p>OPTEND=1<\/p>\n<p>PATH=\/bin:\/usr\/bin:\/usr\/pavan:\/bin:.<\/p>\n<p>PS1=$primary shell prompt<\/p>\n<p>Ps2= &gt; -system prompt(default values)<\/p>\n<p>SHELL=\/bin\/sh<\/p>\n<p>TERM=vt100<\/p>\n<p>TZ=ist-5:30<\/p>\n<p>PATH:- Define the which shell\u00a0 must search in order to command .<\/p>\n<p>HOME:-store default working directory of the user.<\/p>\n<p>LOGNAME:-store the log name of the user.<\/p>\n<p>MAIL:-Define the file where the mail of user stored.<\/p>\n<p>MAILCHECK:-define the duration after which shell checks whether the user received mail by default the values 600 sec<\/p>\n<p>IFS:-define the internal felids separator which is space or tab or newline<\/p>\n<p>SHELL:-define the name of you are working shell.<\/p>\n<p>TERM:-define the name of the term and which you r working.<\/p>\n<p>TZ:-define the name of the time zone in which you are working<\/p>\n<p>User defined variable:-<\/p>\n<p>These are defined by the user and used most exclusively \u00a0in shell programming<\/p>\n<p>Rules for creating user shell variables<\/p>\n<p>The first character should be alphabet or \u2013<\/p>\n<p>Eg:- -a= ,a=,b=,c=,d=<\/p>\n<p>-no commas or blanks are allowed<\/p>\n<p>Eg:-a,b=10 is worng<\/p>\n<p>Variable names should be casesensitive<\/p>\n<p>Eg:-name=,Name=,nAme=<\/p>\n<p>Variable names should be a shell keywords.<\/p>\n<p>SHELL keywords:<\/p>\n<p>Key words are words which meaning as already been explained to the shell.<\/p>\n<p>Keywords are also called as reverse words<\/p>\n<p>The list of key words are in bourn shell<\/p>\n<p>Echo\u00a0 for \u00a0\u00a0\u00a0 if\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 untill read\u00a0\u00a0 else\u00a0\u00a0\u00a0 case\u00a0\u00a0 set\u00a0\u00a0\u00a0\u00a0 wait\u00a0\u00a0 fi\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 esas\u00a0\u00a0 uset\u00a0\u00a0 eval\u00a0\u00a0 while readonly\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break do \u00a0\u00a0\u00a0\u00a0 shift\u00a0\u00a0 exec\u00a0\u00a0 continue\u00a0\u00a0\u00a0\u00a0\u00a0 done\u00a0 ulimit export\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 exit\u00a0\u00a0\u00a0 umask\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 frap<\/p>\n<p><strong>Echo statements<\/strong>: Echo command is used to display the message on the screen and it is used to display the values stored in a shell variable.<\/p>\n<p>$echo \u201cwelcome\u201d<\/p>\n<p>Display output to the screen.<\/p>\n<p>Double cotes are optional.<\/p>\n<p>UNIX command should be in back cotes (`) in echo statement, otherwise it treats as a text.<\/p>\n<p>$echo \u201ctodays date is : date\u201d<\/p>\n<p>O\/p &#8211; todays date is: date<\/p>\n<p>$echo \u201ctodays date is :`date`\u201c<\/p>\n<p>o\/p-todays date is : fri apr 5 9:15 Gt 2013<\/p>\n<p>$echo \u201cno of files `wc \u2013l file1` \u201d<\/p>\n<p>o\/p \u2013 no of files\u00a0 10<\/p>\n<p>$echo \u201cmy log in name`logname`<\/p>\n<p>o\/p \u2013 my login name is shahid<\/p>\n<p>$echo \u201cmy present working directory is :`pwd`\u201d<\/p>\n<p>o\/p \u2013 my present working directory is :\/home\/shahid<\/p>\n<p><strong>Shell variables:<\/strong><\/p>\n<p>$a = 10<\/p>\n<p>$b = 20<\/p>\n<p>$name = \u201cshahid\u201d<\/p>\n<p>$echo $a \u2013 10<\/p>\n<p>$echo $b \u2013 20<\/p>\n<p>$echo $name &#8211; shahid<\/p>\n<p>$mypath=\/home\/shahid\/dir1<\/p>\n<p>$cd $mypath<\/p>\n<p>$pwd<\/p>\n<p>\/home\/shahid\/dir1<\/p>\n<p>Null variables:<\/p>\n<p>A variable which has defined but has not been given any value is known as a null variable.<\/p>\n<p>$n=\u201d \u201c<\/p>\n<p>$n=\u2019 \u2018<\/p>\n<p>Constant: It is a fixed value it doesn\u2019t change during execution of the program.<\/p>\n<p>$readonly a<\/p>\n<p>When the variable is made readonly the shell doesn\u2019t allow you to change their values.<\/p>\n<p>$unset a\u00a0 -&gt; the variable a and its value assigned to it are erased from shell memory.<\/p>\n<p>Escape sequences:<\/p>\n<p>echo \u201cshahid\\n\u201d<\/p>\n<p>echo \u201cpavan\u201d<\/p>\n<p>\\n \u2013 displays in new line<\/p>\n<p>\\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 newline\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $echo\u201dshahid\\nshaik\u201d<\/p>\n<p>\\t\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 tab\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $echo\u201dshahid\\tshaik\u201d&#8212;-shahid \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 shaik<\/p>\n<p>\\b\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 backspace\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $echo\u201dpavan\\bkumar\u201d&#8212;-pavakumar<\/p>\n<p>\\\u201d\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 double quote\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $echo\u201d\\\u201dpavankumar\\\u201d\u201d<\/p>\n<p>\\\u2019\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 single quote<\/p>\n<p>\\\\\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 backslash<\/p>\n<p>Sample program:<\/p>\n<p>Write a program to display list of files and directories and present working directories and no of users logged into the system.<\/p>\n<p>Vi sample.sh<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"250\">#! \/bin\/bsh<\/p>\n<p>ls \u2013l<\/p>\n<p>pwd<\/p>\n<p>who<\/p>\n<p>:wq<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>How to execute shell script?<\/p>\n<p>$sh sample.sh<\/p>\n<p>$.\/sample.sh<\/p>\n<p>We have to check the permissions and then we have to execute.<\/p>\n<p>Chmod 775 sample.sh<\/p>\n<p>Write a program to display the address?<\/p>\n<p>#!\/bin\/sh<\/p>\n<p>echo \u201c100\/14,20 th main\\n\u201d<\/p>\n<p>echo \u201c9<sup>th<\/sup>\u00a0cross maruthi nagar\\n\u201d<\/p>\n<p>echo\u201dBangalore\\n\u201d<\/p>\n<p>:wq<\/p>\n<p>$.\/address.sh<\/p>\n<p>Write a program to count no of users are currently logged into the system?<\/p>\n<p>Echo \u201c The no of users :`who|wc \u2013l`\u201d<\/p>\n<p>:wq users.sh<\/p>\n<p>.\/users.sh<\/p>\n<p>Read input from program?<\/p>\n<p>Echo \u201center your name:\u201d<\/p>\n<p>Read name<\/p>\n<p>Echo \u201chello $name how are you\u201d<\/p>\n<p>Execution:<\/p>\n<p>Enter your name: shahid<\/p>\n<p>Hello shahid how are you<\/p>\n<p>Read is a command to read variable value from the input user or keyboard.<\/p>\n<p>Read command reads value from the keyboard up to space or enter key.<\/p>\n<p>echo \u201center a, b, c values\u201d<\/p>\n<p>read a b c<\/p>\n<p>echo $a,$b,$c<\/p>\n<p>enter a b c values :3\u00a0 4 5<\/p>\n<p>3,4,5<\/p>\n<p>write a program to read two numbers and display?<\/p>\n<p>Echo\u201denter a ,b values\u201d<\/p>\n<p>Read a b<\/p>\n<p>Echo $a\u00a0 $b<\/p>\n<p>Operators:<\/p>\n<ol>\n<li>Arthematic operators<\/li>\n<li>Relational operators<\/li>\n<\/ol>\n<p>Numeric comparison<\/p>\n<p>String comparison<\/p>\n<ol start=\"3\">\n<li>Logical operators<\/li>\n<\/ol>\n<p>1.Arthematic operators : Addition(+) ,subtraction(-) multiplication(*),division(\/),modulus division(%)<\/p>\n<ol start=\"2\">\n<li>Relational operators: -gt (&gt;),- lt(&lt;) ,- ge (&gt;=),-le (&lt;=),-eq(=),-ne(!=)<\/li>\n<li>Logical operators:<\/li>\n<\/ol>\n<p>-And\u00a0 -a<\/p>\n<p>-Or\u00a0\u00a0\u00a0 -o<\/p>\n<p>-Nor\u00a0\u00a0 -!<\/p>\n<p>Write a program to read two numbers and display sum diff pro and div?<\/p>\n<p>Echo \u201center 2 numbers\u201d<\/p>\n<p>Read a b<\/p>\n<p>C = `expr $a + $b`<\/p>\n<p>Echo\u00a0 \u201c addition is $c\u201d<\/p>\n<p>D = `expr $a &#8211; $b`<\/p>\n<p>Echo \u201csubtraction is $D\u201d<\/p>\n<p>E = `expr $a \\ * $b`<\/p>\n<p>Echo \u201cproduct is $E\u201d<\/p>\n<p>F = `expr $a \/ $b `<\/p>\n<p>Echo \u201cdivision is $F\u201d<\/p>\n<p>*_ ? [] &#8212;wild chard characters we will use \u201c\\\u201d<\/p>\n<p>Expr is a command to evaluating arthematic expressions.<\/p>\n<p>But expr is capable of carrying out only integer arthematic.<\/p>\n<p>Write a program to read 2 float numbers and display sum diff prod and divi?<\/p>\n<p>Echo \u201center 2 numbers\u201d<\/p>\n<p>Read a b<\/p>\n<p>C = `echo $a + $b\\bc`<\/p>\n<p>Echo\u00a0 \u201c addition is $c\u201d<\/p>\n<p>D = `echo $a &#8211; $b\\bc`<\/p>\n<p>Echo \u201csubtraction is $D\u201d<\/p>\n<p>E = `echo $a \\ * $b\\bc`<\/p>\n<p>Echo \u201cproduct is $E\u201d<\/p>\n<p>F = `echo $a \/ $b\\bc `<\/p>\n<p>Echo \u201cdivision is $F\u201d\u00e0bc \u2013 basic calculator<\/p>\n<p>IF statements:<\/p>\n<p>If condition<\/p>\n<p>then<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>Fi<\/p>\n<ol start=\"2\">\n<li>if condition<\/li>\n<\/ol>\n<p>Then<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>Else<\/p>\n<p>Fi<\/p>\n<p>3.ifcondition<\/p>\n<p>Then<\/p>\n<p>&#8212;-<\/p>\n<p>Elif condition<\/p>\n<p>&#8212;-<\/p>\n<p>Elif condition<\/p>\n<p>&#8212;-<\/p>\n<p>Fi<\/p>\n<p>If 0-true<\/p>\n<p>If 1-false<\/p>\n<p>Cd $path<\/p>\n<p>0-success<\/p>\n<p>1-failure<\/p>\n<p>Write a program to change directory?<\/p>\n<p>Echo \u201denter directory name\u201d<\/p>\n<p>Read dir<\/p>\n<p>If [cd\u00a0 $dir]<\/p>\n<p>Then<\/p>\n<p>Echo \u201cchanged to $dir\u201d<\/p>\n<p>Pwd<\/p>\n<p>Fi<\/p>\n<p>Write a program to copy a file?<\/p>\n<p>Echo \u201center source and target file names\u201d<\/p>\n<p>Read source target<\/p>\n<p>If cp $source $target<\/p>\n<p>Then<\/p>\n<p>Echo \u201cfiles have been copied successfully\u201d<\/p>\n<p>Else<\/p>\n<p>Echo \u201cfailed to copy the file\u201d<\/p>\n<p>Fi<\/p>\n<p>Write a program to search a string in a file?<\/p>\n<p>Write a program to find greatest number of two numbers?<\/p>\n<p>Echo \u201c enter two numbers\u201d<\/p>\n<p>Read a b<\/p>\n<p>If [$a \u2013gt $b]<\/p>\n<p>Then<\/p>\n<p>Echo \u201c $a is greatest number\u201d<\/p>\n<p>Else<\/p>\n<p>Echo \u201c$b is greatest number\u201d<\/p>\n<p>fi<\/p>\n<p>write a program to check given number is even or odd?<\/p>\n<p>Echo \u201c enter a number\u201d<\/p>\n<p>Read a<\/p>\n<p>If [`expr $a%2` -eq 0]<\/p>\n<p>Then<\/p>\n<p>Echo \u201c even number\u201d<\/p>\n<p>Else<\/p>\n<p>Echo \u201c odd number\u201d<\/p>\n<p>Fi<\/p>\n<p>Text:-(we use conjection if)<\/p>\n<p>If constantsthe\u00a0 are generally\u00a0 used in conjection text command.<\/p>\n<p>Eg:-if text [condi]<\/p>\n<p>If condi<\/p>\n<p>If[condi]<\/p>\n<p>The text command help us to find the contents of\u00a0 a variable the no of variables and the type of file or kind of file permission.<\/p>\n<p>The text command returns an exist status after evaluating the condition<\/p>\n<p>Sy:-if text condition<\/p>\n<p>Then<\/p>\n<p>Commands<\/p>\n<p>Else<\/p>\n<p>Commands<\/p>\n<p>Fi<\/p>\n<p>Write a program to check how many users or working on the system<\/p>\n<p>#!\/usr\/bin\/bsh<\/p>\n<p>Total=\u2019 who|wc \u2013l \u2018<\/p>\n<p>If text $total \u2013eq 1<\/p>\n<p>Then<\/p>\n<p>Echo\u201d u r only the user logged in \u201c<\/p>\n<p>Else<\/p>\n<p>Echo \u201ctotal no of users:$total\u201d<\/p>\n<p>Fi<\/p>\n<p>Write a program to check the given number is +ve or \u2013ve?<\/p>\n<p>#!\/usr\/bin\/bsh<\/p>\n<p>Echo \u201center a number in\u201d<\/p>\n<p>Read a<\/p>\n<p>If text $a \u2013gt 0<\/p>\n<p>Then<\/p>\n<p>Echo \u201c the entered no is +ve\u201d<\/p>\n<p>elseIf text $a \u2013eq 0<\/p>\n<p>echo \u201cthe entered no is 0\u201d<\/p>\n<p>elseif text $a \u2013lt 0<\/p>\n<p>echo \u201cthe entered no is \u2013ve\u201d<\/p>\n<p>fi<\/p>\n<p>write a program to find out student results<\/p>\n<p>#!\/usr\/bin\/bsh<\/p>\n<p>Echo \u201center subject marks\u201d<\/p>\n<p>Read m1 m2 m3<\/p>\n<p>If text $m1 \u2013ge 35<\/p>\n<p>Then<\/p>\n<p>If text $m2 \u2013ge 35<\/p>\n<p>then<\/p>\n<p>If text $m1 \u2013ge 35<\/p>\n<p>Echo \u201cpass\u201d<\/p>\n<p>Else<\/p>\n<p>Echo \u201cfail\u201d<\/p>\n<p>Elseif<\/p>\n<p>Echo \u201cfail\u201d<\/p>\n<p>Elseif<\/p>\n<p>Echo \u201cfail\u201d<\/p>\n<p>Fi<\/p>\n<p>Write a program to print greatings.<\/p>\n<p>Hour=\u2019date | cut \u2013c 12, 13\u2019<\/p>\n<p>If[hour \u2013ge 0 \u2013a $hour \u2013lt 12]<\/p>\n<p>Then<\/p>\n<p>Echo \u201cGood moring\u201d<\/p>\n<p>Elseif[$hour \u2013gt 12 \u2013a $hour \u2013lt 17)<\/p>\n<p>Echo \u201cgood afternoon\u201d<\/p>\n<p>Else<\/p>\n<p>Echo \u201cgood evening\u201d<\/p>\n<p>Fi<\/p>\n<p>Fi<\/p>\n<p>File text command:<\/p>\n<p>-s \u2013true if the file exist and has a size&gt;0<\/p>\n<p>-f-true if the file exists and is not a directory.<\/p>\n<p>-d-true if the exists is in directory<\/p>\n<p>-c- true if the file exist<\/p>\n<p>-r-true if the file exist it have read permission<\/p>\n<p>-w-true if the file write permission<\/p>\n<p>-x-true if the file execute permission<\/p>\n<p>Write a program to check for ordinary file and display it contents<\/p>\n<p>Echo \u201center a file name to be opened\u201d<\/p>\n<p>Read filename<\/p>\n<p>If text \u2013f $filename<\/p>\n<p>Then<\/p>\n<p>Cat&lt;$fname<\/p>\n<p>Else<\/p>\n<p>Echo \u201c file is not present \u201c<\/p>\n<p>Fi<\/p>\n<p>Write a program to check given file an ordinary file or directory<\/p>\n<p>Echo \u201center a filename\u201d<\/p>\n<p>Read fname<\/p>\n<p>If[-f $fname]<\/p>\n<p>Then<\/p>\n<p>Echo\u201dit is an ordinary file\u201d<\/p>\n<p>Elseif[-d $fname]<\/p>\n<p>Echo \u201cit is a directory file\u201d<\/p>\n<p>Else<\/p>\n<p>Echo \u201cit is not an ordinary file\u201d<\/p>\n<p>Fi<\/p>\n<p>Write a program to open a file if the doesn\u2019t have read permission then assign the permission &amp; open a file .<\/p>\n<p>Echo \u201center a file\u201d<\/p>\n<p>Read filename<\/p>\n<p>If [-r $ fname]<\/p>\n<p>Then<\/p>\n<p>Cat &lt;$fname<\/p>\n<p>Else<\/p>\n<p>Chmod u+r $fname<\/p>\n<p>Cat&lt;$fname<\/p>\n<p>Fi<\/p>\n<p>Write a program to append data to the file<\/p>\n<p>Echo \u201center a file\u201d<\/p>\n<p>Read fname<\/p>\n<p>If[-f $fname]<\/p>\n<p>Then<\/p>\n<p>If[-w$fname]<\/p>\n<p>Then<\/p>\n<p>Echo \u201center the content &amp; press ctr+d at the end\u201d<\/p>\n<p>Cat&gt;&gt;fname<\/p>\n<p>Else<\/p>\n<p>Chmod u+w $fname<\/p>\n<p>Cat&gt;&gt;fname<\/p>\n<p>Fi<\/p>\n<p>Else<\/p>\n<p>Echo \u201center the data press ctr+d\u201d<\/p>\n<p>Cat&gt;fname<\/p>\n<p>Fi<\/p>\n<p>String text command:<\/p>\n<p>Str1=str2-true if the strings are equal<\/p>\n<p>Str!=str2-true if the strings not equal<\/p>\n<p>-n str-true if the length of string is &gt;0<\/p>\n<p>-z str-true if the length of equal zero(=0)<\/p>\n<p>Write a program to compare two strings<\/p>\n<p>Echo\u201denter str1\u201d<\/p>\n<p>Read str1<\/p>\n<p>Echo \u201center str2\u201d<\/p>\n<p>Read str2<\/p>\n<p>If text $str1=$str2<\/p>\n<p>Then<\/p>\n<p>Echo \u201cboth are equal\u201d<\/p>\n<p>Else<\/p>\n<p>Echo \u201cboth are not equal\u201d<\/p>\n<p>Fi<\/p>\n<p>Write a program to check given string is empty or not<\/p>\n<p>Echo \u201center a string \u201c<\/p>\n<p>Read string<\/p>\n<p>If text \u2013z $string<\/p>\n<p>Then<\/p>\n<p>Echo \u201cstring is empty\u201d<\/p>\n<p>Else<\/p>\n<p>Echo \u201cstring is not empty\u201d<\/p>\n<p>Fi<\/p>\n<p>CASE CONTROL STATEMENTS<\/p>\n<p>Case value in<\/p>\n<p>Choice 1)<\/p>\n<p>___________<\/p>\n<p>___________<\/p>\n<p>;;<\/p>\n<p>Choice 2)<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>;;<\/p>\n<p>Choice *<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>;;<\/p>\n<p>esac<\/p>\n<p>The expression the following the case keyword is evaluate first the value that it yields is then mathch one by one against potential choices like choice 1 choice 2 &#8230;&#8230;&#8230;&#8230;&#8230;. choice n.when match is found then shell will execute all commands in that case upto ;; .this pair of semicolons placed at the end of each choice.<\/p>\n<p>Eg:-<\/p>\n<p>Echo \u201center a number b\/w 1 to 4\u201d<\/p>\n<p>Read num<\/p>\n<p>Case $num in<\/p>\n<p>Echo \u201c u entered one\u201d<\/p>\n<p>;;<\/p>\n<p>Echo \u201cu entered two\u201d<\/p>\n<p>;;<\/p>\n<p>Echo \u201cu entered three\u201d<\/p>\n<p>;;<\/p>\n<p>Echo \u201cu entered four\u201d<\/p>\n<p>;;<\/p>\n<p>Esac<\/p>\n<p>Write a program to check given character is uppercase alphabet or lower case alphabet or digit or special character.<\/p>\n<p>Echo \u201center a character\u201d<\/p>\n<p>Read ch<\/p>\n<p>Case $ ch in<\/p>\n<p>[a-z] echo \u201cu entered lowercase charecters\u201d<\/p>\n<p>;;<\/p>\n<p>[A-Z] echo \u201c u entere lower case charecters\u201d<\/p>\n<p>;;<\/p>\n<p>[0-9] echo \u201cu entered digits\u201d<\/p>\n<p>;;<\/p>\n<p>?) echo \u201cu entered special character\u201d<\/p>\n<p>;;<\/p>\n<p>Esac<\/p>\n<p>Write a program to display file contents or write on to the file or execute based on user choice<\/p>\n<p>Echo \u201center a filename\u201d<\/p>\n<p>Read fname<\/p>\n<p>Echo \u201cmain menu\u201d<\/p>\n<p>Echo \u201c***********\u201d<\/p>\n<p>Echo\u201dr-readmode\u201d<\/p>\n<p>Echo \u201cw-writemode\u201d<\/p>\n<p>Echo \u201cx-excute mode\u201d<\/p>\n<p>Echo \u201center mode\u201d<\/p>\n<p>Read mode<\/p>\n<p>Case $ mode in<\/p>\n<p>Choice r) If[-f $fname \u2013a \u2013r $fname]<\/p>\n<p>Then<\/p>\n<p>Cat&lt;$fname<\/p>\n<p>Fi<\/p>\n<p>;;<\/p>\n<p>Choice w) if[-f $fname \u2013a \u2013w$fname]<\/p>\n<p>Then<\/p>\n<p>Echo \u201center data &amp; press ctr+d\u201d<\/p>\n<p>Cat&gt;&gt;$fname<\/p>\n<p>Fi<\/p>\n<p>;;<\/p>\n<p>Choice x) if[-f $fname \u2013a \u2013x $fname]<\/p>\n<p>Then<\/p>\n<p>$fname<\/p>\n<p>Fi<\/p>\n<p>;;<\/p>\n<p>Choice *) echo \u201cu r execute invalid input\u201d<\/p>\n<p>Esac<\/p>\n<p>Exit statement:<\/p>\n<p>It is used to terminate execution of shell programming<\/p>\n<p>Eg:-$exit<\/p>\n<p>Looping control statements:<\/p>\n<p>While<\/p>\n<p>Until<\/p>\n<p>forloop<\/p>\n<p>while:-<\/p>\n<p>while condition<\/p>\n<p>do<\/p>\n<p>&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>Done<\/p>\n<p>Until:<\/p>\n<p>Until condition<\/p>\n<p>Do<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>Done<\/p>\n<p>Forloop:<\/p>\n<p>For varable in value1,value2&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.valuen<\/p>\n<p>Do<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Done<\/p>\n<p>Write a program to display number from 1 to 10<\/p>\n<p>Echo \u201center no from 1 to 10\u201d<\/p>\n<p>I=1<\/p>\n<p>While [$i \u2013gt 10]<\/p>\n<p>Do<\/p>\n<p>Echo \u201c$i\u201d<\/p>\n<p>I=\u201dexpr $i+1\u201d<\/p>\n<p>Done<\/p>\n<p>Break Or continue statements:<\/p>\n<p>Break:<\/p>\n<p>When the keyword break is encounted inside any loop etc automatically passes to the first statement after the loop<\/p>\n<p>While condition<\/p>\n<p>Do<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>Break<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>Done<\/p>\n<p>Continue:<\/p>\n<p>When the keyword continue is encounted inside loop control automatically passes to the beginning of the loop &amp; continue from beginning of the loop .<\/p>\n<p>Write a program to display file contents of the file existing<\/p>\n<p>X=0<\/p>\n<p>While test $x=0<\/p>\n<p>Do<\/p>\n<p>Echo \u201center a filename\u201d<\/p>\n<p>Read fname<\/p>\n<p>If text ! \u2013f $name<\/p>\n<p>Then<\/p>\n<p>Echo \u201c$fname is not found enter current file \u201c<\/p>\n<p>Continue<\/p>\n<p>Else<\/p>\n<p>Break<\/p>\n<p>Fi<\/p>\n<p>Done<\/p>\n<p>Cat $ fname | more<\/p>\n<p>Until: execute from false.then true it will not execute<\/p>\n<p>Write a program to print no 1 to 10?<\/p>\n<p>I=1<\/p>\n<p>Untile [$i \u2013gt 10]<\/p>\n<p>Do<\/p>\n<p>Echo $i<\/p>\n<p>I=\u201dexpr $i+1\u201d<\/p>\n<p>Done<\/p>\n<p>Sleep:<\/p>\n<p>Sleep commmad stop the execution of the program that specified no of seconds.<\/p>\n<p>Write a program to print no\u00a0 from 1 to 10.<\/p>\n<p>For i in 1 2 3 4 5 6 7 8 9 10<\/p>\n<p>Do<\/p>\n<p>Echo $i<\/p>\n<p>Done<\/p>\n<p>Command line arguments:<br \/>\n$0-first argument<\/p>\n<p>$1-second argument<\/p>\n<p>$.\/script.sh arg1 arg2&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;argn<\/p>\n<p>$*-contains active string of an arguments<\/p>\n<p>$#-contains no of arguments specified in the command<\/p>\n<p>$?-it will store the last execution commands states.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SHELL SCRIPTING:<\/p>\n<p>-shell is a command line interpreter<\/p>\n<p>-it is interface b\/w user &amp; kernel<\/p>\n<p>&#8211; it takes commands form the user and excute them<\/p>\n<p>Shell are different types:<\/p>\n<p>Shell developed shellprompt executetioncommand<\/p>\n<p>Bourne SteveBorn $ sh<\/p>\n<p>Corn devidCorn $ ksh<\/p>\n<p>Cshell billjoy % csh<\/p>\n<p>Bash born $ sh(or)bsh<\/p>\n<p>Zsh panel $ zsh<\/p>\n<p>Shell variables:<\/p>\n<p> [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4568"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4568"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4568\/revisions"}],"predecessor-version":[{"id":4569,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4568\/revisions\/4569"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}