shell基本知识 - 控制流与函数

来源:互联网 发布:办公网络方案 编辑:程序博客网 时间:2024/05/16 06:00

控制流

if-then-else-elif-fi语句

if [ -d aaa ]; then   mv aaa bbbelif [ -d bbb ]; then   mv bbb aaafi
if [ $expectted -ge 1 ]; then    echo "\"kill -$signo $pid\" (expectted: success)" >> $LOGFILEelse    echo "\"kill -$signo $pid\" (expectted: fail)" >> $LOGFILEfi

case语句

语法:

case $1 in    opt)        ...    ;; #以双分好结束    opt1)        ...    ;;    *) #习惯最后加这个,默认        ...    ;;esac

举例:

case "$opt" inh)    echo "Usage:"    echo "h        help"    echo "t x      time in seconds for each test-case"    exit 0;;t) test_time=$OPTARG ;;*)    tst_brkm TBROK "unknown option: $opt";;esac

for

语法:

for i in *.*do    statementsdone

举例:

for p in $pidsdo      echo "Process $p exit with some value at time "done

while语句

语法:

while condition until conditiondo    statements done 

举例:

while getopts :ht: opt; do    case "$opt" in    h)        echo "Usage:"        echo "h        help"        echo "t x      time in seconds for each test-case"        exit 0    ;;    t) test_time=$OPTARG ;;    *)        tst_brkm TBROK "unknown option: $opt"    ;;    esacdone

break和continue语句

break:退出循环continue:继续下一次循环执行

函数

函数定义

fun(){ #定义函数
……
}

函数用法

fun “a” “b”#调用函数

0 0
原创粉丝点击