1. if/else 语句 语法: if condition; then commands;elif condition; then commands;else commands;fi 示例:需求:脚本需要1个参数才能正确运行,而在脚本执行时,如果指定的参数个数不等于1,则shell脚本就应该打印出一个错误信息,告知用户指定的参数个数不对,然后结束脚本的执行.#!/bin/bashif [ $# -ne 1 ];then echo "Error! Arguments are not correc…
执行脚本: sh login.sh user,其中user为第一个参数 如下所示,如果用户‘user’登录,'who | grep "$1"'为true,until循环结束,程序继续执行,输出 “***** user has just logged in *****”信息. 如果用户‘user’没有登录,屏幕每隔60秒打印一次“ok”. $1表示程序的第一个参数 # login.sh 1 #!/bin/bash until who | grep "$1" >…
在linux的shell中 if 语句通过关系运算符判断表达式的真假来决定执行哪个分支.Shell 有三种 if ... else 语句: if ... fi 语句: if ... else ... fi 语句: if ... elif ... else ... fi 语句. 1) if ... else 语句 if ... else 语句的语法: if [ expression ] then Statement(s) to be executed if expression is true f…
原语句: #!/bin/bash for test in I don't know if this'll work do echo "work:$test" done 结果: work:I work:dont know if thisll work:work 改成后语句: #!/bin/bash for test in I don\'t know if "this'll" work do echo "work:$test" done 结果: wo…