Shell 编程基础之 For 练习

一、语法

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