一、语法

while [ condition ]  # 当 condition 条件成立时,就进行循环,直到条件不成立停止
do
    #执行内容
done

二、练习

  1. 输入用户输入的参数,直到用户输入 "end" 结束循环
    while
    read -p "Plz input a paramter": param
    test $param != "end"
    do
    echo "$param"
    done
    user@ae01:~$ ./test.sh
    Plz input a paramter:a
    a
    Plz input a paramter:b
    b
    Plz input a paramter:c
    c
    Plz input a paramter:end
    user@ae01:~$
  2. 输出1到5的自然数
    i=1
    while [ "$i" -le 5 ]
    do
    echo "$i"
    i=$(($i+1))
    done
    user@ae01:~$ ./test.sh
    1
    2
    3
    4
    5
    user@ae01:~$

相关文章:

  • 2022-01-01
  • 2021-12-06
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2021-10-08
  • 2021-07-02
  • 2021-12-10
猜你喜欢
  • 2021-09-21
  • 2021-12-06
  • 2021-12-30
  • 2021-06-21
  • 2021-12-14
  • 2022-02-08
  • 2021-07-06
相关资源
相似解决方案