我正在尝试在Debian上testing特定软件包版本的信息。 不过,我反对APT的结果似乎没有做任何事情。 我不知道如何编写bash命令:
if [[ $(apt-get -s install golang | grep "E: Unable to locate") ]]; then echo "problem" exit; fi if apt-get -s install golang | grep "E: Unable to locate" > /dev/null; then echo "problem" exit; fi OUTPUT=`apt-get -s install golang | grep --quiet "E: Unable to locate"` if [ -n "$OUTPUT" ]; then echo "problem" exit; fi
该消息与大多数错误消息一样,打印在stderr上。 您可以将stderrredirect到stdout:
if [[ $(apt-get -s install golang 2>&1 | grep "E: Unable to locate") ]]; then
但是,这样做的更好方法是:
if ! apt-get -s install golang > /dev/null then echo "problem" exit 1 fi
如果您希望用户的唯一错误消息是“问题”,则可以select使用> /dev/null 2>&1
。