Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In attempting to verify if a remote database exists or not, I'm seeing mixed behavior with a conditional statement.

Technically the statement is working (it correctly reports if database is found), but on fail (no database found), it's throwing an error that I'm unable to trace for some reason.

REMOTE_EXISTS=$(mysql --login-path=$REMOTE_HOST --batch --skip-column-names -e "SHOW DATABASES LIKE '"$REMOTE_DB"';" | grep "$REMOTE_DB")

if [[ $? != 0 ]]; then
    die "Checking for $REMOTE_DB failed. Please report this error."
elif [[ $REMOTE_EXISTS ]]; then
    vrb "The database '$REMOTE_DB' has been found to exist on '$REMOTE_HOST'. Proceeding."
else
    die "Oops! We couldn't find '$REMOTE_DB' on the '$REMOTE_HOST' server. Are you sure it's there?"
fi

Unsure where I'm going wrong. Login details are being passed via stored configurations from mysql_config_editor, which leads me to believe it's something fundamentally wrong with how my condition or method of going about this is setup.

Unfortunately, the error is vague and being caught in a trap in my script, so reporting is a bit fuzzy. Also, I've tried the following as well:

[ "$REMOTE_EXISTS" -eq 0 ]

[[ "$REMOTE_EXISTS" ]]

No better luck.

Help is appreciated, thank you!

share|improve this question
    
Just to get you correctly: if the database is NOT found it neither goes to the first, nor the last option in your if-query, but rather fails somewhere else? – Fiximan Mar 7 at 8:31
    
What is vrb? What is die? (This should be Bash, not Perl, right?) – Dubu Mar 7 at 9:03
    
@Dubu vrb and die are helper functions from elsewhere in the script. Good question though. – Will Ashworth Mar 7 at 18:04
    
@Fiximan Confirming this. It seemed to be hitting the elif – Will Ashworth Mar 7 at 18:04
    
@WillAshworth so always on the elif? Can you be more specific about the outcomes in each situation? I would suggest replacing the commands in your if-query with echo 1 > test_outcome, echo 2 > test_outcome, and echo 3 > test_outcome respectively, to see which case is met in the respective situations. – Fiximan Mar 7 at 20:44
up vote 2 down vote accepted

I would try

mysql --login-path=$REMOTE_HOST --batch --skip-column-names -e "SHOW DATABASES LIKE '"$REMOTE_DB"';"  > /tmp/remote_db

if [[ $? != 0 ]]; then
    die "Checking for $REMOTE_DB failed. Please report this error."
elif grep -q "$REMOTE_DB" /tmp/remote_db ; then
    vrb "The database '$REMOTE_DB' has been found to exist on '$REMOTE_HOST'. Proceeding."
else
    die "Oops! We couldn't find '$REMOTE_DB' on the '$REMOTE_HOST' server. Are you sure it's there?"
fi
  • I am unsure you need to single quote REMOTE_XX var.
share|improve this answer
    
Interesting idea. Attempting and will post back. Thank you! – Will Ashworth Mar 7 at 18:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.