jzztf / wiki

wiki
https://jzztf.github.io
Other
1 stars 0 forks source link

linux-function #10

Open jzztf opened 6 years ago

jzztf commented 6 years ago

linux函数

if

语法:

if command
then
    command
fi

变体

if command;then
    command
fi
if command
then
    command
else
    command
fi
if command
then
    command
else
    if command
    then
        command
    fi
fi
if command
then
    command
elif command
then
    command
fi

判断

if test [condition]
then
    command
fi
if [ condition ]
then
    command
fi

三种判断

复合条件测试

if-then高级特性

if ((expression))
then
    command
fi
if [[ expression ]]
then
    command
fi

case

case 为每个变量可能会有不同的值提供选项

case variable in
    pattern1) command;;
    pattern2 | pattern3) command;;
    *) command;;
esac  # case 翻过来

for

用于遍历列表

语法:

for var in list
do
    command
done

变体

for var in list; do command
done

while

while命令看起来像是if-then和for的结合体,要进行条件测试,然后进行循环。

语法:

while test command
do
    command
done

中断循环

break命令直接退出循环

continue命令暂时退出循环,一旦遇到合适的条件会继续下去

参考:


**[↑ TOP](#if)**