bash脚本-流程

目标

  使用for循环迭代列表。

实操演示

  • for 循环

语法结构如下:

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

  当变量值在列表中,for循环即执行一次命令,使用变量名获取列表中的当前取值。命令可为任何有效的shell命令和语句。in列表可以包含替换、字符串和文件名。
  in列表是可选的,如过不用它,for循环使用命令行的位置参数。

[root@sinfotek tmp]# cat for.sh 
#!/bin/bash
for loop in 1 2 3 4 5
do 
echo "The value is: $loop"
done
[root@sinfotek tmp]# ./for.sh 
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5


[root@sinfotek tmp]# cat for.sh 
#!/bin/bash
for str in 1 2 3 4 5
do 
echo $str
done
[root@sinfotek tmp]# ./for.sh 
1
2
3
4
5

  • while循环

  while循环用于不断执行一系列命令,也用于从输入文件中读取数据。

其语法格式为:

while condition
do
    command
done

  输出数字1-5

[root@sinfotek tmp]# cat for.sh 
#!/bin/bash
int=1
while (( $int <=5 ))
do
echo $int
let "int++"
done
[root@sinfotek tmp]# ./for.sh 
1
2
3
4
5
  • 读取键盘信息
[root@sinfotek tmp]# cat for.sh 
#!/bin/bash
echo '按下 <CTRL-D> 退出'
echo -n '输入你喜欢的网址:'
while read name
do
echo "是的,$name 是一个好网站。"
done
[root@sinfotek tmp]# ./for.sh 
按下 <CTRL-D> 退出
输入你喜欢的网址:sinfotek
是的,sinfotek 是一个好网站。
  • 无限循环
while true
do
command
done
  • until 循环

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

until 语法格式:

until condition
do
    command
done

  

  condition 一般为条件表达式,如果返回值为 false,则继续执行循环体内的语句,否则跳出循环。

[root@sinfotek tmp]# cat until 
#!/bin/bash
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
[root@sinfotek tmp]# ./until 
0
1
2
3
4
5
6
7
8
9
  • case …esac

   case … esac为多选择语句,与其他语言中的switch …case语句类似,是一种多分枝选择结构,每个case分支用右圆括号开始,用两个分号;;表示break,即执行结束,跳出整个case…esac语句,esac作为结束标记。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。

case … esac语法格式如下:

casein
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
esac

  判断1-4范围内的数字

[root@sinfotek tmp]# cat case 
#!/bin/bash
echo '输入 1 到 4 之间的数字:'
echo '输入的数字为:'
read insert
case $insert in
1) echo '数字1'
;;
2) echo '数字2'
;;
3) echo '数字3'
;;
4) echo '数字4'
;;
*) echo '不在有效范围内'
;;
esac
[root@sinfotek tmp]# ./case 
输入 1 到 4 之间的数字:
输入的数字为:
1
数字1
[root@sinfotek tmp]# ./case 
输入 1 到 4 之间的数字:
输入的数字为:
6
不在有效范围内
  • 跳出循环

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

  • break命令

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

  示例如下:

[root@sinfotek tmp]# cat break 
#!/bin/bash
while :
do
echo -n "输入 1 到 5 之间的数字:"
read number
case $number in
1|2|3|4|5) echo "输入的数字为$number!"
;;
*) echo "抱歉您输入的内容不符合要求。"
break
;;
esac
done
[root@sinfotek tmp]# ./break 
输入 1 到 5 之间的数字:1
输入的数字为1!
输入 1 到 5 之间的数字:2
输入的数字为2!
输入 1 到 5 之间的数字:3
输入的数字为3!
输入 1 到 5 之间的数字:4
输入的数字为4!
输入 1 到 5 之间的数字:5
输入的数字为5!
输入 1 到 5 之间的数字:6
抱歉您输入的内容不符合要求。
  • continue命令

  continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。

[root@sinfotek tmp]# cat continue 
#!/bin/bash
while :
do
echo -n "输入 1 到 5 之间的数字:"
read number
case $number in
1|2|3|4|5) echo "输入的数字为$number!"
;;
*) echo "抱歉您输入的内容不符合要求。"
continue
;;
esac
done
[root@sinfotek tmp]# chmod +x continue 
[root@sinfotek tmp]# ./continue 
输入 1 到 5 之间的数字:2
输入的数字为2!
输入 1 到 5 之间的数字:6
抱歉您输入的内容不符合要求。
输入 1 到 5 之间的数字:1
输入的数字为1!
  • if else

if 语法格式:

if condition
then
    command1 
    command2
    ...
    commandN 
fi
  • if else-if else

if else-if else 语法格式:

if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi

实例

[root@sinfotek tmp]# cat if
#!/bin/bash
a=1
b=2
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 "没有符合的条件"
fi
[root@sinfotek tmp]# ./if
a 小于 b
文档更新时间: 2022-07-21 10:11   作者:xiubao yan