{"id":221,"date":"2010-04-23T10:57:31","date_gmt":"2010-04-23T02:57:31","guid":{"rendered":"http:\/\/www.sysadmindayph.com\/blog\/?p=221"},"modified":"2010-04-23T10:57:31","modified_gmt":"2010-04-23T02:57:31","slug":"ksh-script-basics-special-shell-variables","status":"publish","type":"post","link":"http:\/\/www.sysadmindayph.com\/blog\/ksh-script-basics-special-shell-variables\/","title":{"rendered":"KSH Script Basics | Special Shell Variables"},"content":{"rendered":"<p>Just deleted tons of stale, unsused user accounts. Here are some KSH shell basics and special shell variable. I need to Google them up myself to get reminded that &#8216;$?&#8217; is the output variable for script execution. That is, it&#8217;s 0 for successful execution and 1 if there is any error.<\/p>\n<p><!--more--><\/p>\n<p>Special shell variables<br \/>\nThere are some variables which are set internally by the shell and which are available to the user:<\/p>\n<p>Name          Description<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n$1 &#8211; $9       these variables are the positional parameters.<\/p>\n<p>$0            the name of the command currently being executed.<\/p>\n<p>$#            the number of positional arguments given to this<br \/>\n              invocation of the shell.<\/p>\n<p>$?            the exit status of the last command executed is<br \/>\n              given as a decimal string.  When a command<br \/>\n              completes successfully, it returns the exit status<br \/>\n              of 0 (zero), otherwise it returns a non-zero exit<br \/>\n              status.<\/p>\n<p>$$            the process number of this shell &#8211; useful for<br \/>\n              including in filenames, to make them unique.<\/p>\n<p>$!            the process id of the last command run in<br \/>\n              the background.<\/p>\n<p>$-            the current options supplied to this invocation<br \/>\n              of the shell.<\/p>\n<p>$*            a string containing all the arguments to the<br \/>\n              shell, starting at $1.<\/p>\n<p>$@            same as above, except when quoted.<\/p>\n<p><strong>Branching<\/strong><br \/>\n<code>if then fi<br \/>\nif [[ $value -eq 7 ]];then<br \/>\n   print \"$value is 7\"<br \/>\nfi<\/code><br \/>\nor:<\/p>\n<p><code>if [[ $value -eq 7 ]]<br \/>\nthen<br \/>\n   print \"$value is 7\"<br \/>\nfi<\/code>or:<\/p>\n<p><code>if [[ $value -eq 7 ]];then print \"$value is 7\";fi<\/p>\n<p>if then else fi<br \/>\nif [[ $name = \"John\" ]];then<br \/>\n   print \"Your welcome, ${name}.\"<br \/>\nelse<br \/>\n   print \"Good bye, ${name}!\"<br \/>\nfi<\/code><\/p>\n<p><code>if then elif then else fi<br \/>\nif [[ $name = \"John\" ]];then<br \/>\n   print \"Your welcome, ${name}.\"<br \/>\nelif [[ $name = \"Hanna\" ]];then<br \/>\n   print \"Hello, ${name}, who are you?\"<br \/>\nelse<br \/>\n   print \"Good bye, ${name}!\"<br \/>\nfi<\/code><\/p>\n<p><code>case esac<br \/>\ncase $var in<br \/>\n   john|fred) print $invitation;;<br \/>\n   martin)    print $declination;;<br \/>\n   *)         print \"Wrong name...\";;<br \/>\nesac<\/code><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p><strong>Looping Command Syntax for Scripts<\/strong><\/p>\n<p><code>while do done<br \/>\nwhile [[ $count -gt 0 ]];do<br \/>\n   print \"\\$count is $count\"<br \/>\n   (( count -= 1 ))<br \/>\ndone<\/code><br \/>\n<code>until do done<br \/>\nuntil [[ $answer = \"yes\" ]];do<br \/>\n   print -n \"Please enter \\\"yes\\\": \"<br \/>\n   read answer<br \/>\n   print \"\"<br \/>\ndone<\/code><br \/>\n<code>for var in list do done<br \/>\nfor foo in $(ls);do<br \/>\n   if [[ -d $foo ]];then<br \/>\n      print \"$foo is a directory\"<br \/>\n   else<br \/>\n      print \"$foo is not a directory\"<br \/>\n   fi<br \/>\ndone<\/code><br \/>\ncontinue&#8230;break<br \/>\nOne can skip the rest of a loop and directly go to the next iteration with: &#8220;continue&#8221;. <\/p>\n<p><code>while read line<br \/>\ndo<br \/>\n   if [[ $line = *.gz ]];then<br \/>\n      continue<br \/>\n   else<br \/>\n      print $line<br \/>\n   fi<br \/>\ndone<\/code><br \/>\nOne can also prematurely leave a loop with: &#8220;break&#8221;. <\/p>\n<p><code>while read line;do<br \/>\n   if [[ $line = *!(.c) ]];then<br \/>\n      break<br \/>\n   else<br \/>\n      print $line<br \/>\n   fi<br \/>\ndone<\/code><\/p>\n<p>http:\/\/www.well.ox.ac.uk\/~johnb\/comp\/unix\/ksh.html<\/p>\n<p>Removing accounts from predefine list:<\/p>\n<p><code>#!\/bin\/ksh<\/p>\n<p>for each in `cat .\/list-a`<br \/>\ndo<br \/>\n  grep -i $each  \/etc\/passwd<br \/>\n    if [[ $? -eq 1 ]]; then<br \/>\n        echo \"$each does not exists.\"<br \/>\n        sleep 2<br \/>\n        continue;<br \/>\n    else<br \/>\n        echo \"$each exists\"<br \/>\n        echo \"Deleting account.. \"<br \/>\n        sleep 2<br \/>\n        userdel $each<br \/>\n        echo \"$each deleted\"<br \/>\n        sleep 5<br \/>\n     fi<\/p>\n<p>done<\/code><\/p>\n<p>Or course, the sleep there is just for aesthetic.. I want to see the screen<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just deleted tons of stale, unsused user accounts. Here are some KSH shell basics and special shell variable. I need to Google them up myself to get reminded that &#8216;$?&#8217; is the output variable for &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,212,17,5,3],"tags":[340,341,342,343,29,339],"class_list":["post-221","post","type-post","status-publish","format-standard","hentry","category-commands","category-script","category-scripting","category-solaris","category-unix","tag-if-else","tag-if-else-if","tag-ksh-shell","tag-ksh-shell-script","tag-shell-script","tag-special-shell-variable"],"_links":{"self":[{"href":"http:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts\/221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/comments?post=221"}],"version-history":[{"count":1,"href":"http:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"predecessor-version":[{"id":222,"href":"http:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/posts\/221\/revisions\/222"}],"wp:attachment":[{"href":"http:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sysadmindayph.com\/blog\/wp-json\/wp\/v2\/tags?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}