I need to run a stored procedure using MySQL from bash. The stored procedure returns a variable and I need to check the value of the variable in bash to display it accordingly. If I echo the variable directly, it displays the value correctly, but if I check the value using if
, the if
condition is never satisfied and I don’t get 'YES' displayed in the following case.
To make things simple, I simplified the bash file like this:
result=$(mysql -h localhost -u$MYSQL_USER -p$MYSQL_PASSWORD -s -e "set @myVarible = 'YES';select @myVarible" $MYSQL_DATABASE)
if [[ $result == 'YES' ]] ; then
echo YES
fi
The output of echo -n "$result" | od -ctx1
is
0000000 Y E S \r
59 45 53 0d
0000004
;
beforefi
.result=$(mysql …)
command works, andecho $result
outputsYES
, but theif … fi
command doesn’t output anything? Doesecho $result
outputYES
or'YES'
? What happens if you put$result
in double quotes (e.g.,echo "$result"
)?if [[ $result =~ YES ]] ; then
echo -n "$result" | hexdump -C
.