开发者

Bash : redirect init.d output in variable

开发者 https://www.devze.com 2023-02-19 05:29 出处:网络
I try to redirect the output of an init.d command to a variabl开发者_运维问答e without displaying it on screen, but this does not work.

I try to redirect the output of an init.d command to a variabl开发者_运维问答e without displaying it on screen, but this does not work. For example this works :

$> var=`uname -a`
$> echo $var
$> Linux

But not this :

$> var=`/etc/init.d/nginx reload`
$> the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful
$> echo $var
$> Reloading nginx configuration: nginx.

How can we do to return nothing on the screen please? Thanks.

Nico


nginx -t writes those messages on standard error, not on standard output, so you need to capture those too:

# var=`nginx -t 2>&1`
# echo $var
the configuration file /etc/nginx/nginx.conf syntax is ok configuration file /etc/nginx/nginx.conf test is successful
#


It is entirely possible it is going to stderr instead of stdout. You could try something like this:

var=`/etc/init.d/nginx reload 2>&1`

to test.

0

精彩评论

暂无评论...
验证码 换一张
取 消