Do something if mysql command fails

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I'm trying to get the below command to do something if sql is not true



for filename in test.txt
do
mysql -N -e "SELECT ROLE FROM SENTRY WHERE ROLE = '$filename';"
exitcode=$?

if [ $exitcode -ne 0 ]; then
echo did not succeed
else
echo succeeded
fi

done


When i run this script it returns:



success
success
success


Which is not true



test.txt contains



admin
test1
test2


select role from sentry returns



admin
user






share|improve this question















  • 3




    A query that returns no results is a success in mysql. You've made an assumption about mysql's exit code that does not hold.
    – cunninghamp3
    Apr 19 at 15:32










  • You are right. I need to figure out a way to "if sql not true do ..."
    – user3508766
    Apr 19 at 15:33










  • I do not have the ability to hit against a mysql database at the moment, but if your query remains simple, you can most likely grab the output of the mysql command and do some logic on that, rather than the exit code. SQL_OUTPUT=$(mysql -N -e "SELECT ...")
    – cunninghamp3
    Apr 19 at 15:56











  • im in the same boat right now, server is down. I think i can just throw mysql command in a variable and then "if $mysql =="" do. I think that checks for an empty variable
    – user3508766
    Apr 19 at 15:59










  • select count(sentry.role) from . . . and test for zero?
    – siloko
    Apr 19 at 16:02














up vote
0
down vote

favorite












I'm trying to get the below command to do something if sql is not true



for filename in test.txt
do
mysql -N -e "SELECT ROLE FROM SENTRY WHERE ROLE = '$filename';"
exitcode=$?

if [ $exitcode -ne 0 ]; then
echo did not succeed
else
echo succeeded
fi

done


When i run this script it returns:



success
success
success


Which is not true



test.txt contains



admin
test1
test2


select role from sentry returns



admin
user






share|improve this question















  • 3




    A query that returns no results is a success in mysql. You've made an assumption about mysql's exit code that does not hold.
    – cunninghamp3
    Apr 19 at 15:32










  • You are right. I need to figure out a way to "if sql not true do ..."
    – user3508766
    Apr 19 at 15:33










  • I do not have the ability to hit against a mysql database at the moment, but if your query remains simple, you can most likely grab the output of the mysql command and do some logic on that, rather than the exit code. SQL_OUTPUT=$(mysql -N -e "SELECT ...")
    – cunninghamp3
    Apr 19 at 15:56











  • im in the same boat right now, server is down. I think i can just throw mysql command in a variable and then "if $mysql =="" do. I think that checks for an empty variable
    – user3508766
    Apr 19 at 15:59










  • select count(sentry.role) from . . . and test for zero?
    – siloko
    Apr 19 at 16:02












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to get the below command to do something if sql is not true



for filename in test.txt
do
mysql -N -e "SELECT ROLE FROM SENTRY WHERE ROLE = '$filename';"
exitcode=$?

if [ $exitcode -ne 0 ]; then
echo did not succeed
else
echo succeeded
fi

done


When i run this script it returns:



success
success
success


Which is not true



test.txt contains



admin
test1
test2


select role from sentry returns



admin
user






share|improve this question











I'm trying to get the below command to do something if sql is not true



for filename in test.txt
do
mysql -N -e "SELECT ROLE FROM SENTRY WHERE ROLE = '$filename';"
exitcode=$?

if [ $exitcode -ne 0 ]; then
echo did not succeed
else
echo succeeded
fi

done


When i run this script it returns:



success
success
success


Which is not true



test.txt contains



admin
test1
test2


select role from sentry returns



admin
user








share|improve this question










share|improve this question




share|improve this question









asked Apr 19 at 15:27









user3508766

447




447







  • 3




    A query that returns no results is a success in mysql. You've made an assumption about mysql's exit code that does not hold.
    – cunninghamp3
    Apr 19 at 15:32










  • You are right. I need to figure out a way to "if sql not true do ..."
    – user3508766
    Apr 19 at 15:33










  • I do not have the ability to hit against a mysql database at the moment, but if your query remains simple, you can most likely grab the output of the mysql command and do some logic on that, rather than the exit code. SQL_OUTPUT=$(mysql -N -e "SELECT ...")
    – cunninghamp3
    Apr 19 at 15:56











  • im in the same boat right now, server is down. I think i can just throw mysql command in a variable and then "if $mysql =="" do. I think that checks for an empty variable
    – user3508766
    Apr 19 at 15:59










  • select count(sentry.role) from . . . and test for zero?
    – siloko
    Apr 19 at 16:02












  • 3




    A query that returns no results is a success in mysql. You've made an assumption about mysql's exit code that does not hold.
    – cunninghamp3
    Apr 19 at 15:32










  • You are right. I need to figure out a way to "if sql not true do ..."
    – user3508766
    Apr 19 at 15:33










  • I do not have the ability to hit against a mysql database at the moment, but if your query remains simple, you can most likely grab the output of the mysql command and do some logic on that, rather than the exit code. SQL_OUTPUT=$(mysql -N -e "SELECT ...")
    – cunninghamp3
    Apr 19 at 15:56











  • im in the same boat right now, server is down. I think i can just throw mysql command in a variable and then "if $mysql =="" do. I think that checks for an empty variable
    – user3508766
    Apr 19 at 15:59










  • select count(sentry.role) from . . . and test for zero?
    – siloko
    Apr 19 at 16:02







3




3




A query that returns no results is a success in mysql. You've made an assumption about mysql's exit code that does not hold.
– cunninghamp3
Apr 19 at 15:32




A query that returns no results is a success in mysql. You've made an assumption about mysql's exit code that does not hold.
– cunninghamp3
Apr 19 at 15:32












You are right. I need to figure out a way to "if sql not true do ..."
– user3508766
Apr 19 at 15:33




You are right. I need to figure out a way to "if sql not true do ..."
– user3508766
Apr 19 at 15:33












I do not have the ability to hit against a mysql database at the moment, but if your query remains simple, you can most likely grab the output of the mysql command and do some logic on that, rather than the exit code. SQL_OUTPUT=$(mysql -N -e "SELECT ...")
– cunninghamp3
Apr 19 at 15:56





I do not have the ability to hit against a mysql database at the moment, but if your query remains simple, you can most likely grab the output of the mysql command and do some logic on that, rather than the exit code. SQL_OUTPUT=$(mysql -N -e "SELECT ...")
– cunninghamp3
Apr 19 at 15:56













im in the same boat right now, server is down. I think i can just throw mysql command in a variable and then "if $mysql =="" do. I think that checks for an empty variable
– user3508766
Apr 19 at 15:59




im in the same boat right now, server is down. I think i can just throw mysql command in a variable and then "if $mysql =="" do. I think that checks for an empty variable
– user3508766
Apr 19 at 15:59












select count(sentry.role) from . . . and test for zero?
– siloko
Apr 19 at 16:02




select count(sentry.role) from . . . and test for zero?
– siloko
Apr 19 at 16:02















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438760%2fdo-something-if-mysql-command-fails%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438760%2fdo-something-if-mysql-command-fails%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?