Friday, May 16, 2014

Unix Shell: case and looping

1. case statement:
script_1:
 #! /bin/bash  

 case $1 in  
 -a)  
   echo "option is -a"  
   ;;  
 -b | --boy)  
   echo "option is -b"  
   ;;  
 c | cat)  
   echo "option is c or cat"  
   ;;  
 1 | 3 | 5 | 7 | 9)  
   echo "option is odd number"  
   ;;  
 *)  
   echo "unknown option"  
   ;;  
 esac  

Note: keyword "esac" is very essential to end the "case statement"

terminal:
1) fourth command: the script only check first parameter, so it only recognize -b
2) fifth command: since the script only recognize "cat", not "--cat", so it output "unknown option"
3) seventh command: any number appear in the list [1, 3, 5, 7, 9] will be taken as odd number and output as following.
 
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 --boy  
 option is -b  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -a  
 option is -a  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -b  
 option is -b  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 -b -c  
 option is -b  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 --cat  
 unknown option  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 cat  
 option is c or cat  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 5  
 option is odd number  

2. for loop
text:
 Hello, New York  
 Hello, Chicago  
 Hello, Los Angeles  

script_1:
 #! /bin/bash  

 text=`cat text`  

 for i in $text  
 do  
   echo $i  
   echo $i | sed -ne 's/Chicago/&, the capital of middle/p'      
 done  

terminal:
Following output indicate that: "for i in $text" use "space" as default separator
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1  
 Hello,  
 New  
 York  
 Hello,  
 Chicago  
 Chicago, the capital of middle  
 Hello,  
 Los  
 Angeles  
=======================================
script_1:
 #! /bin/bash  
 #following code is iterating all parameters  
 for i  
 do  
   echo $i  
 done  

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

3. while loop
script_1:
Following script is trying to output each parameter and remove one by one.
 #! /bin/bash  
 while [ $# -ne 0 ]  
 do  
   echo $1  
   shift  
 done  

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

4. until loop
script_1:
The same as above "while loop" script the only difference is the condition is reversed, from "$# -ne 0" to "$# -eq 0"
 #! /bin/bash  
 until [ $# -eq 0 ]  
 do  
   echo $1  
   shift  
 done  


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

5. continue and break
./script_1:
 #! /bin/bash  

 while true  
 do  
   if [ $# -eq 0 ]  
   then   
     break  
   fi  
   echo $1  
   shift  
 done  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 2 3 4 5  
 2  
 3  
 4  
 5  
======================================
./script_1:
 #! /bin/bash  
 #default "break" only leave current loop scope, break 2 leave "one more scope"  
 while true  
 do  
   while true  
   do  
     if [ $# -eq 0 ]  
     then   
       break 2 # it leave the outer-most loop scope  
     fi  
     echo $1  
     shift  
   done  
 done  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 2 3 4 5  
 2  
 3  
 4  
 5  
=======================================
./script_1:
 #! /bin/bash  

 #continue just restart the loop from the very beginning and ignore  
 #the remaining part  
 while true  
 do  
   if [ $# -ne 0 ]  
   then   
     echo $1  
     shift  
     continue  
   fi  
   break  
 done  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 2 3 4 5  
 2  
 3  
 4  
 5  
========================================
./script_1:
 #! /bin/bash  

 #continue by default just restart from the beginning of current scope  
 #continue 2 restart from the one more outer level. Following script is trying  
 # to restart from the outer level loop, whenever there are still some parameters  
 #When program run out of parameters, it break from the inner loop, then right after  
 #immediately, break from outer loop  
 while true  
 do   
   echo "start the outer loop!"  
   while true  
   do  
     if [ $# -ne 0 ]  
     then   
       echo $1  
       shift  
       continue 2  
     fi  
     break  
   done  
   break  
 done  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 2 3 4 5  
 start the outer loop!  
 2  
 start the outer loop!  
 3  
 start the outer loop!  
 4  
 start the outer loop!  
 5  
 start the outer loop!  

No comments:

Post a Comment