Reputation: 2553
I don't get this - if I check the exit status of a command in a function and store in a local variable, I always get the answer 0. From outside the function, I get the correct exit status.
#!/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"
Output is:
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
The status of 4 is the correct one.
Upvotes: 2
Views: 971
Reputation: 42458
The local
command is run after the service mysql status
command in your function. It is that which is returning 0. You are losing the return status of the service
command.
Split the local
statement into two:
local output
local mysql_status
output=`service mysql status`
mysql_status=$?
Upvotes: 7