Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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_Variables_Io Redirection_Command Substitution - Fatal编程技术网

bash:运行一次命令并将stdout和stderr保存在它们自己的变量中

bash:运行一次命令并将stdout和stderr保存在它们自己的变量中,bash,variables,io-redirection,command-substitution,Bash,Variables,Io Redirection,Command Substitution,我需要确保命令输出正确的文本。我目前有: command_stdout="$(mycommand --flag 2>/dev/null)" command_stderr="$(mycommand --flag 2>&1 1>/dev/null)" 不必运行同一个命令两次,我是否可以运行一次,但仍能将stdout和stderr的输出保存到相应的变量中?将stderr重定向到文件,然后将第二个变量设置为文件内容 command_st

我需要确保命令输出正确的文本。我目前有:

command_stdout="$(mycommand --flag 2>/dev/null)"
command_stderr="$(mycommand --flag 2>&1 1>/dev/null)"

不必运行同一个命令两次,我是否可以运行一次,但仍能将stdout和stderr的输出保存到相应的变量中?

将stderr重定向到文件,然后将第二个变量设置为文件内容

command_stdout="$(mycommand --flag 2>/tmp/stderr.$$)"
command_stderr="$(</tmp/stderr.$$)"
rm /tmp/stderr.$$
command\u stdout=“$(mycommand--flag 2>/tmp/stderr.$$)”

命令_stderr=“$”(将stderr重定向到文件,然后将第二个变量设置为文件内容

command_stdout="$(mycommand --flag 2>/tmp/stderr.$$)"
command_stderr="$(</tmp/stderr.$$)"
rm /tmp/stderr.$$
command\u stdout=“$(mycommand--flag 2>/tmp/stderr.$$)”

命令_stderr=”$(谢谢,这看起来很有效。有没有一种不用写文件的方法?想不出任何方法。一次只能分配一个变量。如果我知道更好的方法,你不认为我会把它放在答案中吗?为了对称,你可以将两个流重定向到文件,然后将它们都读入各自的变量中:)不,因为
命令\u stdout
赋值发生在子shell中,它在主shell中不可见。谢谢,这看起来很有效。有没有一种不用写文件的方法?想不出任何方法。一次只能赋值给一个变量。如果我知道更好的方法,你不认为我会把它放在ans中吗wer相反?为了对称,您可以将两个流重定向到文件,然后将它们都读入各自的变量:)不,因为
命令stdout
赋值发生在子shell中,它在主shell中不可见。和的可能重复和可能重复