Skip to content

Instantly share code, notes, and snippets.

@amontalban
Forked from SpekkoRice/checkiftablexists
Last active August 11, 2016 21:44
Show Gist options
  • Save amontalban/bec2aad4b0cd33776d821e20b952662d to your computer and use it in GitHub Desktop.
Save amontalban/bec2aad4b0cd33776d821e20b952662d to your computer and use it in GitHub Desktop.
BASH: Check if MySQL Table exists
#!/bin/bash
checkIfMySQLTableExists() {
table=$1;
DB=$2;
if [ $(mysql -N -s -u root -p -e \
"select count(*) from information_schema.tables where \
table_schema='${DB}' and table_name='${table}';") -eq 1 ]; then
echo "Table ${table} exists! ...";
else
echo "Table ${table} does not exist! ..."
exit 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment