Skip to content

Instantly share code, notes, and snippets.

@SpekkoRice
Created March 5, 2015 12:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save SpekkoRice/f88d8d7918b85dbfd85a to your computer and use it in GitHub Desktop.
Save SpekkoRice/f88d8d7918b85dbfd85a to your computer and use it in GitHub Desktop.
BASH: Check if MySQL Table exists
#!/bin/bash
checkIfMySQLTableExists() {
table=$1;
if [ $(mysql -N -s -u root -p -e \
"select count(*) from information_schema.tables where \
table_schema='${table}' and table_name='${DB}';") -eq 1 ]; then
echo "Table ${table} exists! ...";
else
echo "Table ${table} does not exist! ..."
exit 1
fi
}
@chriskonnertz
Copy link

chriskonnertz commented Feb 1, 2018

table_schema='${table}' and table_name='${DB}

I think you mixed up the vars, it should be:

table_schema='${DB}' and table_name='${table}

@jessuppi
Copy link

jessuppi commented Nov 8, 2021

if [[ $(mysql --execute "SHOW TABLES FROM ${DB_NAME} LIKE '${DB_PREFIX}options';") -gt 0 ]]; then

This is what we settled on for now in SlickStack, thanks for the inspiration:

https://github.com/littlebizzy/slickstack/blob/master/bash/ss-install-wordpress-config.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment