bash脚本 - 检索每一个充满*
我有一个bash脚本,就像bash脚本 - 检索每一个充满*
for i in /path/to/file/*.in; do
./RunCode < "$i"
done
我希望能够捕捉到类似* .OUT输出值,带*号是一样的*。在。我如何检索*被扩展到的文本,以便我可以重用它?
更改文件后缀使用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.
啊,我只是没有在手册中寻找正确的东西。这很好,谢谢! – samwill 2014-09-27 22:38:37
通过在你的问题的措辞(可能是更清晰),我想你想删除前导路径。
您可以使用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
- 从name
删除尾随.in
扩展名,并替换为.out
。
我其实并不想去掉主要路径,但这真的很有帮助。我标记了另一个答案,因为这是我真正想要的,但是我赞成你。谢谢! – samwill 2014-09-27 22:40:57
很高兴帮助:) – 2014-09-28 00:39:06
你已经拥有了,它是'$ i'。 – 2014-09-27 20:50:50