小编How*_*hen的帖子

在控制台上输出 stderr 和 stdout 并同时将它们存储在一个文件中

我可以同时输出stdoutstderr控制台屏幕和存储他们中的一个上到日志文件?

我写了一个测试shell脚本:

#!/bin/sh

echo OUT! >&1
echo ERR! >&2
Run Code Online (Sandbox Code Playgroud)

我可以通过运行脚本在屏幕上输出它们:

$./test 
OUT!
ERR!
Run Code Online (Sandbox Code Playgroud)

我可以通过以下方式输出stderr并捕获stdout到日志文件中:

$./test | tee 1>log
ERR!

$cat log 
OUT!
Run Code Online (Sandbox Code Playgroud)

我只能通过以下方式输出所有内容stdoutstderro输入日志文件:

$./test 2>&1| tee 1>log

$cat log 
OUT!
ERR!
Run Code Online (Sandbox Code Playgroud)

我可以通过以下方式输出stdoutstderr并将它们全部捕获到日志文件中:

$./test 2>&1 | tee log
OUT!
ERR!

$cat log 
OUT!
ERR!
Run Code Online (Sandbox Code Playgroud)

我可以通过以下方式输出两者都可以捕获stdout到日志文件中:

$./test | tee 2>&1 log
ERR!
OUT!

$cat log 
OUT!
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. 如何只输出stdout并捕获stderr到文件中?(我试过./test|tee 2>log …

shell bash io-redirection

8
推荐指数
2
解决办法
2万
查看次数

如何使用 & for sudo

我知道如果我把&应用程序放在终端的末尾,它会在后台运行并且终端仍然可以接收命令,例如:

howchen@host:~
-> cutecom&
[1] 17269
howchen@host:~
Run Code Online (Sandbox Code Playgroud)

它可以弹出cutecom应用程序。但是,我需要用 root 运行这个应用程序,我不能输入:

howchen@host:~
-> su cutecom&
Run Code Online (Sandbox Code Playgroud)

如果我以前从未运行过某些应用程序su,它将输出如下:

howchen@host:~
-> su cutecom&
[1] 17404
howchen@host:~
-> No passwd entry for user 'cutecom'
Run Code Online (Sandbox Code Playgroud)

linux shell

0
推荐指数
1
解决办法
9500
查看次数

标签 统计

shell ×2

bash ×1

io-redirection ×1

linux ×1