read -p "Enter the number: " num if [ $num -lt 0 ]; then echo "Number $num is negative" elif [ $num -gt 0 ]; then echo "Number $num is positive" else echo "Number $num is zero" fi 让我运行它来涵盖这里的所有三种情况: Running a script with bash elif statement 用逻辑运算符...
read -p "Enter the number: " num if [ $num -lt 0 ]; then echo "Number $num is negative" elif [ $num -gt 0 ]; then echo "Number $num is positive" else echo "Number $num is zero" fi 让我运行它来涵盖这里的所有三种情况: Running a script with bash elif statement 用逻辑运算符...
echo "All the command-line parameters are: "$*"" if [ $# -lt "$MINPARAMS" ] then echo echo "This script needs at least $MINPARAMS command-line arguments!" fi echo exit 0 运行代码: bash test30.sh 1 2 10 The name of this script is "test.sh". The name of this script is "t...
如果这样,那就那样,否则就……。还不明白吗?了解了 Bash Shell 脚本中的 if-else 语句后就明白了。Bash 支持 if-else 语句,以便你可以在 shell 脚本中使用逻辑推理。 通用的 if-else 语法如下: 复制 if [ expression ]; then ## 如果条件为真则执行此块,否则转到下一个 elif [ expression ]; then ##...
if[$mod-eq0];then echo'Number $num is even' else echo'Number $num is odd' fi 让我们用相同的数字再次运行它: Running a bash script that checks odd even number 正如你所看到的,该脚本更好,因为它还告诉你该数字是否为奇数。 使用elif(否则如果)语句 ...
elif [ $numberTwelve -gt 12 ] then echo "numberTwelve variable is greater than 12" else echo "neither of the statemens matched" fi 输出如下: [zexcon@fedora ~]$ ./learnToScript.sh numberTwelve variable is equal to 12 你所看到的是if语句的第一行,它在检查变量的值是否真的等于12。如果是...
In this example, the script first checks if ‘a’ is greater than ‘b’. If it’s not, it moves on to the ‘elif’ statement to check if ‘a’ is greater than ‘c’. If neither condition is met, it executes the ‘else’ statement, which in this case, prints ‘a is not greate...
$ bash script.sh hello world 那么,为什么我们需要 Shell 脚本呢?因为你不必一遍又一遍地输入同一个命令,你只需运行 Shell 脚本即可。 此外,如果你的脚本中有复杂的逻辑,把所有的命令都输入到终端中可能并不是一个好主意。 例如,如果你输入下面的命令,它会奏效,但这并不容易理解。不断地输入相同的命令(甚至要...
#!/bin/bash # 参数1:需要检测的参数 parameter_1=$1 # 检测参数的类型 if [ "$parameter_1" == "integer" ]; then echo "参数1是整数" elif [ "$parameter_1" == "string" ]; then echo "参数1是字符串" else echo "参数1不是整数或字符串" fi 这个脚本首先获取了传递给它的参数(即需要检...
if CONDITION; then if CONDITION2; then CMD fi fi 条件取反: ! COMMAND 双分支: if CONDITION; then 分支1 else 分支2 fi 练习2: 传递两个整数给脚本,返回其较大者 test.sh #!/bin/bash if $1 -gt $2;then echo $1 else echo $2