1

I'm pretty new to bash shell script, I have the following:

CMD_MYSQL="${MYSQL_DIR}mysql --user=${MYSQL_USER} --pass=${MYSQL_PASS} ..."

if [ ($CMD_MYSQL --execute="SELECT COUNT(*) FROM information_schema.tables WHERE \
  table_schema='$MYSQL_DB' and table_name='${tablename}_new';") -eq 1 ]; then
  ...
else
  echo "Table ${tablename}_new does not exist."
fi

You can safely ignore other variables.

I have syntax error near $CMD_MYSQL after if, I suppose because I try to call the variable $CMD_MYSQL. How should I fix this? Thanks.

6
  • try echo "$CMD_MYSQL --execute="RENAME TABLE $tablename to ${tablename}_old"; Commented Feb 5, 2014 at 7:42
  • @RaghunandanKrishnamurthy Sorry, not that $CMD_MYSQL. Please see edited. The syntax error is in the if condition. Commented Feb 5, 2014 at 7:44
  • do you want to use the return code of the output of mysql? in the former case you don't need square brackets and -eq in the latter - you are missing $ before (MYSQL Commented Feb 5, 2014 at 7:45
  • @bobah Yes, I just want to test the output. Commented Feb 5, 2014 at 7:47
  • Use acute/backquote - acute <command> acute Commented Feb 5, 2014 at 7:47

2 Answers 2

1

I assume you want to test the output of the your command (and not its return code). Then the syntax should look like

CMD_MYSQL="${MYSQL_DIR}mysql --user=${MYSQL_USER} --pass=${MYSQL_PASS} ..."

if [ "$($CMD_MYSQL --execute="SELECT COUNT(*) FROM information_schema.tables WHERE \
  table_schema='$MYSQL_DB' and table_name='${tablename}_new';")" -eq 1 ]; then
  echo "Renaming ($MYSQL_DB.$tablename)..."
  $CMD_MYSQL --execute="RENAME TABLE $tablename to ${tablename}_old;"
else
  echo "Table ${tablename}_new does not exist."
fi

note the extra $ in front of the parentheses.

The parentheses, without $ in front, create a subshell. With the $, they substitute with the output of the command in the parentheses.

If your query does outputs a more complex result than just a number, you might need to search for the number inside the result with something like:

if [[ "$($CMD_MYSQL --execute="SELECT COUNT(*) FROM information_schema.tables WHERE \
  table_schema='$MYSQL_DB' and table_name='${tablename}_new';")" == *1* ]]; then
  echo "Renaming ($MYSQL_DB.$tablename)..."
  $CMD_MYSQL --execute="RENAME TABLE $tablename to ${tablename}_old;"
else
  echo "Table ${tablename}_new does not exist."
fi
Sign up to request clarification or add additional context in comments.

2 Comments

What does you query return exactly? If you get that error, it probably means your query returns several words, not a single number.
I guess I missed -N -s in my MySQL query. serverfault.com/a/334368/74192
0
CMD_MYSQL="${MYSQL_DIR}mysql --user=${MYSQL_USER} --pass=${MYSQL_PASS} ..."

if [ "**$CMD_MYSQL --execute="SELECT COUNT(*) FROM information_schema.tables WHERE \
  table_schema='$MYSQL_DB' and table_name='${tablename}_new';**" -eq 1 ]; then
  echo "Table ${tablename}_new **does** exist."
else
  echo "Table ${tablename}_new does **not** exist."
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.