我有一個bash腳本,就像bash腳本 - 檢索每一個充滿*
for i in /path/to/file/*.in; do
./RunCode < "$i"
done
我希望能夠捕捉到類似* .OUT輸出值,帶*號是一樣的*。在。我如何檢索*被擴展到的文本,以便我可以重用它?
我有一個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