我有一个类似
的bash脚本for i in /path/to/file/*.in; do
./RunCode < "$i"
done
我希望能够以* .out之类的方式捕获输出,其中*与* .in相同。如何检索*展开的文本以便我可以重复使用它?
答案 0 :(得分:2)
根据你问题中的措辞(可能更清楚),我假设您希望删除主要路径。
您可以使用parameter expansion来完成您想要的任务:
out_dir="/path/out"
for i in /path/to/file/*.in; do
name="${i##*/}"
./RunCode < "$i" > "$out_dir/${name%.in}.out"
done
这将删除主要路径和.in
扩展名,将所有输出文件命名为.out
,并将其放在/path/out
目录中。
${i##*/}
- 删除最后一次出现/
的所有前导字符,以获取具有.in
扩展名的文件的名称。
${name%.in}.out
- 从.in
移除尾随的name
扩展名,并替换为.out
。
答案 1 :(得分:1)
使用bash更改文件后缀:
for i in /path/to/file/*.in; do
./RunCode < "$i" > "${i%.in}.out"
done
来自man bash
:
${parameter%word}
${parameter%%word}
Remove matching suffix pattern. The word is expanded to produce a
pattern just as in pathname expansion. If the pattern matches a
trailing portion of the expanded value of parameter, then the result
of the expansion is the expanded value of parameter with the shortest
matching pattern (the ``%'' case) or the longest matching pattern
(the ``%%'' case) deleted. If parameter is @ or *, the pattern removal
operation is applied to each positional parameter in turn, and the
expansion is the resultant list. If parameter is an array variable
subscripted with @ or *, the pattern removal operation is applied
to each member of the array in turn, and the expansion is the resultant
list.