linux-tutorial/codes/shell/demos/statement/for-demo.sh

40 lines
601 B
Bash
Raw Normal View History

2019-03-01 10:36:45 +08:00
#!/usr/bin/env bash
################### for 语句 ###################
echo "print 0 to 9"
for (( j = 0; j < 10; j ++ )); do
2019-10-10 08:56:31 +08:00
echo ${j}
2019-03-01 10:36:45 +08:00
done
# Output:
# print 0 to 9
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
################### for in 语句 ###################
echo "print 1 to 5"
2019-10-10 08:56:31 +08:00
for i in {1..5}; do
echo ${i};
done
2019-03-01 10:36:45 +08:00
# Output:
# print 1 to 5
# 1
# 2
# 3
# 4
# 5
################### for in 语句遍历文件 ###################
DIR=/home/zp
for FILE in ${DIR}/*.sh; do
2019-10-10 08:56:31 +08:00
mv "$FILE" "${DIR}/scripts"
2019-03-01 10:36:45 +08:00
done
# 将 /home/zp 目录下所有 sh 文件拷贝到 /home/zp/scripts