bash 如何将bash脚本的整个输出保存到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17554018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save entire output of bash script to file
提问by hcaw
I am trying to get the entire output of a bash script to save to a file. I currently have one argument (ip address) at the beginning of the code looks like this:
我正在尝试将 bash 脚本的整个输出保存到文件中。我目前在代码开头有一个参数(ip 地址)如下所示:
#!/bin/bash
USAGE="Usage: exec > ""
[<IP address>]"
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
ip_addr=
What I'd like to do is add another argument called "output", that the entire output of the script will save to. I'm aware I could just run myscript.sh | tee textfile.txt, but I'd like to make the script a little easier to run for others.
我想要做的是添加另一个名为“输出”的参数,脚本的整个输出将保存到该参数。我知道我可以直接运行myscript.sh | tee textfile.txt,但我想让脚本更容易为其他人运行。
Thanks in advance,
提前致谢,
hcaw
哈哈
回答by chepner
After the usage message, add the following line:
在使用消息之后,添加以下行:
myscript 192.0.2.42 output.txt
This will redirect standard output for the rest of the script to the file named in the 2nd argument.
这会将脚本其余部分的标准输出重定向到第二个参数中命名的文件。
Then run using
然后运行使用
##代码##
