简体   繁体   English

如何在bash脚本中将cat输出路径获取为变量

[英]How to get cat output path as a variable in bash script

I'm using cat to create a new file via a shell script. 我正在使用cat通过shell脚本创建一个新文件。 It looks something like: 看起来像:

./script.sh > output.txt

How can I access output.txt as a variable in my script. 如何在脚本中将output.txt作为变量访问。 I've tried $1 but that doesn't work. 我试过$1但不起作用。

The script looks something like: 该脚本看起来像:

#!/bin/sh

cat  << EOF
echo "stuff"
EOF

Since there doesn't apear to be an os-agnostic way to do this, is there a way I pass the output into the script as an argument and then save the cat results to a file inside the script? 既然似乎没有一种与操作系统无关的方法,那么有没有一种方法可以将输出作为参数传递到脚本中,然后将cat结果保存到脚本中的文件中?

So the command would look like: ./script.sh output.txt and I can access the output as $1. 所以命令看起来像: ./script.sh output.txt ,我可以将输出作为$ 1访问。 Is something like this possible? 这样的事情可能吗?

The Literal Question: Determining Where Your Stdout Was Redirected To 字面问题:确定您的标准输出被重定向到的位置

When a user runs: 用户运行时:

./yourscript >outfile

...they're telling their shell to open outfile for write, and connect it to the stdout of your script, before starting your script. ...他们告诉他们的shell打开outfile进行写入,然后启动脚本之前将其连接到脚本的stdout。 Consequently, all the operations on the filename are already finished when your script is started, so the name isn't passed to the script directly. 因此,启动脚本时,对文件名的所有操作均已完成,因此名称不会直接传递给脚本。

On Linux (only), you can access the location to which your stdout was redirected before your script was started through procfs: 在Linux(仅)上,您可以在通过procfs启动脚本之前访问将stdout重定向到的位置:

output_dest=$(readlink -f /dev/fd/1)

echo "My output is being written to $output_dest"

This is literally interrogating where your first file descriptor (which is stdout) is open to. 这实际上是在询问您的第一个文件描述符(即stdout)打开的位置。 Note that the results won't always be useful -- if your program is being piped into something else, for instance, it might be something like [pipe: 12345] . 请注意,结果并不总是有用的-例如,如果将程序通过管道传递到其他内容中,则可能类似于[pipe: 12345]

If you care about portability or robustness, you should generally write your software in such a way that it doesn't need to know or care where its stdout is being directed. 如果您关心可移植性或健壮性,通常应该以不需要知道或关心其stdout指向何处的方式编写软件。


The Best Practice: Redirecting Your Script's Stdout Yourself 最佳实践:自行重定向脚本的标准输出

Better practice, if you need an output filename that your script can access, is to accept that as an explicit argument: 更好的做法是,如果需要脚本可以访问的输出文件名,则可以接受它作为显式参数:

#!/bin/sh
#      ^^ note that that makes this a POSIX sh script, not a bash script

outfile=$1

exec >"$outfile"  # all commands below here have their output written to outfile

cat >>EOF
This is written to $outfile
EOF

...and then directing the user to pass the filename as an argument: ...然后指示用户将文件名作为参数传递:

./yourscript outfile
#!/bin/sh
outfile=$1
cat  << EOF > "$outfile"
echo "stuff"
EOF

With

./script.sh output.txt

You write to the file output.txt 您写入文件output.txt

Setting a default value, in case the user doesn't pass an argument, is left for a different question. 如果用户不传递参数,则设置默认值将留给其他问题。

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

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