Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash Stdout&;使用变量的Stderr重定向_Bash_Shell_Variables_Stdout_Stderr - Fatal编程技术网

Bash Stdout&;使用变量的Stderr重定向

Bash Stdout&;使用变量的Stderr重定向,bash,shell,variables,stdout,stderr,Bash,Shell,Variables,Stdout,Stderr,我试图在我的脚本中创建两个变量,将错误和输出重定向到文件,并在屏幕上仅显示错误,其他变量仅在屏幕上显示输出。当我把它作为变量时,它就不起作用了。变量将变为空。有什么帮助吗 #!/bin/bash timestamp="`date +%Y%m%d%H%M%S`" displayonlyerror="2>&1 >> /tmp/postinstall_output_$timestamp.log | tee -a /tmp/postinstall_output_$timesta

我试图在我的脚本中创建两个变量,将错误和输出重定向到文件,并在屏幕上仅显示错误,其他变量仅在屏幕上显示输出。当我把它作为变量时,它就不起作用了。变量将变为空。有什么帮助吗

#!/bin/bash
timestamp="`date +%Y%m%d%H%M%S`"
displayonlyerror="2>&1 >> /tmp/postinstall_output_$timestamp.log | tee -a /tmp/postinstall_output_$timestamp.log"
displayoutput="2>&1 |tee -a /tmp/postinstall_output_$timestamp.log"

echo "No screen session found" $displayoutput
ech "No screen session found" $displayerror


它不会像那样工作,shell在对其他令牌执行扩展之前解析重定向。相反,将
displayonlyerror
displayoutput
定义为以下函数(
file
是日志文件名,您可以更改它):

然后像这样使用它们:

displayoutput cmd args
displayonlyerror cmd args

那最好的处理方法是什么呢?非常感谢。我已经有了一个名为color的函数,可以在执行命令时突出显示错误。在执行之前,将使用下面的内容<代码>颜色()3> &1
color command1 2>&1|tee-a/tmp/postinstall_output_$timestamp.log
自从我们创建了另外两个函数来存储stderr和stdout以来,我们现在如何调用?有没有一种方法可以一次调用两个函数?您是否尝试过
显示输出color cmd args
或类似的方法?它奏效了。我尝试了与之相反的方法,比如彩色显示输出命令…非常感谢:-)
displayoutput cmd args
displayonlyerror cmd args