简体   繁体   English

如何在bash中打印以给定文件名作为stdin的文件内容?

[英]How can I print contents of file given filename as stdin in bash?

Is there a bash command like cat except that it takes the filename from stdin rather than the first argument? 是否有像cat这样的bash命令,除了它从stdin而不是第一个参数获取文件名之外? For example: 例如:

echo "/home/root/file.txt" | somecommand

would have the same output as cat /home/root/file.txt cat /home/root/file.txt输出相同

Found the answer: 找到了答案:

xargs cat

For example: 例如:

echo "/home/root/file.txt" | xargs cat

If the filename that you're expecting doesn't have a space: 如果您期望的文件名没有空格:

#!/bin/bash    
read filename
cat $filename

The read builtin shell command reads tokens from stdin into environment variables. read内置的shell命令将令牌从stdin读入环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM