Nik*_*hhi 27 io-redirection xargs
我想运行:
./a.out < x.dat > x.ans
Run Code Online (Sandbox Code Playgroud)
对于目录A中的每个 * .dat文件。
当然,它可以通过 bash/python/任何脚本来完成,但我喜欢写性感的单行。我所能达到的只有(仍然没有任何标准输出):
ls A/*.dat | xargs -I file -a file ./a.out
Run Code Online (Sandbox Code Playgroud)
但是-axargs 中的不理解 replace-str 'file'。
谢谢你的帮助。
roz*_*acz 36
首先,不要将ls输出用作文件列表。使用外壳扩展或find. 请参阅下面的ls+xargs误用的潜在后果和正确xargs使用的示例。
如果您只想处理 下的文件A/,那么一个简单的for循环就足够了:
for file in A/*.dat; do ./a.out < "$file" > "${file%.dat}.ans"; done
Run Code Online (Sandbox Code Playgroud)
ls | xargs 呢?这是一个示例,说明如果您使用lswithxargs来完成工作,事情可能会变得多么糟糕。考虑以下场景:
首先,让我们创建一些空文件:
$ touch A/mypreciousfile.dat\ with\ junk\ at\ the\ end.dat
$ touch A/mypreciousfile.dat
$ touch A/mypreciousfile.dat.ans
Run Code Online (Sandbox Code Playgroud)查看文件并且它们不包含任何内容:
$ ls -1 A/
mypreciousfile.dat
mypreciousfile.dat with junk at the end.dat
mypreciousfile.dat.ans
$ cat A/*
Run Code Online (Sandbox Code Playgroud)使用xargs以下命令运行魔术命令:
$ ls A/*.dat | xargs -I file sh -c "echo TRICKED > file.ans"
Run Code Online (Sandbox Code Playgroud)结果:
$ cat A/mypreciousfile.dat
TRICKED with junk at the end.dat.ans
$ cat A/mypreciousfile.dat.ans
TRICKED
Run Code Online (Sandbox Code Playgroud)所以你刚刚设法覆盖了mypreciousfile.dat和mypreciousfile.dat.ans。如果这些文件中有任何内容,它就会被删除。
xargs :正确的方式 find 如果您想坚持使用xargs,请使用-0(null-terminated names) :
find A/ -name "*.dat" -type f -print0 | xargs -0 -I file sh -c './a.out < "file" > "file.ans"'
Run Code Online (Sandbox Code Playgroud)
注意两点:
.dat.ans结尾的文件;"。这两个问题都可以通过不同的shell调用方式来解决:
find A/ -name "*.dat" -type f -print0 | xargs -0 -L 1 bash -c './a.out < "$0" > "${0%dat}ans"'
Run Code Online (Sandbox Code Playgroud)
find ... -exec find A/ -name "*.dat" -type f -exec sh -c './a.out < "{}" > "{}.ans"' \;
Run Code Online (Sandbox Code Playgroud)
这再次产生.dat.ans文件,如果文件名包含". 为此,请使用bash并更改调用方式:
find A/ -name "*.dat" -type f -exec bash -c './a.out < "$0" > "${0%dat}ans"' {} \;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31404 次 |
| 最近记录: |