问题原文
原帖链接 · PeterB · CC BY-SA 4.0
Apache's configtest is useful to catch syntax errors. On success it prints "Syntax OK" and when running apachectl configtest in a bash script I want to suppress this.
I have tried the usual output redirections to /dev/null, assigning the results to a variable like so:
AOUTPUT=$(/usr/sbin/apache2ctl -t && /usr/sbin/apache2ctl graceful)
but "Syntax OK" is always output.
Can this be suppressed?
已采纳解答
原帖链接 · meuh · CC BY-SA 4.0
Inside $() you can still redirect stderr to be the same as stdout, and then it will be captured by the assignment. For example:
if AOUTPUT=$(/usr/sbin/apache2ctl -t 2>&1)
then /usr/sbin/apache2ctl graceful
else rc=$?; printf "%s\n" "$AOUTPUT" >&2; exit $rc
fi
I added the >&2 so the errors still come out on stderr, and the exit to try to duplicate the original error code from the command, but you would only really need them if you were putting this in a shell script and wanted to preserve those features.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
