如何捕获从回声中传输的文件?

时间:2015-02-21 18:38:34

标签: bash unix pipe cat

我希望这会打印出foobar.txt的内容:

echo "~/sandbox/foobar.txt" | cat

但它只是打印到控制台:

~/sandbox/foobar.txt

我如何cat文件内容而不是打印文件名?

编辑:这是我实际尝试做的一个非人为的例子:

echo $RESULT \
  | grep "check file .*make.err" \
  | sed -e "s/.*check file '//" \
  | sed -e "s/'.*//" \
  | xargs cat

编辑2 :RESULT保存我脚本文件中上一个命令的输出。这可能是这样的:

runspec v6152 - Copyright 1999-2008 Standard Performance Evaluation Corporation
Using 'macosx' tools
Reading MANIFEST... 18357 files
Loading runspec modules................
Locating benchmarks...found 31 benchmarks in 6 benchsets.
Reading config file '/Users/<REDACTED>/spec/installation/config/<REDACTED>.cfg'
Benchmarks selected: 400.perlbench
Compiling Binaries
  Building 400.perlbench base macosx-ia32-iccifortv101-pgofast-static default: (build_base_macosx-ia32-iccifortv101-pgofast-static.0000)
Error with make 'specmake build': check file '/Users/<REDACTED>/spec/installation/benchspec/CPU2006/400.perlbench/build/build_base_macosx-ia32-iccifortv101-pgofast-static.0000/make.err'
  Command returned exit code 2
  Error with make!
*** Error building 400.perlbench
If you wish to ignore this error, please use '-I' or ignore errors.

The log for this run is in /Users/<REDACTED>/spec/installation/result/CPU2006.062.log
The debug log for this run is in /Users/<REDACTED>/spec/installation/result/CPU2006.062.log.debug

*
* Temporary files were NOT deleted; keeping temporaries such as
* /Users/<REDACTED>/spec/installation/result/CPU2006.062.log.debug
* (These may be large!)
*
runspec finished at Sat Feb 21 08:40:02 2015; 15 total seconds elapsed

2 个答案:

答案 0 :(得分:3)

要将stdin转换为参数,可以使用xargs:

echo ~/sandbox/foobar.txt | xargs cat

要防止出现特殊字符(引号和空格)问题,您可以使用xargs -0并传入\0个分隔文件:

printf "%s\0" ~/sandbox/foobar.txt | xargs -0 cat --

请注意,您不能像在示例中那样双重引用~。将~替换为用户的主目录是shell的工作,如果你双引号,则禁止这样做。

答案 1 :(得分:-1)

如果你想要foobar.txt的内容:

cat ~/sandbox/foobar.txt

或:

cat `echo ~/sandbox/foobar.txt`

这相当于(但过于复杂)

如果您希望将foobar.txt的内容视为要捕获的文件的文件名,您可以使用:

cat `cat ~/sandbox/foobar.txt`

反引号使得内部猫的结果显示为外部猫的参数。大笑猫!