Sunday, May 18, 2014

Unix Shell function

1. Function in the same file
 #! /bin/bash  

 testFunc_1()  
 {  
   echo $1  
   echo Hello world  
   return 0  
 }  
 testFunc_2()  
 {  
   return 10  
 }  

 ##################Normal Call############################  

 testFunc_1 wow!   
 #output:  
 #wow!  
 #Hello world!  

 echo $? #output 0, since the "testFunc_1" just return 0 as the status  

 testFunc_2  
 echo $? #output 10, since the "testFunc_2" just return 10 as the status  

 ##################Command Substitution###################  
 echo "==================separator=================="  
 var=$(testFunc_1 wow!)  
 #this will output nothing, but variable "var" just contain all content  
 #outputted from the testFunc_1  
 #note: var doesn't save return status  

 echo $? #output 0, since testFunc_1 return status 0  
 echo $var #output: wow! Hello world  
 printf "%s\n" $var   
 #output:  
 #wow!  
 #Hello  
 #world  

 var=$(testFunc_2) #since testFunc_2 doesn't output anything there, so  
 #$var will be empty. Note again: $var doesn't save return status in  
 #command substitution  

 echo $? #output 10, since testFunc_2 return the status 10  
 #This indicate that command substitution also output the command's return status  
 # to $?  
 echo $var #output: empty line  
 printf "%s\n" $var #output: empty line  

 #############conditional expression###################  

 echo "==================separator=================="  
 var=$(testFunc_1 wow!)  
 if [ -n "$var" ] #double quote is necessary here  
 then  
   echo "return string"  
 else  
   echo "return empty string"  
 fi  
 #$var is taken as the string value here, content is: wow! Hello world  
 #double quote is needed, otherwise, it will intepreted with:  
 # -n wow! Hello world, which make system complain too many arguments  
 #for test command.  

 if $var #error, this will complain, "wow!: command not found"  
 then  
   echo "return success status"  
 else  
   echo "return failure status"  
 fi  
 #again, $var just contain string from the function, and it is just interpreted  
 #as string here, making system think "command not found"  

 if testFunc_1 wow!  
 then  
   echo "return success status"  
 else  
   echo "return failure status"  
 fi  
 #output:  
 #wow!  
 #Hello world  
 #return success status  
 #  
 #first two lines are from the testFunc_1  
 #last line is from caller, since testFunc_1 return 0 as the status code  

 if testFunc_2  
 then  
   echo "return success status"  
 else  
   echo "return failure status"  
 fi  
 #output: return failure status  

 #Above examples indicate that: after command substitution to save function  
 #result into a variable, the meaning totally changed. The variable doesn't  
 #save return code of function, instead, it save the output from the function  
 #as the string, and whenever the user used that variable at other statements,  
 #it is just interpreted as string.  

2. Function and Variables:
./script_1:
 #! /bin/bash  

 testFunc_3()  
 {  
   var="Hello amazing world!"  
   return 0  
 }  
 testFunc_1()  
 {  
   echo "script_1's testFunc_1()!"  
   return 0  
 }  

./script_2:
 #! /bin/bash  

 testFunc_1()  
 {  
   echo $#  
   echo $0 $1 $2  
   return 0  
 }  

 testFunc_1 Hello world!  
 #output:  
 #2  
 #./script_2 Hello world!  

 echo $#  
 echo $0 $1 $2  
 #output:  
 #2  
 #./script_2 good morning!  

 #above examples indicated that: when entering the function scope  
 # "$#", $0, $1, $2...all these variables content are replaced  
 #temporarily, when leaving scope, it is restored to script's ones  

 ##################Other Variables#########################  

 var="Hello world!"  
 testFunc_2()  
 {  
   var="Amazing world!"  
   return 0  
 }  

 echo $var  
 #output "Hello world!"  

 testFunc_2  
 echo $var  
 #output "Amazing world!"  
 #  
 #This indicate that: in script, there is only one variable entity   
 #for variables with same name, there is no concept about global variables  
 #or local ones. So we should be careful with hanlding variables by functions  

 ######################Functions in other files##########################  
 source ./script_1  
 testFunc_3  
 echo $var  
 #output "Hello amazing world!"  
 #  
 #we can use "source" command to "inject" another script file here, and then   
 #use other script's function as local function  

 testFunc_1  
 #output "script_1's testFunc_1()!"  
 #  
 #This indicate that if there is another function with same name as previous one  
 #it will just replace the previous one. And any calling this function will be  
 #directed to the new function   

No comments:

Post a Comment