Sunday, May 11, 2014

Unix Shell Logical Operations

1. if-elif-else-fi

script:
 #! /bin/bash  

 var_1=${1:-0} # if not specified, assign 0 by default  
 var_2=${2:-0} # if not specified, assign 0 by default  

 #note: keyword "then" can't be ignored after "if" and "elif".  
 if [ $var_1 -gt $var_2 ] # if var_1 is greater than var_2  
 then  
   echo "$var_1 is max"  
 elif [ $var_1 -eq $var_2 ] # if var_1 is equal to var_2  
 then  
   echo "$var_1 = $var_2"  
 else  
   echo "$var_2 is max"  
 fi  

7 terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script  
 0 = 0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 1 3  
 3 is max  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 2 3  
 3 is max  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 12 3  
 12 is max  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 12   
 12 is max  

======================================
exit status of structure "if-elif-else-fi":

script:
This is trying to output the exit status of "if-elif-else-fi"
 #! /bin/bash  
 var1=${1:-0}  
 var2=${2:-0}  
 if [ $var1 -lt $var2 ]  
 then  
   echo "var2 is max"  
 elif [ $var1 -gt $var2 ]  
 then  
   : #do nothing  
 else  
   Hello    
 fi  
 echo $?  

terminal:
structure "if-elif-else-fi"'s exit status is the status of last command got executed inside the structure.
1) First Command, inside the "if-elif-else" structure, the last command got executed is "Hello", which is invalid, the and exit status of entire "if-elif-else" is the exit status of "Hello".
2) Second command, inside the "if-elif-else" structure, the last command got executed is "echo", whose exit status is 0. So the entire "if-elif-else"structure's exit status is also 0.
3) Third command, inside the "if-elif-else" structure, the last command got executed is the comparison operation after "elif", and then the exit status is 0, since the comparison succeeded.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 2 2  
 ./script: line 12: Hello: command not found  
 127  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 1 2  
 var2 is max  
 0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 2 1  
 0  

2. Logical Operations

world:
 #! /bin/bash  
 echo "Hello world!"  

script:
 #! /bin/bash  

 if ./world  
 then  
   echo $?  
   echo "world script can be executed"  
 else  
   echo $?  
   echo "world script can't be executed"  
 fi  

terminal:
The result of "./world" after keyword "if" does depend on the exit status of "./world". If the exit status is 0, it means success and then goes to the first block, outputting "world script can be executed"
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ chmod +x ./world  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script  
 Hello world!  
 0  
 world script can be executed  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ chmod -x ./world  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script  
 ./script: line 3: ./world: Permission denied  
 126  
 world script can't be executed  

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

text:
 Hello world!   
 Hello Hello my great world!  

script:
 #! /bin/bash  
 pattern=${1:-}  
 echo $pattern  
 if grep $pattern ./text >/dev/null  
 then  
   echo $?  
   echo "Found $pattern"  
 else  
   echo $?  
   echo "Failed to find $pattern"  
 fi  

terminal:
The result after keyword "if" does depend on the exit status of "grep". If exit status is 0, it means success and then goes to the first block outputting the "Found $pattern".
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script Hello  
 Hello  
 0  
 Found Hello  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script amazing  
 amazing  
 1  
 Failed to find amazing  

=======================================
Logical NOT:

script:
 #! /bin/bash  

 var1=${1:-0}  
 var2=${2:-0}  

 if [ ! $var1 -le 0 ]  
 then  
   echo "$var1 is positive"  
 else  
   echo "$var1 is negative or 0"  
 fi  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script -10  
 -10 is negative or 0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 10  
 10 is positive  
========================================
Logical And:

script:
 #! /bin/bash  

 var1=${1:-0}  

 #note: [] is another expression of "test command", we can't use expression like:  
 # [ ! $var -lt 0 && ! $var1 -gt 0 ], this is not ok to be converted to "test command"  
 # expression  

 #note: we have to put in a space between "!" and "$var1", otherwise ! is working on $var1  
 #with a space, ! is working on expression "$var1 -lt 0"  

 if [ ! $var1 -lt 0 ] && [ ! $var1 -gt 0 ]  
 then  
   echo "$var1 is 0"  
 else  
   echo "$var1 is non-zero"  
 fi  


terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 1  
 1 is non-zero  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 0  
 0 is 0  
=========================================
Logical OR:

script:
 #! /bin/bash  

 var1=${1:-0}  
 #note: with expression like:  
 #if $var1 - lt 0 || $var1 -gt 0, $var1 will be taken as one command, normally an invalid  
 #command, again, we need to convert expression to a test command format: [ $var1 -lt 0 ],  

 #we need to make sure that statements after "if" are "commands"  
 if [ $var1 -lt 0 ] || [ $var1 -gt 0 ]  
 then  
   echo "$var1 is non-zero"  
 else  
   echo "$var1 is zero"  
 fi  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 0  
 0 is zero  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script 2  
 2 is non-zero  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script -2  
 -2 is non-zero  

3. Short Circuit of Logical Operations
text:
 Hello world!   
 Hello Hello my great world!  

script_1:
 #! /bin/bash  

 pattern=${1:-}  

 grep $pattern ./text > /dev/null &&   
 {  
   cat ./text  
   echo $pattern found in text  
 }  

script_2:
 #! /bin/bash  

 pattern=${1:-}  

 if grep $pattern ./text >/dev/null  
 then  
   cat ./text  
   echo $pattern found in text  
 fi  

terminal:
for script_1, only when grep get the text successfully, the next part is then executed. So actually, script_1 is equal to script_2, but script_2 is more clear.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 Hello  
 Hello world!   
 Hello Hello my great world!  
 Hello found in text  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_2 Hello  
 Hello world!   
 Hello Hello my great world!  
 Hello found in text  

No comments:

Post a Comment