函数定义
语法一:
函数名 () {
代码块
return N
}
语法二:
function 函数名 {
代码块
return N
}
创建与执行函数
创建
执行
流程控制
if
if [ condition ] #condition 值为true or false
then #条件为真的时候执行
commands #代码块 一行或者多行代码
fi #语句结束
if else
if [ condition ]
then #条件为真
commands1 #真 要执行代码块
else #条件为假
commands2 #假 要执行的代码块
fi 结束
if elif else
if [ condition 1] 满足第一个条件
then 真
command1 执行command1代码块
elif [ condition 2] 满足第二个条件
then 真
commands2 执行command2代码块
else 如果条件都不满足
commandsX 执行commandX代码块
fi 结束判断
if简写
if [ !test1 ]
then
echo 'test1 is null'
fi
简写为
[!test1 ] && echo 'test1 is null'
if [! test2 ]
then
echo 'test2 is null'
else
echo 'test2!!'
fi
简写为
[!test2] && echo 'test2 is null' || echo 'test2!!'