Redirecting command output to a variable in bash fails

前端 未结 2 1893
醉酒成梦
醉酒成梦 2020-12-09 08:13

I\'m trying to redirect command output to a variable:

OUTPUT=$(sudo apache2ctl configtest)

and then to read it:

echo $OUTPU         


        
相关标签:
2条回答
  • 2020-12-09 08:43

    maybe the output goes to stderr, not stdout?

    try this: OUTPUT=$(sudo apache2ctl configtest 2>&1)

    0 讨论(0)
  • 2020-12-09 08:55

    For nginx possible situation when configtest can be successful with error in config files. Example:

    nginx: [warn] conflicting server name "test.com" on 0.0.0.0:80, ignored
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    

    for correct check errors in bash scripts need use:

    if [[ $((sudo /sbin/service nginx configtest) 2>&1 | grep "failed\|warn" ) ]]; then
        echo "ERROR!!!"
    else
        echo "OK!!!"
    fi
    
    0 讨论(0)
提交回复
热议问题