bash之流程控制

发布于:2024-10-17 ⋅ 阅读:(9) ⋅ 点赞:(0)

一.if else                

和 Java、PHP 等语言不一样,sh 的流程控制不可为空

在 sh/bash 里可不能这么写,如果 else 分支没有语句执行,就不要写这个 else。

if 语句语法格式:

if condition
then
    command1
    command2
    ...
    commandN
fi

if else
if else 语法格式:

if condition
then
    command1
    command2
    ...
    commandN
else
    command
fi

if-elif-else 语法格式:

if condition1
then
    command1
elif condition2
then
    command2
else
    commandN
fi

以下实例判断两个变量是否相等:

a=10
b=20
if [ $a == $b ]
then
   echo "a == b"
elif [ $a -gt $b ]
then
   echo "a > b"
elif [ $a -lt $b ]
then
   echo "a < b"
else
   echo "Ineligible"
fi

输出结果:

a < b

if else 语句经常与 test 命令结合使用

num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
    echo 'Two numbers are equal!'
else
    echo 'The two numbers are not equal!'
fi

输出结果:

Two numbers are equal!

二.for 循环

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

例如,顺序输出当前列表中的数字:

for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done

输出结果:

The value is: 1

The value is: 2

The value is: 3

The value is: 4

The value is: 5

顺序输出字符串中的字符:

for str in This is a string
do
    echo $str
done

输出结果:

This

is

a

string

三.while 语句

        while 循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。其格式为:

while condition
do
    command
done
#!/bin/bash
int=1
while(( $int<=5 ))
do
    echo $int
    let "int++"
done

运行脚本,输出:

1

2

3

4

5

        如果 int 小于等于 5,那么条件返回真。int 从 1 开始,每次循环处理时,int 加 1。运行上述脚本,返回数字 1 到 5,然后终止。

        使用了 Bash let 命令,它用于执行一个或多个表达式,变量计算中不需要加上 $ 来表示变量。

        while 循环可用于读取键盘信息。下面的例子中,输入信息被设置为变量 MAN,按 <Ctrl+D> 结束循环。

echo 'press <CTRL-D> exit'
echo -n 'Who do you think is the most handsome: '
while read MAN
do
    echo "Yes! $MAN is really handsome"
done

四.无限循环

无限循环语法格式:

while :
do
    command
done
或者
while true
do
    command
done

或者:

for (( ; ; ))

五.until 循环

        until 循环执行一系列命令直至条件为真时停止。 until 循环与 while 循环在处理方式上刚好相反。 一般 while 循环优于 until 循环,但在某些时候—也只是极少数情况下,until 循环更加有用。 until 语法格式:

until condition
do
    command
done

        条件可为任意测试条件,测试发生在循环末尾,因此循环至少执行一次—请注意这一点。

六.case

        Shell case 语句为多选择语句。可以用 case 语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。case 语句格式如下:

case 值 in
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
esac

        取值后面必须为单词 in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。
        取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。
        下面的脚本提示输入 1 到 4,与每一种模式进行匹配:

echo 'Enter a number between 1 and 4:'
echo 'The number you entered is:'
read aNum
case $aNum in
    1)  echo 'You have chosen 1'
    ;;
    2)  echo 'You have chosen 2'
    ;;
    3)  echo 'You have chosen 3'
    ;;
    4)  echo 'You have chosen 4'
    ;;
    *)  echo 'You did not enter a number between 1 and 4'
    ;;
esac

输入不同的内容,会有不同的结果,例如:

Enter a number between 1 and 4:
The number you entered is:
3
You have chosen 3

        在这段 Bash 脚本中,`;;`和`*)`具有以下特定用途:

        一、`;;`的作用1. 结束分支: - 在 `case` 语句中,每个分支以 `;;` 作为结束标志。 - 当某个条件匹配并执行完对应分支的命令后,遇到 `;;` 就表示这个分支的处理结束,不会继续执行后续的分支。 - 例如在 `1|2|3|4|5) echo "The number you entered is $aNum!" ;;` 这个分支中,当输入的数字是 1、2、3、4 或 5 时,打印相应的提示信息,然后 `;;` 表示这个分支处理完毕。

        二、`*)`的作用1. 处理默认情况: - 在 `case` 语句中,`*)` 表示默认分支,用于处理所有未被前面特定条件匹配的情况。 - 当输入的数字不是 1 到 5 之间的任何一个数字时,就会执行这个默认分支。 - 例如在 `*) echo "The number you entered is not between 1 and 5! game over!" ; break ;;` 中,如果用户输入的数字不在 1 到 5 的范围内,就会打印错误提示信息 “The number you entered is not between 1 and 5! game over!”,然后执行 `break` 语句跳出循环。

        总的来说,`;;` 和 `*)` 在 `case` 语句中起到了明确分支结束和处理默认情况的重要作用,使脚本能够根据不同的输入进行相应的处理。

七.跳出循环

        在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,Shell 使用两个命令来实现该功能:break 和 continue。

break 命令

break 命令允许跳出所有循环(终止执行后面的所有循环)。

        下面的例子中,脚本进入死循环直至用户输入数字大于 5。要跳出这个循环,返回到 shell 提示符下,需要使用 break 命令。

#!/bin/bash
while :
do
    echo -n "Enter a number between 1 and 5:"
    read aNum
    case $aNum in
        1|2|3|4|5) echo "The number you entered is $aNum!"
        ;;
        *) echo "The number you entered is not between 1 and 5! game over!"
            break
        ;;
    esac
done

执行以上代码,输出结果为:

Enter a number between 1 and 5:3
The number you entered is 3!
Enter a number between 1 and 5:7
The number you entered is not between 1 and 5! game over!

八.continue

        continue 命令与 break 命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。 对上面的例子进行修改:

#!/bin/bash
while :
do
    echo -n "Enter a number between 1 and 5: "
    read aNum
    case $aNum in
        1|2|3|4|5) echo "The number you entered is $aNum!"
        ;;
        *) echo "The number you entered is not between 1 and 5!"
            continue
            echo "game over"
        ;;
    esac
done

        运行代码发现,当输入大于 5 的数字时,该例中的循环不会结束,语句 echo "game over" 永远不会被执行。

九.esac

        case 的语法和 C family 语言差别很大,它需要一个 esac(就是 case 反过来)作为结束标记,每个 case 分支用右圆括号,用两个分号表示 break。