👉博__主👈:米码收割机
👉技__能👈:C++/Python语言
👉公众号👈:测试开发自动化
👉专__注👈:专注主流机器人、人工智能等相关领域的开发、测试技术
shell条件判断、循环语句、基本运算符
1. 关系运算符
| 关系运算符 | 符号说明 |
| = | 判断两个值是否相等 |
| != | 判断两个值是否不相等 |
| -eq | 判断两个整数是否相等 |
| -ne | 判断两个整数是否不相等 |
| -lt | 判断左边的整数是否小于右边的整数 |
| -gt | 判断左边的整数是否大于右边的整数 |
| -le | 判断左边的整数是否小于或等于右边的整数 |
| -ge | 判断左边的整数是否大于或等于右边的整数 |
2. 逻辑运算符
| 逻辑运算符号 | 符号说明 |
| && | 逻辑与运算符,用于连接两个条件,如果两个条件都为真,则整个条件为真 |
| || | 逻辑或运算符,用于连接两个条件,如果其中一个条件为真,则整个条件为真 |
| ! | 逻辑非运算符,用于取反一个条件的值,如果原条件为真,则取反后为假,反之亦然 |
3. 条件语句
3.1 if 语句
#!/bin/bash count=0 if [ $count -eq 0 ]; then echo "The count is zero." fi
在该例子中,使用方括号测试条件 $count -eq 0,如果条件为真,则打印 “The count is zero.”。
3.2 if-else 语句
#!/bin/bash num=4 if [ $((num % 2)) -eq 0 ]; then echo "$num is even." else echo "$num is odd." fi
在该例子中,使用 $((num % 2)) 计算 $num 的余数,并测试条件是否为零。如果条件为真,则打印 $num is even。否则打印 $num is odd.。
3.3 if-elif-else 语句
#!/bin/bash num=-5 if [ $num -gt 0 ]; then echo "$num is positive." elif [ $num -lt 0 ]; then echo "$num is negative." else echo "$num is zero." fi
在该例子中,先使用 -gt 和 -lt 测试 $num 的值,并根据测试结果执行相应的操作。如果 $num 大于零,则打印$num is positive.;如果 $num 小于零,则打印 $num is negative.;如果 $num 等于零,则打印 $num is zero.。
3.4 逻辑运算符测试多个条件
#!/bin/bash age=18 nationality="American" if [ $age -ge 18 ] && [ "$nationality" == "American" ]; then echo "You are old enough to vote in the U.S." else echo "You are not old enough to vote in the U.S." fi
在该例子中,使用 -ge 测试 $age 是否大于等于 18,并使用 == 测试 $nationality 是否等于 American。
如果两个测试条件都为真,则打印 You are old enough to vote in the U.S.,否则打印 You are not old enough to vote in the U.S.。
3.5. case条件语句
#!/bin/bash read -p "Enter a fruit: " fruit case $fruit in "apple") echo "You entered an apple." ;; "banana") echo "You entered a banana." ;; "orange") echo "You entered an orange." ;; *) echo "You entered an unknown fruit." ;; esac
在该例子中,使用 read 命令读取用户输入,并将其保存到 fruit 变量中。然后,使用 case 语句测试 fruit 是否等于 apple、banana或 orange 中的任意一个值。如果 fruit 等于其中一个值,则打印相应的消息;否则打印 You entered an unknown fruit.。
4. 循环语句
4.1 for循环
#!/bin/bash fruits=("apple" "banana" "orange") for fruit in "${fruits[@]}"; do echo "I like $fruit." done
在该例子中,使用 for 循环迭代名为 fruits 的数组中的每个值,并使用 $fruit 变量打印一条消息
4.2 while循环
#!/bin/bash number=$((RANDOM % 100)) guess=-1 while [ $guess -ne $number ]; do read -p "Guess a number between 0 and 99: " guess if [ $guess -lt $number ]; then echo "Your guess is too low." elif [ $guess -gt $number ]; then echo "Your guess is too high." else echo "Congratulations, you guessed the number!" fi done
在该例子中,使用 while 循环实现猜数字游戏。首先,使用 $RANDOM 变量生成一个随机数,然后要求用户猜测该数字。如果猜测的数字比随机数小,则打印 Your guess is too low.;如果猜测的数字比随机数大,则打印 Your guess is too high.;如果猜测的数字等于随机数,则打印 Congratulations, you guessed the number!,并退出循环。
break 和 continue 命令:
在循环中,可以使用 break 命令跳出循环,或使用 continue 命令跳过当前循环迭代。break 和 continue 命令通常与条件语句结合使用,以实现更复杂的控制流。
4.3 until循环
#!/bin/bash seconds=10 until [ $seconds -eq 0 ]; do echo "Time remaining: $seconds seconds." sleep 1 seconds=$((seconds - 1)) done echo "Time's up!"
在该例子中,使用 until 循环实现一个倒计时计时器。首先,将 seconds 变量设置为 10。然后,使用 until 循环检查 seconds 是否等于 0。在循环中,打印 Time remaining: [seconds] seconds.,然后使用 sleep 命令暂停一秒钟,并将 seconds 减少 1。循环结束后,打印 Time's up!。