Shell编程入门(第二版)(下)

流程控制语句

三、select/in[较少用]

格式:

[python] view plaincopy
  1.     select [变量] in [关键字]  
  2.     do   
  3.         command 1   
  4.         ... ...   
  5.         command n   
  6.     done   
  7. #select把关键字中的每一项做成相似表单,以交互的方式运行do和done之间的命令  

演示样例-select.sh

[python] view plaincopy
  1. #!/bin/bash  
  2. # Show select usage  
  3.   
  4. echo "What's your favorate OS?"  
  5.   
  6. select var in "Linux" "Windows" "UNIX" "Other"  
  7. do  
  8.     break  
  9. done  
  10.   
  11. echo "You have selected $var"  



四、case/esac

格式:

[python] view plaincopy
  1. case 变量 in   
  2. 字符串1)   
  3.     命令列表1   
  4.     ;;   
  5.     ...   
  6. 字符串n)   
  7.     命令列表n   
  8.     ;;   
  9. esac  

演示样例-case.sh

[python] view plaincopy
  1. #!/bin/bash  
  2. # Show Usage for case/esac  
  3.   
  4. echo "*********************************"  
  5. echo "Please select a oprator as below:"  
  6. echo "C ... copy"  
  7. echo "D ... delete"  
  8. echo "B ... backup"  
  9. echo "*********************************"  
  10.   
  11. read op  
  12. case $op in  
  13.     C)    
  14.         echo "copy...."  
  15.         ;;    
  16.     D)    
  17.         echo "delete...."  
  18.         ;;    
  19.     B)    
  20.         echo "backup..."  
  21.         ;;  
  22.     *)  
  23.         echo "Unknow operator!"  
  24.         exit 1  
  25. esac  

演示样例-select.case

[python] view plaincopy
  1. #!/bin/bash  
  2. # A test shell script for select and case  
  3.   
  4. echo "a is 5, b is 3, please select your method"  
  5. a=5  
  6. b=3;  
  7.   
  8. select var in "a+b" "a-b" "a*b" "a/b"  
  9. do  
  10.     break  
  11. done  
  12.   
  13. case $var in  
  14.     "a+b")  
  15.         echo "a+b="`expr $a + $b `  
  16.         ;;    
  17.     "a-b")  
  18.         echo "a-b="`expr $a - $b`  
  19.         ;;    
  20.     "a*b")  
  21.         echo "a*b="`expr $a \* $b`  
  22.         ;;  
  23.     "a/b")  
  24.         echo "a/b="`expr $a / $b`  
  25.         ;;  
  26.     *)  
  27.         echo "input error..."  
  28.         exit 1  
  29. esac  

实例-/etc/rc.d/init.d/httpd部分源码

[python] view plaincopy
  1. # See how we were called.  
  2. case "$1" in  
  3.   start)  
  4.     start  
  5.     ;;  
  6.   stop)  
  7.     stop  
  8.     ;;  
  9.   status)  
  10.     ;;  
  11.   restart)  
  12.     stop  
  13.     start  
  14.     ;;    
  15.   condrestart|try-restart)  
  16.     if status -p ${pidfile} $httpd >&/dev/null; then  
  17.         stop  
  18.         start  
  19.     fi  
  20.     ;;  
  21.   force-reload|reload)  
  22.         reload  
  23.     ;;  
  24.   graceful|help|configtest|fullstatus)  
  25.     $apachectl $@  
  26.     RETVAL=$?  
  27.     ;;  
  28.   *)  
  29.     echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"  
  30.     RETVAL=2  
  31. esac  



五、while

格式:

[python] view plaincopy
  1. while 条件    #无限:while true  
  2. do   
  3.     命令   
  4. done   

演示样例-while.sh

[python] view plaincopy
  1. #!/bin/bash  
  2. # A usage for while  
  3.   
  4. num=1  
  5. while [ $num -le 10 ]  
  6. do  
  7.     echo $(expr $num \* $num)  
  8.     let num++  
  9. done  

演示样例-useradd.sh

[python] view plaincopy
  1. #!/bin/bash  
  2. # A shell script to add user(s)  
  3. #echo 123456 | passwd --stdin xiaofang  #用非交互方式设置xiaofang的password  
  4.   
  5. echo -n "Plese input the user name: "  
  6. read username  
  7. echo -n "Plese input the sum users: "  
  8. read sum  
  9.   
  10. num=1  
  11. while [ $num -le $sum ]  
  12. do  
  13.     /usr/sbin/useradd "$username$num"     
  14.     if [ $? -ne 0 ]   
  15.     then  
  16.         echo "user: $username already exists."  
  17.         exit 1  
  18.     fi    
  19.       
  20.     let num++  
  21. done  
  22.   
  23. echo -n "Please input the passwd for this users: "  
  24. read passwd  
  25.   
  26. i=1  
  27. while [ $i -le $sum ]  
  28. do  
  29.     echo $passwd | /usr/bin/passwd --stdin "$username$i"  
  30.     let i++  
  31. done  




演示样例-userdel.sh

[python] view plaincopy
  1. #!/bin/bash  
  2. # A shell script for delete user(s)  
  3.   
  4. echo -n "Please input the username: "  
  5. read username  
  6. echo -n "Please input the user number: "  
  7. read num   
  8.   
  9. i=1  
  10. while [ $i -le $num ]  
  11. do  
  12.     /usr/sbin/userdel -r $username$i  
  13.     if [ $? -ne 0 ]   
  14.     then  
  15.         echo "User: $username$i is not exists."  
  16.         let i++   
  17.         continue  
  18.     fi    
  19.   
  20.     let i++   
  21. done  

六、until

格式:

[python] view plaincopy
  1.     until 条件   
  2.     do   
  3.         命令   
  4.     done   
  5. #until相似while循环,不同的是until是条件返回值为假时才继续运行。  

演示样例-until.sh

[python] view plaincopy
  1. #!/bin/bash  
  2. # A script to show until usage.  
  3.   
  4. echo "Please input Y/y to stop..."  
  5. read input  
  6.   
  7. until [ "$input" = "Y" ] || [ "$input" = "y" ]  
  8. do  
  9.     echo "input error, input again!"  
  10.     read input  
  11. done  

七、跳出循环:breakcontinue 

break:跳出整个循环 

continue:跳过本次循环,进行下次循环

 

演示样例-break_continue.sh

[python] view plaincopy
  1. #!/bin/bash  
  2. # A test shell script for break&continue  
  3.   
  4. while true  
  5. do  
  6.     echo "*****************************"  
  7.     echo "Please have a select as blow:"  
  8.     echo "1 Copy"  
  9.     echo "2 Delete"  
  10.     echo "3 Backup"  
  11.     echo "4 Quit***********************"  
  12.     read op  
  13.   
  14.     case $op in  
  15.         "1")  
  16.             echo "$op is Copy"  
  17.             ;;    
  18.         "2")  
  19.             echo "$op is Delete"  
  20.             ;;    
  21.         "3")  
  22.             echo "$op is Backup"  
  23.             ;;  
  24.         "4")  
  25.             echo "Exit..."  
  26.             break  
  27.             ;;  
  28.         "*")  
  29.             echo "Invalide selectino, please select again..."  
  30.             continue  
  31.             ;;  
  32.     esac  
  33. done  

 

八、shift指令

參数左移,每运行一次,參数序列顺次左移一个位置,$#的值减1, 用于分别处理每一个參数,移出去的參数不再可用

 

演示样例-shift.sh

[python] view plaincopy
  1. #!/bin/bash  
  2. # A test shell script for shift  
  3.   
  4. if [ $# -lt 1 ]   
  5. then  
  6.     echo "No enough parameters"  
  7.     exit 1  
  8. fi  
  9.   
  10. num=0  
  11. while [ $# -gt 0 ]   
  12. do  
  13.     echo '$1 is '$1  
  14.     let num++  
  15.     shift  
  16. done  
  17.   
  18. echo $num  

函数应用

实例-/etc/rc.d/init.d/httpd中的start源码

 

一、函数的定义

[python] view plaincopy
  1. 函数名 ()   
  2. {   
  3.     命令序列   
  4. }   


二、函数的调用:不带() 

函数名 參数參数2 ... 參数n

实例-调用

 

 

三、函数中的变量

变量均为全局变量,没有局部变量

 

四、函数中的參数:

调用函数时,能够传递參数,在函数中用$1$2...来引用 

 

演示样例-function.sh

[python] view plaincopy
  1. #!/bin/bash  
  2. # A test shell script for function  
  3.   
  4. # function  
  5. Help(){  
  6.     echo "Usage: sh function \$1 \$2 \$3"  
  7. }  
  8.   
  9. Display(){  
  10.     echo "three argument: $1 $2 $3"  
  11. }  
  12.   
  13. # main  
  14. if [ $# -ne 3 ]   
  15. then  
  16.     Help  
  17. else  
  18.     echo "Think you for your input"  
  19.     Display $1 $2 $3  
  20. fi  

Shell 脚本调试 

sh -x script  这将运行该脚本并显示全部变量的值。 

sh -n script  不运行脚本仅仅是检查语法的模式,将返回全部语法错误。 

 

最佳实践-命令最好使用绝对路径

 

一个脚本能够运行-

1.对脚本有rx权限,仅仅有r,能够使用sh运行

2.对脚本所在文件夹至少有rx权限

 

拓展实例-setuid.sh

[python] view plaincopy
  1. #!/bin/bash  
  2. # After the system installed, please check setuid files first for security  
  3. # mkdir /backup  
  4. # find / -perm -4000 -o -perm -2000 > /backup/setuid.list  
  5.   
  6. /bin/find / -perm -4000 -o -perm -2000 > /tmp/setuid.list 2> /dev/null  
  7.   
  8. for var in `/bin/cat /tmp/setuid.list`  
  9. do  
  10.     /bin/grep $var /backup/setuid.list > /dev/null 2> /dev/null  
  11.     if [ $? -ne 0 ]   
  12.     then  
  13.         echo "$var is not in /backup/setuid.list, It's danger!"  
  14.     fi    
  15. done  
posted @ 2017-06-30 16:15  yxysuanfa  阅读(217)  评论(0编辑  收藏  举报