Shell for

for循环一般格式为:
for 变量 in 列表
do
command1
command2
...
commandN
done
列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。

in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。

vim test1.sh

#!/bin/bash
for loop in 1 2 3 4 5 6
do
    echo "The value is: $loop"
done

for loop in "Spark" "Scala" "MLlib" "Dataset"
do
    echo "The value is: $loop"
done

#显示主目录下以.sh结尾的文件
for file in $HOME/*.sh
do
   echo $file
done

  

$ sh test1.sh
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
The value is: 6
The value is: Spark
The value is: Scala
The value is: MLlib
The value is: Dataset
/home/wx/test1.sh
/home/wx/test2.sh
/home/wx/test3.sh
/home/wx/test4.sh
/home/wx/test.sh

原文地址:https://www.cnblogs.com/wwxbi/p/6171502.html