• shell函数


    shell函数

    为什么要使用shell函数

    linux别的的作用

    1. [root@lamp ~]# alias
    2. alias cp='cp -i'
    3. alias l.='ls -d .* --color=auto'
    4. alias ll='ls -l --color=auto'
    5. alias ls='ls --color=auto'
    6. alias mv='mv -i'
    7. alias rm='rm -i'
    8. alias vi='vim'
    9. alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

    设置别名与使用

    1. [root@lamp ~]# alias cdone='cd /'
    2. [root@lamp ~]# alias
    3. alias cdone='cd /'
    4. alias cp='cp -i'
    5. alias l.='ls -d .* --color=auto'
    6. alias ll='ls -l --color=auto'
    7. alias ls='ls --color=auto'
    8. alias mv='mv -i'
    9. alias rm='rm -i'
    10. alias vi='vim'
    11. alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    12. [root@lamp ~]# cdone
    13. [root@lamp /]# pwd
    14. /

    函数也是具有和别名类似的功能。函数的作用就是把程序里多次调研相同的代码部分定义成一份,然后为这一份代码起个名字,其它所有的重复调用这部分代码都只用调用这个名字就可以。当需要修改这部分重复代码时,只需要改变函数体内的一份代码即可实现调用修改。

    使用函数的优势:

    1、把相同的程序段定义成函数,可以减少整个程序的代码量。

    2、增加程序的可读性,以及便于管理。

    3、可实现程序功能模块化,不同的程序使用函数模块化。

    4、让程序代码结构更清晰。

    shell函数语法

    语法格式:

    1. 简单语法格式
    2. 函数名(){
    3.   指令...
    4.   return n
    5. }
    6. 规范语法格式
    7. function 函数名(){
    8.   指令...
    9.   return n
    10. }

    注意:shell的返回值是exit,函数里用return输出返回值。

    shell函数的执行

    调用函数

    1)直接执行函数名即可(不带括号)。

    函数名

    注意:1、执行函数时,函数后的小括号不需要。2、函数定义及函数体必须在要执行的函数名的前面定义,shell的执行从上到下按行执行。

    2)带参数的函数执行方法。

    函数名 参数1 参数2

    注意:函数的传参和脚本的传参类似,只是脚本名换成函数名即可。

    函数后接的参数说明:1、shell的位置参数($1、$2、$3、$4、$5、$#、$*、$?、$@)都可以是函数的参数。2、此时父脚本的参数临时地被函数参数所掩盖或隐藏。3、$0比较特殊,它仍然是父脚本的名称。4、当函数完成时,原来的命令行脚本的参数即恢复。5、在shell函数里面,return命令功能与shell里的exit类似,作用是跳出函数。6、在shell函数体里使用exit会退出整个shell脚本,而不是退出shell函数。7、return语句会返回一个退出值(返回值)给调用函数的程序。8、函数的参数变量是在函数体里面定义,如果是普通变量,一般使用local i定义。

    shell函数范例

    开发脚本建立两个简单函数并调用执行。

    1. [root@lamp /]# cat fun01.sh
    2. #!/bin/bash
    3. test_fun(){
    4.   echo "i am shell fun."
    5. }
    6. test_fun
    7. [root@lamp /]# sh fun01.sh
    8. i am shell fun.

    调用其它脚本文件中的函数。

    1. [root@lamp /]# cat fun01.sh
    2. #!/bin/bash
    3. . /fun02.sh
    4. test_fun(){
    5.   echo "i am shell fun."
    6. }
    7. test_fun
    8. test_fun02
    9. [root@lamp /]# cat fun02.sh
    10. #!/bin/bash
    11. test_fun02(){
    12.   echo "i am shell fun02."
    13. }
    14. [root@lamp /]# sh fun01.sh
    15. i am shell fun.
    16. i am shell fun02.

    传参

    1. [root@lamp /]# cat fun01.sh
    2. #!/bin/bash
    3. . /fun02.sh
    4. test_fun(){
    5.   echo "i am shell fun."
    6. }
    7. test_fun
    8. test_fun02 $1
    9. [root@lamp /]# cat fun02.sh
    10. #!/bin/bash
    11. test_fun02(){
    12.   echo "i am shell $1."
    13. }
    14. [root@lamp /]# sh fun01.sh golden
    15. i am shell fun.
    16. i am shell golden.

    函数传参转成参数命令行传输,对任意指定url判断是否异常。

    1. [root@lamp /]# curl -I -m 3 -o /dev/null -s -w %{http_code} www.baidu.com

    -I 仅测试HTTP头

    -m 3 最多查询3秒

    -o /dev/null 屏蔽原有输出信息

    -s silent 模式,不输出任何东西

    -w %{http_code} 控制额外输出

    1. [root@lamp ~]# cat check_url.sh
    2. #!/bin/bash
    3. [ -f /etc/init.d/functions ]&& . /etc/init.d/functions
    4. usage(){
    5.   echo "USAGE:$0 url."
    6.   exit 1
    7. }
    8. RETVAL=0
    9. check(){
    10.   wget -I 10 --spider -t 2 $1 &>/dev/null
    11.   RETVAL=$?
    12.   if [ $RETVAL -eq 0 ];then
    13.     action "$1 url" /bin/true
    14.   else
    15.     action "$1 url" /bin/false
    16.   fi
    17.   return $RETVAL
    18. }
    19. main(){
    20.   if [ $# -ne 1 ];then
    21.     usage
    22.   fi
    23.   check $1
    24. }
    25. main $*
    26. [root@lamp ~]# sh check_url.sh www.baidu.com
    27. www.baidu.com url [ OK ]
    28. [root@lamp ~]# sh check_url.sh www.baiduxxxx.com
    29. www.baiduxxxx.com url [FAILED]

    给字符串加颜色。

    1. [root@lamp ~]# cat color.sh
    2. #!/bin/bash
    3. RED_COLOR='E[1;31m'
    4. GREEN_COLOR='E[1;32m'
    5. YELLOW_COLOR='E[1;33m'
    6. BLUE_COLOR='E[1;34m'
    7. PINK='E[1;35m'
    8. RES='E[0m'
    9. echo -e "$RED_COLOR red $RES"
    10. echo -e "$GREEN_COLOR GREEN $RES"
    11. echo -e "$YELLOW_COLOR YELLOW $RES"
    12. echo -e "$BLUE_COLOR BLUE $RES"
    13. echo -e "$PINK PINK $RES"

    输出结果。

    传2个参数,颜色名称和内容,输出带颜色的内容。

    1. [root@lamp ~]# cat color_str.sh
    2. #!/bin/bash
    3. RED='E[1;31m'
    4. GREEN='E[1;32m'
    5. YELLOW='E[1;33m'
    6. BLUE='E[1;34m'
    7. PINK='E[1;35m'
    8. RES='E[0m'
    9. usage(){
    10.   echo "USAGE:$0 color contents."
    11.   exit 1
    12. }
    13. color(){
    14.   if [ "$1" = "red" ];then
    15.     echo -e "${RED}$2 $RES"
    16.   elif [ "$1" = "green" ];then
    17.     echo -e "${GREEN}$2 $RES"
    18.   elif [ $1 = "yellow" ];then
    19.     echo -e "${YELLOW}$2 $RES"
    20.   elif [ "$1" = "blue" ];then
    21.     echo -e "${BLUE}$2 $RES"
    22.   elif [ "$1" = "pink" ];then
    23.     echo -e "${PINK}$2 $RES"
    24.   else
    25.     echo "$2"
    26.   fi
    27. }
    28. main(){
    29.   if [ $# -ne 2 ];then
    30.     usage
    31.   fi
    32.   color $1 $2
    33. }
    34. main $*

    输出结果。

    case结构条件句

    case结构条件句语法

    case语句实际上就是规范的多分支if语句。

    1. case "字符串变量" in
    2.   值1) 指令1...
    3. ;;
    4.   值2) 指令2...
    5. ;;
    6.   *) 指令3...
    7. esac

    case结构条件句范例

    根据用户的输入判断是哪个数字。如果用户输入数字输出对应输入的数字,如果是其他内容返回不正确。

    1. [root@lamp ~]# cat case.sh
    2. #!/bin/bash
    3. usage(){
    4.   echo "USAGE:$0 number."
    5.   exit 1
    6. }
    7. case_fun(){
    8.   case $1 in
    9.     [1-3])
    10.       echo $1
    11.   ;;
    12.     *)
    13.       echo "input error."
    14.   esac
    15. }
    16. main(){
    17.   case $# in
    18.     1) case_fun $1
    19.   ;;
    20.     *) usage
    21.   esac
    22. }
    23. main $*

    输出结果。

    1. [root@lamp ~]# sh case.sh
    2. USAGE:case.sh number.
    3. [root@lamp ~]# sh case.sh 1
    4. 1
    5. [root@lamp ~]# sh case.sh 2
    6. 2
    7. [root@lamp ~]# sh case.sh 3
    8. 3
    9. [root@lamp ~]# sh case.sh 4
    10. input error.

    执行脚本打印一个水果菜单:

    1、apple

    2、pear

    3、banana

    4、cherry

    当用户选择水果的时候,打印选择水果是什么,并给水果单词加上颜色。

    1. [root@lamp ~]# cat fruit.sh
    2. #!/bin/bash
    3. RED='E[1;31m'
    4. GREEN='E[1;32m'
    5. YELLOW='E[1;33m'
    6. BLUE='E[1;34m'
    7. PINK='E[1;35m'
    8. RES='E[0m'
    9. FLICKER='E[31;5m'
    10. usage(){
    11.   echo -e "${FLICKER}Pls select the exist num behind. ${RES}"
    12.   exit 1
    13. }
    14. choice(){
    15.   case $num in
    16.     1) echo -e "${BLUE}apple${RES}"
    17.   ;;
    18.     2) echo -e "${GREEN}pear${RES}"
    19.   ;;
    20.     3) echo -e "${YELLOW}banana${RES}"
    21.   ;;
    22.     4) echo -e "${RED}cherry${RES}"
    23.   ;;
    24.     *) usage
    25.   esac
    26. }
    27. main(){
    28.   choice $num
    29. }
    30. echo "
    31.   1、apple
    32.   2、pear
    33.   3、banana
    34.   4、cherry"
    35. read -t 10 -p "Pls input a num:" num
    36. main $num

    echo输出字符串显示不同颜色。

    1. [root@lamp ~]# echo -e "33[30m 黑色 33[0m"
    2. [root@lamp ~]# echo -e "33[31m 红色 33[0m"
    3. [root@lamp ~]# echo -e "33[32m 绿色 33[0m"
    4. [root@lamp ~]# echo -e "33[33m 黄色 33[0m"
    5. [root@lamp ~]# echo -e "33[34m 蓝色 33[0m"
    6. [root@lamp ~]# echo -e "33[35m 紫色 33[0m"
    7. [root@lamp ~]# echo -e "33[36m 天蓝色 33[0m"
    8. [root@lamp ~]# echo -e "33[37m 白色 33[0m"

    1. [root@lamp ~]# echo -e "33[40;37m 黑底白字 33[0m"
    2. [root@lamp ~]# echo -e "33[41;37m 红底白字 33[0m"
    3. [root@lamp ~]# echo -e "33[42;37m 绿底白字 33[0m"
    4. [root@lamp ~]# echo -e "33[43;37m 黄底白字 33[0m"
    5. [root@lamp ~]# echo -e "33[44;37m 蓝底白字 33[0m"
    6. [root@lamp ~]# echo -e "33[45;37m 紫底白字 33[0m"
    7. [root@lamp ~]# echo -e "33[46;37m 天蓝底白字 33[0m"
    8. [root@lamp ~]# echo -e "33[47;30m 白底黑字 33[0m"

    更多文字颜色及背景色查看:man console_codes

    nginx启动脚本。

    1. [root@lamp ~]# cat nginx_control.sh
    2. #!/bin/bash
    3. [ -f /etc/init.d/functions ] && . /etc/init.d/functions
    4. nginx=/application/nginx/sbin/nginx
    5. profile=/application/nginx/logs/nginx.pid
    6. state() {
    7.   RETVAL=$?
    8.   if [ $RETVAL -eq 0 ];then
    9.     action "Nginx is $1" /bin/true
    10.   else
    11.     action "Nginx is $1" /bin/false
    12.   fi
    13.   return $RETVAL
    14. }
    15. start() {
    16.   if [ -f $pidfile ];then
    17.     echo "Nginx is running"
    18.   else
    19.     $nginx
    20.     RETVAL=$?
    21.     state started
    22.   fi
    23.   return $RETVAL
    24. }
    25. stop() {
    26.   if [ ! -f $pidfile ];then
    27.     echo "nginx is not running."
    28.   else
    29.     $nginx -s stop
    30.     state stoped
    31.   fi
    32.   return $RETVAL
    33. }
    34. reload() {
    35.   if [ -f $pidfile ];then
    36.     echo "Can't open $pidfile,no such file or directory."
    37.   else
    38.     $nginx -s reload
    39.     state reload
    40.   fi
    41.   return $RETVAL
    42. }
    43. case "$1" in
    44.   start)
    45.     start
    46.     RETVAL=$?
    47.     ;;
    48.   stop)
    49.     stop
    50.     RETVAL=$?
    51.     ;;
    52.   restart)
    53.     stop
    54.     sleep 2
    55.     restart
    56.     RETVAL=$?
    57.     ;;
    58.   reload)
    59.     reload
    60.     RETVAL=$?
    61.     ;;
    62.   *)
    63.     echo "USAGE:$0 {start|stop|reload|restart}"
    64. esac
    65. exit $RETVAL

    case总结:

    1、case语句就相当于多分支的if语句。case语句优势是更规范、易读。

    2、case语句适合变量的值少,且为固定的数字或字符串集合。(start、stop、restart)。

    3、系统服务启动脚本传参的判断多用case语句。

    当型循环和直到型循环

    while循环工作中使用的不多,一般是守护进程程序或始终循环执行场景,其它循环计算,都会用for替换while。

    当型和直到型循环语句

    1、while条件句

    语法:

    1. while 条件
    2.   do
    3.   指令 …
    4. done

    2、until条件句

    语法:

    1. until 条件
    2.   do
    3.   指令 ...
    4. done

    不管条件是否成立都会执行一次。

    当型和直到型基本范例

    每隔2秒屏幕输出负载值。

    1. [root@lamp ~]# cat while.sh
    2. #!/bin/bash
    3. while true
    4.   do
    5.     uptime
    6.     sleep 2
    7.   done

    防止客户端执行脚本中断的方法。

    1、sh while.sh &

    2、nohup while.sh &

    3、screen保持回话。

    ctl+c:停止执行当前脚本或任务

    ctrl+z:暂停执行当前脚本或任务

    bg:把当前脚本或任务放入后台执行

    fg:当前脚本或任务进行前台执行,如果有多个任务,可以fg加任务编号调出

    jobs:查看当前执行的脚本或任务

    进程管理命令

    bg:后台运行

    fg:挂起程序

    jobs:显示后台程序

    kill、killall、pkill:杀掉进程

    crontab:设置定时

    ps:查看进程

    pstree:显示进程状态树

    top:显示进程

    nice:改变优先权

    nohup:用户退出系统之后继续工作

    pgrep:查找匹配条件的进程

    strace:跟踪一个进程的系统调用情况(strace + pid)

    ltrace:跟踪进程调用库函数的情况

    vmstat:报告虚拟内存统计信息

    while实现1到100之和。

    1. [root@lamp ~]# cat while_add.sh
    2. #!/bin/bash
    3. i=1
    4. sum=0
    5. while [ $i -le 100 ]
    6.   do
    7.     ((sum+=$i))
    8.     ((i++))
    9.   done
    10. echo $sum

    注意:在工作中多使用算法,代码执行效率更高。

    使用while循环竖向打印10、9、8…

    1. [root@lamp ~]# cat while_seq.sh
    2. #!/bin/bash
    3. i=10
    4. while [ $i -gt 0 ]
    5.   do
    6.     echo "$i"
    7.     ((--i))
    8.   done

    手机充值10元,每发一次短信花费0.15元,当前余额小于0.15元不能发短信,提示余额不足,请充值,可以允许用户充值后继续发短信。

    1. [root@lamp ~]# cat messages.sh
    2. #!/bin/bash
    3. total=0
    4. menu='
    5. 1:充值
    6. 2:发送信息
    7. 3:退出
    8. '
    9. pay(){
    10.   read -t 10 -p "Pls input pay money:" num
    11.   expr ${num} + 1 &>/dev/null
    12.   [ $? -ne 0 ]&&{
    13.     echo "Input error"
    14.     return 1
    15.   }
    16.   if [ ${num} -gt 0 ];then
    17.     total=$(( $total + $num * 100 ))
    18.   else
    19.     echo "Input error"
    20.     return 1
    21.   fi
    22. }
    23.  
    24. send(){
    25.   if [ ${total} -gt 15 ];then
    26.     echo "send messages is ok."
    27.     total=$(( $total - 15 ))
    28.   else
    29.     echo "余额不足!"
    30.   fi
    31. }
    32.  
    33. while true
    34. do
    35.   echo "当前余额:${total}"
    36.   echo ${menu}
    37.   read -t 10 -p "Pls input a num:" choice
    38.   case ${choice} in
    39.     1) pay
    40.     ;;
    41.     2) send
    42.     ;;
    43.     3) exit 0
    44.     ;;
    45.     *)
    46.   esac
    47. done

    while按行读取文件的方式。

    方式一

    1. exec <FILE
    2. sum=0
    3. while read line
    4. do
    5.   cmd
    6. done

    方式二

    1. cat ${FILE_PATH} | while read line
    2. do
    3.   cmd
    4. done

    方式三

    1. while read line
    2. do
    3.   cmd
    4. done<FILE

    计算apache一天的日志中所有行的各元素的访问字节数的总和。

    1. [root@lamp ~]# cat log_cmd.sh
    2. #/bin/bash
    3. exec access.log
    4. while read line
    5. do
    6.   i=`echo $line|awk '{print $10}'`
    7.   expr $i + 1 &>/dev/null
    8.   if [ $? -ne 0 ];then
    9.     continue
    10.   fi
    11.   ((sum+=i))
    12. done
    13. [ -n "$sum" ] && echo $sum

    while循环小结。

    1、while循环的特长是执行守护进程以及我们希望循环不退出持续执行的场景,用频率小于1分钟的循环处理,其他的while循环几乎都可以被for循环替代。

    2、几乎所有的case语句都可以用if语句替换,一般在系统启动脚本传入少量固定规则字符串,用case语句,其他普通判断多用if。

    3、工作中if和for最常用,其次while(守护进程)和case(服务启动脚本)。

    读取一个文件,倒插入一个新的文件,清空原文件。

    1. [root@lamp ~]# cat read_line.sh
    2. #!/bin/bash
    3. file=/root/read.sh
    4. num=`wc -l ${file}|awk -F ' ' '{print $1}'`
    5. while [ ${num} -gt 0 ]
    6. do
    7.   line=`tail -1 ${file}`
    8.   echo ${line}
    9.   echo ${line}>>/root/read_bak.sh
    10.   sed -i '$d' ${file}
    11.   num=`wc -l ${file}|awk -F ' ' '{print $1}'`
    12. done

    for循环结构

    for循环结构语法

    1、for循环结构

    语法

    1. for 变量名 in 变量取值列表
    2. do
    3.   指令…
    4. done

    在此结构中"in 变量取值列表"可省略,省略时相当于in $@,使用for i就相当于使用for i in $@。

    2、C语言型for循环结构

    语法

    1. for((exp1;exp2;exp3))
    2. do
    3.   指令...
    4. done

    for和while对比。

    1. [root@lamp ~]# cat for.sh
    2. #!/bin/bash
    3. for((i=1;i<=5;i++))
    4. do
    5.   echo $i
    6. done
    7.  
    8. [root@lamp ~]# cat while.sh
    9. #!/bin/bash
    10. i=1
    11. while((i<=5))
    12. do
    13.   echo $i
    14.   ((i++))
    15. done

    方法一:直接列出元素的方法。

    1. [root@lamp ~]# cat for_02.sh
    2. #!/bin/bash
    3. for i in 1 2 3 4 5
    4. do
    5.   echo ssh 192.168.1.${i}
    6. done
    7. [root@lamp ~]# sh for_02.sh
    8. ssh 192.168.1.1
    9. ssh 192.168.1.2
    10. ssh 192.168.1.3
    11. ssh 192.168.1.4
    12. ssh 192.168.1.5

    方法二:使用大括号的方法

    1. [root@lamp ~]# cat for_02.sh
    2. #!/bin/bash
    3. for i in {1..5}
    4. do
    5.   echo ssh 192.168.1.${i}
    6. done
    7. [root@lamp ~]# sh for_02.sh
    8. ssh 192.168.1.1
    9. ssh 192.168.1.2
    10. ssh 192.168.1.3
    11. ssh 192.168.1.4
    12. ssh 192.168.1.5

    方法三:使用命令结果。

    1. [root@lamp ~]# cat for_03.sh
    2. #!/bin/bash
    3. for i in `seq 5`
    4. do
    5.   echo ssh 192.168.1.${i}
    6. done
    7. [root@lamp ~]# sh for_03.sh
    8. ssh 192.168.1.1
    9. ssh 192.168.1.2
    10. ssh 192.168.1.3
    11. ssh 192.168.1.4
    12. ssh 192.168.1.5

    四位一行显示的优化方法。

    1. [root@lamp ~]# seq 10 >a.log
    2. [root@lamp ~]# cat a.log
    3. 1
    4. 2
    5. 3
    6. 4
    7. 5
    8. 6
    9. 7
    10. 8
    11. 9
    12. 10
    13. [root@lamp ~]# cat a.log|xargs -n4
    14. 1 2 3 4
    15. 5 6 7 8
    16. 9 10
    17. [root@lamp ~]# xargs -n4 <a.log
    18. 1 2 3 4
    19. 5 6 7 8
    20. 9 10
    21. [root@lamp ~]# cat a.log|grep 3
    22. 3
    23. [root@lamp ~]# grep 3 a.log
    24. 3

    显示当前路径的所有文件。

    1. [root@lamp ~]# cat for_04.sh
    2. #!/bin/bash
    3. for i in `ls`
    4. do
    5.   echo $i
    6. done

    批量生成随机文件。

    1. [root@lamp ~]# cat for_05.sh
    2. #!/bin/bash
    3. for((i=1;i<=10;i++))
    4. do
    5.   mkdir -p ./test
    6.   touch ./test/`echo $RANDOM|md5sum|cut -c 1-8`_finished.jpg
    7. done

    批量改名。

    1. [root@lamp ~]# cat for_06.sh
    2. #!/bin/bash
    3. for i in `ls /root/test`
    4. do
    5.   cd /root/test
    6.   mv $i `echo $i|sed 's#_finished.jpg#.html#g'`
    7. done

     

    1. [root@lamp test]# ls|awk -F '[_]' '{print "mv " $0,$1".html"}'|bash

     

    1. [root@lamp test]# rename "_finished.html" ".jpg" *.html

    rename "改什么" "改成什么" 对谁进行修改

    排除关闭某服务开机自启动。

    1. [root@lamp test]# chkconfig --list|grep 3:on|awk '{print $1}'|egrep -v "rsyslog|sshd|systat"|sed -r 's#(.*)#chkconfig 1 off#g'|bash

    for实现1+2+3+…+100。

    1. [root@lamp ~]# cat for_07.sh
    2. #!/bin/bash
    3. for ((i=1;i<=100;i++))
    4. do
    5.   ((sum+=$i))
    6. done
    7. echo "sum=${sum}"

     

    1. [root@lamp ~]# cat for_08.sh
    2. #!/bin/bash
    3. for i in `seq 100`
    4. do
    5.   let sum+=i
    6. done
    7. echo "sum=${sum}"

    算法最快。

    1. [root@lamp ~]# echo $((100*(100+1)/2))

     

    chpasswd批量更新用户口令工具。

    建立指令文件passwd.txt(格式为[username:passwd])

    # vi passwd.txt

    username1:passwd1

    username2:passwd2

    将passwd.txt的内容传输到chpasswd指令来修改相应用户的密码

    # chpasswd < doiido.txt

    break continue exit return对比

    break、continue、exit一般用于循环结构中控制循环(for、while、if)的走向。

    break n:n表示跳出循环的层数,如果省略n表示跳出整个循环。

    continue n:n表示退到第n层继续循环,如果省略n表示跳过本次循环,忽略本次循环的剩余代码,进入循环的下一次循环。

    exit n:退出当前shell程序,n为返回值。n也可以省略,再下一个shell里通过$?接收这个n值。

    return n:用于在函数里,作为函数的返回值,用于判断函数执行是否正确。

    1. [root@lamp ~]# cat for_09.sh
    2. #!/bin/bash
    3. for((i=0;i<=5;i++))
    4. do
    5.   if [ $i -eq 3 ];then
    6.   #continue
    7.   #break
    8.   exit
    9.   fi
    10.   echo $i
    11. done
    12. echo "ok"

    contine生效

    1. [root@lamp ~]# sh for_09.sh
    2. 0
    3. 1
    4. 2
    5. 4
    6. 5
    7. ok

    break生效

    1. [root@lamp ~]# sh for_09.sh
    2. 0
    3. 1
    4. 2
    5. ok

    exit生效

    1. [root@lamp ~]# sh for_09.sh
    2. 0
    3. 1
    4. 2
  • 相关阅读:
    Postman的使用和测试
    Django报错 django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
    MySQL 修改字段类型或长度
    mysql导入导出sql文件
    Django 无法同步数据库model相应字段问题
    Django objects.all()、objects.get()与objects.filter()之间的区别介绍
    inconsistent use of tabs and spaces in indentation
    JavaScript 计时器
    JavaScript--编程题
    JavaScript--Array 数组对象
  • 原文地址:https://www.cnblogs.com/yinshoucheng-golden/p/9570986.html
Copyright © 2020-2023  润新知