Shell 编程基础之 While 练习

一、语法

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:~$
原文地址:https://www.cnblogs.com/tannerBG/p/4056989.html