更新时间:2021-09-23 08:29:53
首先要注意几点:当你使用 Data/data1.txt
作为参数时,它真的应该是 /Data/data1.txt
(带前导斜线)?另外,外循环应该只扫描 .txt 文件,还是/Data 中的所有文件?这是一个答案,假设只有 /Data/data1.txt
和 .txt 文件:
A couple of notes first: when you use Data/data1.txt
as an argument, should it really be /Data/data1.txt
(with a leading slash)? Also, should the outer loop scan only for .txt files, or all files in /Data? Here's an answer, assuming /Data/data1.txt
and .txt files only:
#!/bin/bash
for filename in /Data/*.txt; do
for ((i=0; i<=3; i++)); do
./MyProgram.exe "$filename" "Logs/$(basename "$filename" .txt)_Log$i.txt"
done
done
注意事项:
/Data/*.txt
扩展为/Data 中文本文件的路径(包括/Data/部分)$( ... )
运行一个 shell 命令并将其输出插入命令行中的那个点basename somepath .txt
输出 somepath 的基本部分,从末尾删除 .txt(例如 /Data/file.txt
-> file
)/Data/*.txt
expands to the paths of the text files in /Data (including the /Data/ part)$( ... )
runs a shell command and inserts its output at that point in the command linebasename somepath .txt
outputs the base part of somepath, with .txt removed from the end (e.g. /Data/file.txt
-> file
)如果您需要使用 Data/file.txt
而不是 /Data/file.txt
运行 MyProgram,请使用 "${filename#/}"
删除前导斜杠.另一方面,如果你真的想扫描Data
而不是/Data
,只需使用作为Data/*.txt
中的文件名.
If you needed to run MyProgram with Data/file.txt
instead of /Data/file.txt
, use "${filename#/}"
to remove the leading slash. On the other hand, if it's really Data
not /Data
you want to scan, just use for filename in Data/*.txt
.