linux bash if中多个条件语句的写法
1. 先说说基本的if的写法
if..then..fi statement (Simple If)
if..then..else..fi statement (If-Else)
if..elif..else..fi statement (Else If ladder)
if..then..else..if..then..fi..fi..(Nested if)
if和then可以在同一行也可在分开的行,如果在统一样,那么要用分号分割例如
if [ ]; then
….
fi
或者
if []
then
….
fi
另外要注意的一点是条件表达式和方括号之间一定要有空格,否则会提示有语法错误。例如:
if [$1 = 0]
then
…
fi
这里的$1表示脚本的输入参数中第二个,比如你输入的是 1, 那么bash回提示 "[1"是不可识别的命令。
1. Bash If..then..fi statement
if [ conditional expression ]
then
statement1
statement2
.
fi
2. Bash If..then..else..fi statement
If [ conditional expression ]
then
statement1
statement2
.
else
statement3
statement4
.
fi
3. Bash If..elif..else..fi
If [ conditional expression1 ]
then
statement1
statement2
.
elif [ conditional expression2 ]
then
statement3
statement4
.
.
.
else
statement5
fi
4. Bash If..then..else..if..then..fi..fi..
If [ conditional expression1 ]
then
statement1
statement2
.
else
if [ conditional expression2 ]
then
statement3
.
fi
fi
2. if的条件表达式中有多个
if[cond1]&&[cond2]&&[cond3]then....
if[[cond1&&cond2&&cond3]]then....
if[cond1-acond2-acond3]then....
版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章转载自:IT夜班车,否则按侵权处理.
分享到: