Saturday, May 10, 2014

Unix Shell Variables(3)

1. Positional Parameters

We can refer to current script's parameter using $num, if num is more than 1 digit, we have to use the format ${num}
script:
 #! /bin/bash  

 echo $1, $2, ${10}  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 1 2 3 4 5 6 7 8 9 10  
 1, 2, 10  

2. Positional Parameters Special Variables:
script:
 #! /bin/bash  

 echo $# #output the number of parameters  
 echo $* #output all parameters  
 echo $@ #output all parameters  
 echo "$*" #output all parameters, as one string  
 echo "$@" #output all parameters, as separate string  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 1 2 3 4 5  
 5  
 1 2 3 4 5  
 1 2 3 4 5  
 1 2 3 4 5  
 1 2 3 4 5  

===================================

script:
 #! /bin/bash  
 for i in $*  
 do echo $i  
 done  
 #output:  
 #1  
 #2  
 #3  
 #4  
 echo  
 for i in $@  
 do echo $i  
 done  
 #output:  
 #1  
 #2  
 #3  
 #4  
 echo  
 for i in "$*"  
 do echo $i  
 done  
 #output:  
 #1 2 3 4  
 echo  
 for i in "$@"  
 do echo $i  
 done  
 #output:  
 #1  
 #2 3  
 #4  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 1 "2 3" 4  
 1  
 2  
 3  
 4  

 1  
 2  
 3  
 4  

 1 2 3 4  

 1  
 2 3  
 4  


3. Positional Parameters Shift
"shift" get rid of the first positional parameter and shift over the remaining parameters. After shift is used, all related parameters are updated, including $#, $@, $*.

script:
 #! /bin/bash  

 echo $# #output the number of parameters  
 echo $1, $2, $3, $4  
 echo $*  
 shift  
 echo $# #output the number of parameters  
 echo $1, $2, $3, $4  
 echo $*  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 1 2 3 4  
 4  
 1, 2, 3, 4  
 1 2 3 4  
 3  
 2, 3, 4,  
 2 3 4  

4. All special variables
script:
 #! /bin/bash  

 echo $# #number of parameters  
 echo $@ #all arguments  
 echo $* #all arguments  
 echo $- #options given to the script  
 echo $? #exit status of previous command  
 echo $$ #current process Id   
 echo $0 #name of the current process  
 echo $! #last background process id  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script --s 8 9 10  
 4  
 --s 8 9 10  
 --s 8 9 10  
 hB  
 0  
 3426  
 ./script  


5. Arithmetic Expressions
 #! /bin/bash  
 echo $((5+5)) #output 10  
 echo $(($1+$2)) #output 7  
 i=10  
 echo $((i++)), $i #output 10, 11  
 i=10  
 echo $((++i)), $i #output 11, 11  
 i=10  
 echo $((i+=2)), $i #output 12, 12  
 echo $(($1+=2)) #error, $1 is replaced with 3 here, 3+=2 is illegal  

Note For Arithmetic Expressions:
./script_1:
 #! /bin/bash  

 i=1  
 i=$((++i))  
 echo $i #output 2 here  

 $((++i))   
 # error: 3: command not found  
 # We have to prefix with "i=$((++i))" otherwise, i (3 here) will  
 # be taken as one command, which is invalid  
 echo $i #output 3 here  


6. Exit Status
In unix world, return code can be classified:
0: success
1 - 125: failure because of reasons varied from app to app
126: find the command but not having permission
127: can't find the command.

command "exit" can be used to return the code in current script:

 #! /bin/bash  

 ls  
 echo $? # return 0, means success  
 ls Hello # output: ls: cannot access Hello: No such file or directory  
 echo $? # return 2, means failure  
 ./Hello #output: ./script: line 7: ./Hello: No such file or directory  
 echo $? # output: 127, command not found  
 ./world #output: ./script: line 9: ./world: Permission denied  
 echo $? # output: 126, command found but not having permission to run  
 exit 0

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script  
 script    script~ script_2 text text~    world  
 0  
 ls: cannot access Hello: No such file or directory  
 2  
 ./script: line 7: ./Hello: No such file or directory  
 127  
 ./script: line 9: ./world: Permission denied  
 126  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $?  
 0  

the reason "echo $?" outputs 0 in the terminal is because: ./script has "exit 0" in the end, which determines the exit status of ./script.

No comments:

Post a Comment