linux-shell編程12:shell腳本控制語句

控制語句應用

shell支持的控制語句有break,continue,exit,shift
  • shift

    shift的作用是將位置參數參數左移一位,每執行一次shift,$2將變爲$1,依次類推
    [root@server0 programe]# chmod u+x shift.sh
    [root@server0 programe]# ./shift
    .sh one two three four five six seven eight
    one
    two
    three
    four
    five
    six
    seven
    eight
    [root@server0 programe]# cat shift_.sh
    #!/bin/bash
    for i in $@
    do
    echo $1
    shift
    done
    [root@server0 programe]#

  • continue用來在for,while,until循環中使用當前循環中斷執行,進入下一次循環
  • break,終止整個for,while,until循環
  • exit用來結束腳本的運行

    [root@server0 programe]# cat con_p.sh
    #!/bin/bash
    for num in {1..100}
    do
    case $num in
    1)
    continue
    ;;
    5)
    break
    ;;
    esac
    echo ${num}
    done
    sleep 5
    exit
    echo "……………………………………………………"
    [root@server0 programe]#

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章