Skip to content

Shell 流程控制

条件判断

if...then...fi

sh
#!/bin/bash

if [ 条件 ]; then
  # ...
fi
例子
bash
$ echo '
#!/bin/bash

a=10
b=20

if [ $a -lt $b ]; then
    echo "a<b"
fi

if (( a < b )); then
    echo "a<b"
fi
' > xxxx.sh

$ bash xxxx.sh
a<b
a<b
sh
#!/bin/bash

a=10
b=20

if [ $a -lt $b ]; then
    echo "a<b"
fi

if (( a < b )); then
    echo "a<b"
fi

if...then...else...fi

sh
#!/bin/bash

if [ 条件 ]; then
  # ...
else
  # ...
fi

if...then...elif...then...fi

sh
#!/bin/bash

if [ 条件1 ]; then
  # ...
elif [ 条件2 ]; then
  # ...
fi

case...in...esac

sh
#!/bin/bash

case $变量 in
  "值1"
    # ...
    ;;
  "值2"
    # ...
    ;;
  *
    # ...
    ;;
esac
例子
bash
$ echo '
#!/bin/bash

read -p "请输入 ( yes/no ) " answer

case $answer in
  "yes")
    echo "aaaa"
    ;;
  "no")
    echo "bbbb"
    ;;
  *)
    echo "cccc"
esac
' > xxxx.sh

$ bash xxxx.sh
请输入( yes/no ) yes
aaaa
$ bash xxxx.sh
请输入( yes/no ) no
bbbb
$ bash xxxx.sh
请输入( yes/no ) xxx
cccc
sh
#!/bin/bash

read -p "请输入 ( yes/no ) " answer

case $answer in
  "yes")
    echo "aaaaa"
    ;;
  "no")
    echo "bbbb"
    ;;
  *)
    echo "cccc"
esac

循环遍历

for...do...done

sh
#!/bin/bash

for (( 循环计时器变量 = 初始值; 判断循环计时器; 更新循环计时器 ))
  do
    # ...
  done
sh
#!/bin/bash

for (( 循环计时器变量 = 初始值; 判断循环计时器; 更新循环计时器 )); do
  # ...
done
例子
bash
$ echo '
#!/bin/bash

for ((i=0; i<=$#; i++)); do
  echo $i ${!i}
done
' > xxxx.sh

$ bash xxxx.sh 10 20 30
0 111.sh
1 10
2 20
3 30
sh
#!/bin/bash

for ((i=0; i<=$#; i++)); do
  echo $i ${!i}
done

fot...in...

sh
#!/bin/bash

for 变量 in 序列
  do
    echo $i ${!i}
  done
sh
#!/bin/bash

for 变量 in 序列; do
  echo $i ${!i}
done
例子
sh
#!/bin/bash

for filename in `ls`; do
  echo $filename
done

while...do...done

sh
#!/bin/bash

while [ 条件 ];
  do
    # ...
  done
sh
#!/bin/bash

while [ 条件 ]; do
  # ...
done

避免死循环

为了避免出现重复不停的死循环,建议使用一个循环计控制器变量并将其作为循环的判断条件

sh
#!/bin/bash

循环计控制器变量=初始值

while [ 条件 ]; do
  # ...
  # 更新循环计时器变量
done
例子
bash
$ echo '
#!/bin/bash

num=1

while (( num <= 5 )); do
  echo $num
  (( num++ ))
done
' > xxxx.sh

$ bash xxxx.sh
1
2
3
4
5
sh
#!/bin/bash

num=1

while (( num <= 5 )); do
  echo $num
  (( num++ ))
done