1

When using a bash script to execute remote MySQL queries, how can I know if the command was successful or not. Locally it would return an exit code. However, remotely it seems to send the query and if it is able to connect to the remote MySQL database it takes it as successful. Is there any way to view the output when executing remotely, like when executing locally. Here is the script:

#!/bin/bash

RemoveID=`mysql -u root -proot -h 192.168.1.56 -e "delete from table where ID = '$1'"`

2 Answers 2

6

SELECT statements will return number of rows into your shell variable.

For DELETE statements just append a SELECT ROW_COUNT() after your mysql query, so it would be in your example like:

RemoveID=`mysql -u root -proot -h 192.168.1.56 -e "delete from table where ID = '$1';select row_count()"`

echo $RemoveID ROW_COUNT() 1

0
1

You could run a follow up query to check for the deleted ID in the script.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .