Shell Loop

一、Loop

for loop syntax:

1、数字范围语法

for VARIABLE in 1 2 3 4 5 .. N
do
	command1
	command2
	commandN
done
或者
for VARIABLE in file1 file2 file3
do
	command1 on $VARIABLE
	command2
	commandN
done
或者
for OUTPUT in $(Linux-Command)
do
	command1 on $OUTPUT
	command2 on $OUTPUT
	commandN
done
二、Examples
1、例一
#!/bin/bash
for i in 1 2 3 4 5
do
   echo "Welcome $i times"
done

输出:

Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

2、例二

#!/bin/bash
for i in {1..5}
do
   echo "Welcome $i times"
done
输出:
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

3、例三

#!/bin/bash
for i in {0..10..2}
do
  echo "Welcome $i times"
done
注意:{START..END..INCREMENT}
输出:
Welcome 0 times
Welcome 2 times
Welcome 4 times
Welcome 6 times
Welcome 8 times
Welcome 10 times
三、seq command
#!/bin/bash
for i in $(seq 1 2 20)
do
   echo "Welcome $i times"
done
输出:

1
3
5
7
9

四、三个表达式

for (( EXP1; EXP2; EXP3 ))
do
	command1
	command2
	command3
done

例:

#!/bin/bash
for (( c=1; c<=5; c++ ))
do
   echo "Welcome $c times"
done

输出:

Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
无限循环:
#!/bin/bash
for (( ; ; ))
do
   echo "infinite loops [ CTRL+C to stop]"
done

你可能感兴趣的:(linux,shell,loop)