如果您並不需要顯示命令的錯誤輸出,你可以不喜歡
if ! echo | mysql $dbcreds mysql; then
error "Could not connect to MySQL. Did you forget to add '--db-user=' or '--db-password='?"
die "Check your credentials or ensure server is running with /etc/init.d/mysqld status"
fi
在這個例子中,錯誤和模具定義功能。腳本中的其他地方。 $ dbcreds也被定義,儘管這是從命令行選項構建的。如果該命令沒有生成錯誤,則不返回任何內容。如果發生錯誤,文本將由此特定命令返回。
糾正我,如果我錯了,但我得到你真正希望做的東西有點超過費解的印象
[ `id -u` -eq '0' ] || die "Must be run as root!"
,你實際上是if語句之前搶奪用戶ID,然後執行測試。這樣做,你可以顯示結果,如果你選擇。這將是
UID=`id -u`
if [ $UID -eq '0' ]; then
echo "User is root"
else
echo "User is not root"
exit 1 ##set an exit code higher than 0 if you're exiting because of an error
fi
只是爲了完成答案''[[$ {PIPESTATUS [0]} -ne 0]] && handle_the_error' – jozxyqk 2014-07-29 04:49:59