如何保存bash脚本的全部输出到文件 [英] How to save entire output of bash script to file

查看:908
本文介绍了如何保存bash脚本的全部输出到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个bash脚本的整个输出保存到一个文件中。我现在有一个参数(IP地址)在code的开头是这样的:

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:

#!/bin/bash

USAGE="Usage: $0 [<IP address>]"

if [ "$#" == "0" ]; then
        echo "$USAGE"
        exit 1
fi
ip_addr=$1

我想要做的就是添加一个名为输出另一种说法,即脚本的整个输出将保存到。我知道我可以只运行 myscript.sh |三通TextFile.txt的,但我想使脚本变得更容易为他人经营。

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.

由于提前,

hcaw

推荐答案

用法消息后,添加以下行:

After the usage message, add the following line:

exec > "$2"

这将重定向标准输出为脚本的其余部分在第二参数命名的文件。

This will redirect standard output for the rest of the script to the file named in the 2nd argument.

然后,使用运行

myscript 192.0.2.42 output.txt

这篇关于如何保存bash脚本的全部输出到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆