![](/img/trans.png)
[英]First line of the output of a command within a bash script overriding the exit status
[英]bash script - exit status of previous command different if checked in a function
我不明白這一點-如果我檢查函數中命令的退出狀態並將其存儲在局部變量中,則總是得到答案0。從函數外部,可以獲得正確的退出狀態。
#!/bin/bash
function check_mysql()
{
local output=`service mysql status`
local mysql_status=$?
echo "local output=$output"
echo "local status=$mysql_status"
}
check_mysql
g_output=`service mysql status`
g_mysql_status=$?
echo "g output=$g_output"
echo "g status=$g_mysql_status"
輸出是:
local output=MySQL is running but PID file could not be found..failed
local status=0
g output=MySQL is running but PID file could not be found..failed
g status=4
狀態4是正確的。
local
命令在函數中的service mysql status
命令之后運行。 就是返回0的那個。您正在丟失service
命令的返回狀態。
將local
語句分成兩個部分:
local output
local mysql_status
output=`service mysql status`
mysql_status=$?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.