Bash 列出特定文件后的文件
我想使用 bash 脚本在特定文件之后的目录中的所有文件上运行我的程序。
如果我有一个类似的目录:
fileA
fileB
fileC
fileD
我想对 fileC
之后的所有文件运行 ./prog
。我该如何编写 bash 脚本来执行此操作?
我目前有
for FILE in ./tests/*; do
./prog $FILE
if [ $? -eq 0 ]; then
echo "success: $FILE"
else
echo "**FAILURE: $FILE"
exit 1
fi
done
但是,我想从该目录中的特定文件开始。它不一定需要排序,因为 ls
按特定顺序列出文件,每次都是相同的。
我通常只是运行我的脚本,当它失败时,我会针对该特定文件修复它,但随后我想从该文件恢复,而不是从头开始。
I want to run my program on all the files in a directory after a specific file using a bash script.
If I have a directory like:
fileA
fileB
fileC
fileD
I want to run ./prog <file>
for all files after fileC
. How would I write a bash script to do this?
I currently have
for FILE in ./tests/*; do
./prog $FILE
if [ $? -eq 0 ]; then
echo "success: $FILE"
else
echo "**FAILURE: $FILE"
exit 1
fi
done
But, I want to start at a specific file in that directory. It doesn't necessarily need to be sorted since ls
list files in a specific order which is the same each time.
I typically just run my script, and when it fails, I fix it for that specific file, but then I'd want to resume from that file, and not restart from the beginning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果文件已排序,则可以使用
'<'
和'>'
运算符对两个变量进行字符串比较:If the files are sorted, then you can use
'<'
and'>'
operators to do a stringwise compare of two variables:grep -A 999999 列出测试/起始文件(^ 和 $ 以避免部分匹配)和 999999 下一个条目。
tail -n +2 列出从第 2 行开始的所有内容(从 1 开始)
grep -A 999999 lists tests/startfile (^ and $ to avoid partial match) and the 999999 next entries.
tail -n +2 lists everything starting from line 2 (1-based)