11.Shell for循环 - morris131/morris-book GitHub Wiki
for循环一般格式为:
for 变量 in 列表
do
command1
command2
...
commandN
done
列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。in列表是可选的,如果不用它,for循环使用命令行的位置参数。
#!/bin/bash
for num in 1 2 3 4 5
do
echo "The value is $num"
done
# chmod +x fornumber.sh
# ./fornumber.sh
The value is 1
The value is 2
The value is 3
The value is 4
The value is 5
#!/bin/bash
for str in 'This is a string'
do
echo $str
done
# chmod +x forstring.sh
# ./forstring.sh
This is a string
#!/bin/bash
for FILE in $HOME/.bash*
do
echo $FILE
done
# chmod +x forfile.sh
# ./forfile.sh
/root/.bash_history
/root/.bash_logout
/root/.bash_profile
/root/.bashrc