Sunday, May 25, 2014

Unix Shell Built-in Commands(3)

1. set:
script_1:
 #! /bin/bash  

 #without any option, set will output all shell variables in current environment  
 set  
 #output:  
 #BASH=/bin/bash  
 #BASHOPTS=cmdhist:complete_fullquote:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath  
 #BASH_ALIASES=()  
 #BASH_ARGC=()  
 #BASH_ARGV=()  
 #......  

 #with only -o option, it will output all shell option's settings in current environment  
 set -o  
 #output:  
 #allexport       off  
 #braceexpand      on  
 #emacs         off  
 #errexit        off  
 #errtrace        off  
 #functrace       off  
 #hashall        on  
 #histexpand       off  
 #history        off  
 #ignoreeof       off  
 #interactive-comments    on  
 #keyword        off  
 #monitor        off  
 #noclobber       off  
 #noexec         off  
 #noglob         off  
 #nolog         off  
 #notify         off  
 #nounset        off  
 #onecmd         off  
 #physical        off  
 #pipefail        off  
 #posix         off  
 #privileged       off  
 #verbose        off  
 #vi           off  
 #xtrace         off  
 #with only +o option, it will output all shell option's settings in current environment  
 #in a different compared to "-o" option.  
 #-o means "enable", +o means disable  

 set +o  
 #output:  
 #set +o allexport  
 #set -o braceexpand  
 #set +o emacs  
 #set +o errexit  
 #set +o errtrace  
 #set +o functrace  
 #set -o hashall  
 #set +o histexpand  
 #set +o history  
 #set +o ignoreeof  
 #set -o interactive-comments  
 #set +o keyword  
 #set +o monitor  
 #set +o noclobber  
 #set +o noexec  
 #set +o noglob  
 #set +o nolog  
 #set +o notify  
 #set +o nounset  
 #set +o onecmd  
 #set +o physical  
 #set +o pipefail  
 #set +o posix  
 #set +o privileged  
 #set +o verbose  
 #set +o vi  
 #set +o xtrace  

2. Enable or Disable shell option
terminal:
-o will enable emacs shell option
+o will disable emacs shell option

 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ set -o emacs  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ set -o | grep emacs  
 emacs         on  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ set +o emacs  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ set -o | grep emacs  
 emacs         off  

3. $- : currently enabled shell options
terminal:
After disabling "C" option, $- lack C inside.
 
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ set -C
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $-  
 himBCH  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ set +C  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $-  
 himBH  

-C is equal to -o noclobber:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ set -o noclobber  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $-  
 himBCH  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ set +o noclobber  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $-  
 himBH  

each long form shell option(allexport, braceexpand, emacs, etc.) has a corresponding short form shell option(noclobber mapped to C)

4. Change positional parameters
./script_1:
 #! /bin/bash  

 echo $#  
 echo $1 $2  

 # use "--" to end option then change the positional parameter  
 set -- Hello amazing world  

 echo $#  
 echo $1 $2 $3  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 my god  
 2  
 my god  
 3  
 Hello amazing world  

No comments:

Post a Comment