linux shell 脚本中if语句的用法 在脚本中使用if if[! -w “$logfile” ] 为什 shell脚本中的if中多条件语句如何写。

\u5728linux\u4e0bshell\u811a\u672c\u4e2dif\u4e2d\u7528\u5230or\u600e\u4e48\u5199

linux\u4e0bshell\u811a\u672c\u7684\u903b\u8f91\u7684or\u7528\u8fd0\u7b97\u7b26 || \u8868\u793a\uff0cif\u4e2d\u7528\u5230or\u7684\u5199\u6cd5\u5b9e\u4f8b\u5982\u4e0b\uff1a
a=10
b=20
if [[ $a -lt 50 || $b -gt 50 ]]
then
echo "\u8fd4\u56de true"
else
echo "\u8fd4\u56de false"
fi
\u5176\u4e2d$a -lt 100\u8868\u793aa50\u4e3a\u5047\uff1b\u771f or \u5047\u4e3a\u771f\u3002
\u6240\u4ee5\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a\u8fd4\u56de true

\u6269\u5c55\u8d44\u6599
1\u3001shell\u4e2d\u6d41\u7a0b\u63a7\u5236if\u57fa\u672c\u8bed\u6cd5\u4ecb\u7ecd\uff1a
if condition
then
command1
command2 ...
commandN
fi



2\u3001shell\u4e2d\u7684\u53e6\u4e00\u4e2a\u903b\u8f91\u8fd0\u7b97\u7b26and \uff08&&\uff09\u7528\u6cd5\u5b9e\u4f8b\u4ecb\u7ecd\uff1a
a=10
b=20
if [[ $a -lt 100 && $b -gt 100 ]]
then
echo "\u8fd4\u56de true"
else
echo "\u8fd4\u56de false"
fi
\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a\u8fd4\u56de false

\u53ef\u4ee5\u4f7f\u7528 if-elif-else \u8bed\u6cd5\u6765\u5199\u591a\u6761\u4ef6\u8bed\u53e5\u3002
1\u3001\u9996\u5148\u8981\u7406\u89e3if-else\u7684\u57fa\u672c\u7528\u6cd5\uff0cif\u6761\u4ef6+then\u64cd\u4f5c+else\u64cd\u4f5c+fi\u95ed\u5408\uff0c\u4e66\u5199\u65b9\u6cd5\u5982\u4e0b\uff1a

2\u3001if -elif-else \u8bed\u6cd5\u7684\u5177\u4f53\u683c\u5f0f--if\u5355\u6761\u4ef6\u591a\u5206\u652f\uff0c\u4e66\u5199\u65b9\u6cd5\u5982\u4e0b\uff1a

3\u3001\u5b9e\u4f8b - if\u5355\u6761\u4ef6\u591a\u5206\u652f\uff0c\u8fd9\u4e2a\u5b9e\u4f8b\u7684\u8f93\u51fa\u7ed3\u679c\u662f\uff1aa \u5c0f\u4e8e b\u3002

4\u3001\u5b9e\u4f8b - if\u591a\u6761\u4ef6\u591a\u5206\u652f\uff0c\u8fd9\u4e2a\u5b9e\u4f8b\u8f93\u51fa\u7ed3\u679c\u662f\uff1aa \u7b49\u4e8e b,\u6216 a\u5c0f\u4e8e10\u3002

\u6269\u5c55\u8d44\u6599
1\u3001shell\u8bed\u6cd5\u6ce8\u610f\u4e8b\u9879
shell\u7684if\u8bed\u6cd5\u548cC\u8bed\u8a00\u7b49\u9ad8\u7ea7\u8bed\u8a00\u975e\u5e38\u76f8\u4f3c\uff0c\u552f\u4e00\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9\u5c31\u662fshell\u7684if\u8bed\u53e5\u5bf9\u7a7a\u683c\u65b9\u9762\u7684\u8981\u6c42\u6bd4\u8f83\u4e25\u683c\uff0c\u5982\u679c\u5728\u9700\u8981\u7a7a\u683c\u7684\u5730\u65b9\u6ca1\u6709\u6253\u4e0a\u7a7a\u683c\uff0c\u5c31\u4f1a\u62a5\u9519\u3002
\u5982if [ 1 == 1 ];then echo "abc";fi\u4e2d\u5982\u679c\u5728\u5c11\u5199\u4e86if\u540e\u9762\u7684\u7a7a\u683c\u5c31\u62a5\u9519\uff1a

2\u3001 shell\u8bed\u6cd5\u4e2d[[ ]]\u548c[ ]\u7684\u4e3b\u8981\u533a\u522b
\uff081\uff09 [ ] \u5b9e\u9645\u4e0a\u662fbash \u4e2d test \u547d\u4ee4\u7684\u7b80\u5199\u3002\u5373\u6240\u6709\u7684 [ expr ] \u7b49\u4e8e test expr\u3002\u5bf9 test \u547d\u4ee4\u6765\u8bf4\uff0c \u7528 -eq \u8981\u8fdb\u884c\u6570\u5b57\u6bd4\u8f83\uff0c\u800c\u4f60\u6b64\u65f6\u4f20\u5165\u5b57\u7b26\u4e32\uff0c\u5c31\u62a5\u9519\u4e86\u3002
\uff082\uff09 [[ ]] \u662f\u5185\u7f6e\u5728shell\u4e2d\u7684\u4e00\u4e2a\u547d\u4ee4\uff0c\u5b83\u6bd4test\u5f3a\u5927\u7684\u591a\u3002\u652f\u6301\u5b57\u7b26\u4e32\u7684\u6a21\u5f0f\u5339\u914d\uff08\u4f7f\u7528=~\u64cd\u4f5c\u7b26\u65f6\u751a\u81f3\u652f\u6301shell\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff09\u3002\u903b\u8f91\u7ec4\u5408\u53ef\u4ee5\u4e0d\u4f7f\u7528test\u7684-a,-o\u800c\u4f7f\u7528&& ||\u3002

if [ ! -w "$logfile" ]
then echo "not writeable"
echo "not writeable again"
fi

 注意空格,shell里面有的地方必须有空格,有的地方必须没有空格。[ ]前后都要有空格



if[! -w “$logfile” ]

把“”符号去掉试试。
改为:if[! -w $logfile ]

if语句后面有没有加结尾:fi ?

if [ ! -w "$logfile" ]; then 
    # 代码块
fi

注意所有符号为半角英文符号,还有空格



扩展阅读:100个必会的shell脚本 ... java 执行shell脚本 ... linux shell read ... gg脚本加密lua脚本 ... linux自动执行shell脚本 ... linux shell 命令 ... efi shell 脚本 ... centos shell脚本 ... 一个bash shell脚本的第一行是 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网