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

流程控制语句

三、select/in[较少用]

格式:

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

示例-select.sh

[python]  view plain copy
  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 plain copy
  1. case 变量 in   
  2. 字符串1)   
  3.     命令列表1   
  4.     ;;   
  5.     ...   
  6. 字符串n)   
  7.     命令列表n   
  8.     ;;   
  9. esac  

示例-case.sh

[python]  view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  1. while 条件    #无限:while true  
  2. do   
  3.     命令   
  4. done   

示例-while.sh

[python]  view plain copy
  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 plain copy
  1. #!/bin/bash  
  2. # A shell script to add user(s)  
  3. #echo 123456 | passwd --stdin xiaofang  #用非交互方式设置xiaofang的密码  
  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 plain copy
  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 plain copy
  1.     until 条件   
  2.     do   
  3.         命令   
  4.     done   
  5. #until类似while循环,不同的是until是条件返回值为假时才继续执行。  

示例-until.sh

[python]  view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  1. 函数名 ()   
  2. {   
  3.     命令序列   
  4. }   


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

函数名 参数参数2 ... 参数n

实例-调用

 

 

三、函数中的变量

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

 

四、函数中的参数:

调用函数时,可以传递参数,在函数中用$1$2...来引用 

 

示例-function.sh

[python]  view plain copy
  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 plain copy
  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  
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值