Saturday, May 10, 2014

Unix Shell Variables(2)

1. Variable Expansion Operators
${varname:-word}:
 #! /bin/bash  
 #If var1 is defined, return its value, otherwise, return "Hello world!"  
 echo ${var1:-"Hello world!"}  
 # output "Hello world!"  

 #var1 is not defined  
 echo $var1  
 # output empty line  

${varname:=word}:
 #! /bin/bash  
 #If var1 is defined, return its value, otherwise, define it, assign value  
 #"Hello world!", and then return the value  
 echo ${var2:="Hello world!"}  
 # output "Hello world!"  

 #var1 is already defined.  
 echo $var2  
 # output "Hello world!"  

${varname:?message}:
 #! /bin/bash  

 #${var3:?word} is used to test if var3 is defined. If defined, return its value,  
 #otherwise, output word then exit the program. If word is null/empty here, then  
 #its output varies from shell to shell.  
 echo ${var3:?}  
 #output "./script: line 3: var3: parameter null or not set", then program exit here  

 #! /bin/bash  

 echo ${var3:?"Not defined!"}  
 #output "./script: line 3: var3: Not defined!", then program exit here  

 #! /bin/bash  

 var3="Hello world!"  
 echo ${var3:?"Not defined!"}  
 # output "Hello world!"  

${varname:+word}:
 #! /bin/bash  
 #${var4:+word}: if var4 exists and isn't null, it will return word  
 #otherwise, return null  
 echo ${var4:+"var4 exists"}  
 #output: Empty line. since var4 is not defined.  

 echo $var4  
 #output: Empty line. since var4 is not defined.  

 var5="Hello world!"  
 echo ${var5:+"var5 exists"}  
 #output: "var5 exists"  

Note: if colon ":" is omitted in above expressions, then it only check if variable is defined, not checking if it is null or not.

2. Pattern-matching operators
${var#pattern}
 #! /bin/bash  
 var=Helloworld  
 #${var#pattern} will check the pattern in $var value "from the beginning",   
 #if there is the pattern in the beginning, it will remove it, and then   
 #return the rest of value  
 echo ${var#Hello} #remove "Hello" at the beginning  
 #output "world"  

 echo ${var#world} #remove nothing since world is not at the beginning  
 #output "Helloworld"  

 echo ${var#H[a-z]*o} #remove Hello, since it matches "H[a-z]*o"  
 #output "world"  

 echo $var #var is not changed, it keeps the original value  
 #output "Helloworld"  

${var##pattern}
 #! /bin/bash  

 var=Helloworld  

 #${var##pattern} is very similar from ${var#pattern}, the only difference is:  
 #${var##pattern} is looking for the longest matching pattern from the beginning  
 #${var#pattern} is looking for the shortest matching pattern from the beginning  

 echo ${var#H[a-z]*o} #remove "Hello" at the beginning  
 #output "world"  

 echo ${var##H[a-z]*o} #remove the "Hellowo" at the beginning  
 #output "rld"  

${var%pattern}
 #! /bin/bash  

 var=Helloworld  

 #${var%o[a-z]*d} is looking for the shortest pattern from the end of variable value  
 #If found the value, it will just remove the tail part and return the beginning part  

 echo ${var%world}  
 #output "Hello"  

 echo ${var%o[a-z]*d}  
 #output "Hellow"  

${var%%pattern}
 #! /bin/bash  

 var=Helloworld  

 #${var%%o[a-z]*d} is very similar from the ${var%o[a-z]*d}, the only difference is:  
 #${var%%o[a-z]*d} is looking for the longest pattern from the end of text.  

 echo ${var%o[a-z]*d}  
 #output "Hellow"  

 echo ${var%%o[a-z]*d}  
 #output "Hell"  

3. Length of variable
 #! /bin/bash  

 var=Helloworld  
 echo ${#var}  
 #output 10  

 var=3  
 echo ${#var}  
 #output 1  

 unset var  
 echo ${#var}  
 #output 0  

No comments:

Post a Comment