我需要让脚本在第一次循环迭代中执行特定操作。
for file in /path/to/files/*; do
echo "${file}"
done
Run Code Online (Sandbox Code Playgroud)
我想要这个输出:
First run: file1
file2
file3
file4
...
Run Code Online (Sandbox Code Playgroud)
您可以使用文件创建一个数组,然后从该数组中删除并保留第一个元素,单独处理它,然后处理数组的其余元素:
# create array with files
files=(/path/to/files/*)
# get the 1st element of the array
first=${files[0]}
# remove the 1st element of from array
files=("${files[@]:1}")
# process the 1st element
echo First run: "$first"
# process the remaining elements
for file in "${files[@]}"; do
echo "$file"
done
Run Code Online (Sandbox Code Playgroud)
一种非常常见的安排是更改循环内的变量。
noise='First run: '
for file in /path/to/files/*; do
echo "$noise$file"
noise=''
done
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2704 次 |
| 最近记录: |