一、语法

for var in con1 con2 con3 ...
do
    # 执行内容
done
for var in {min..max}
do
    # 执行内容
done
for (( 初始值; 限制值; 步阶 ))
do
    # 执行内容
done

二、练习

  1. 输出 Shell 的运行时的输入参数
    echo "$# parameters"
    for p in "$@"
    do
      echo "$p"
    done
    user@ae01:~$ sh ./test.sh 1 "2 3"
    2 parameters
    1
    2 3
    user@ae01:~$
  2. 输出1到5的自然数
    for p in {1..5}
    do
      echo "$p"
    done
    user@ae01:~$ ./test.sh
    1
    2
    3
    4
    5
    user@ae01:~$

    for ((i=1; i<=5; i++))
    do
      echo $i
    done
    user@ae01:~$ ./test.sh
    1
    2
    3
    4
    5
    user@ae01:~$

    for i in `seq 1 5`
    do
      echo $i
    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
  • 2022-12-23
  • 2021-10-08
  • 2021-07-02
猜你喜欢
  • 2021-09-21
  • 2021-07-22
  • 2021-12-30
  • 2021-06-21
  • 2021-12-14
  • 2022-02-08
  • 2021-07-06
相关资源
相似解决方案