bash 将 shell 脚本输出到控制台和文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43502402/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:02:51  来源:igfitidea点击:

output of shell script to console and file

linuxbashshellstdoutstderr

提问by

I have shell script in Linux like below

我在 Linux 中有 shell 脚本,如下所示

#!/bin/bash
LOG_LOCATION=/home/$USER/logs
exec > >(tee /home/$USER/logs/"") 2>&1

[ $# -ne 1 ] && { echo "Usage : 
exec 2>&1 | tee /home/logging/""
table ";exit 1; } table= TIMESTAMP=`date "+%Y-%m-%d"` touch /home/$USER/logs/${TIMESTAMP}.success_log touch /home/$USER/logs/${TIMESTAMP}.fail_log success_logs=/home/$USER/logs/${TIMESTAMP}.success_log failed_logs=/home/$USER/logs/${TIMESTAMP}.fail_log #Function to get the status of the job creation function log_status { status= message= if [ "$status" -ne 0 ]; then echo "`date +\"%Y-%m-%d %H:%M:%S\"` [ERROR] $message [Status] $status : failed" | tee -a "${failed_logs}" #echo "Please find the attached log file for more details" exit 1 else echo "`date +\"%Y-%m-%d %H:%M:%S\"` [INFO] $message [Status] $status : success" | tee -a "${success_logs}" fi } `hive -e "create table testing.${table} as select * from fishing.${table}"` cp /home/$USER/logs/"" /home/$USER/debug/"" g_STATUS=$? log_status $g_STATUS "Hive create ${table}" echo "***********************************************************************************************************************************************************************"

If I have this in my shell script

如果我的 shell 脚本中有这个

exec> /home/logging/"" 2>&1

Then I am getting logs only on console not on the redirected file.

然后我只在控制台上而不是在重定向文件上获取日志。

If I have this in my script

如果我的脚本中有这个

exec > >(tee trace.log) 2>&1

Then I am having logs on the redirected file but not on the console.

然后我在重定向的文件上有日志,但没有在控制台上。

How can I have logs both on console and redirected file

如何在控制台和重定向文件上都有日志

采纳答案by anubhava

You can use process substitution with execbuiltin:

您可以使用exec内置的进程替换:

#!/usr/bin/bash
date 2>&1 | tee ""

to redirect both stdout and stderr to a file as as well as show it in terminal.

将 stdout 和 stderr 重定向到一个文件并在终端中显示它。

回答by Alec Bennett

The purpose of the teecommand is specifically intended to direct the output to both a file and to the terminal, which is what it sounds like you're wanting. This can be replicated pretty easily with something like the following:

tee命令的目的是专门将输出定向到文件和终端,这听起来像是您想要的。这可以很容易地复制,如下所示:

script.sh:

脚本.sh:

##代码##

Then, running the command with ./script.sh abc.txtwill produce the output of the date command to the terminal as well as the file abc.txt

然后,运行命令 with./script.sh abc.txt将产生 date 命令的输出到终端以及文件abc.txt

In your case, exec 2>&1 | tee /home/logging/"$1"should correctly produce the results you want, but you will need to call the script with that argument carefully. That assumes the /home/loggingdirectory exists, and you call the script above with something like ./script log.txt

在您的情况下,exec 2>&1 | tee /home/logging/"$1"应该正确地产生您想要的结果,但您需要小心地使用该参数调用脚本。假设该/home/logging目录存在,并且您使用类似的内容调用上面的脚本./script log.txt