Reputation: 1850
i run this script from the command line:
check_databse_exist=`mysql -u root --password=root -Bse 'show databases' | egrep -v 'information_schema|mysql'`
for db in $check_databse_exist; do
if [ "$db" == "test_traffic" ] ; then
exist=1
fi
done
if [ $exist -eq 1 ] ; then
#do other stuff
fi
exit 0
why is it giving:
[: 16: jobeet: unexpected operator
[: 16: jobeet_test: unexpected operator
[: 16: landpage_db: unexpected operator
[: 16: my_db: unexpected operator
[: 16: symfony2: unexpected operator
./cibuild: 24: [0: not found
i just want to loop and if found set exist = 1
thanks
Upvotes: 0
Views: 334
Reputation: 361585
if [$exist == 1] ; then
Due to a shell scripting quirk you need spaces around the square brackets. They're not optional.
if [ $exist == 1 ]; then
For what it's worth, you could refactor this a bit if you merely want to check for the existence of one table, and don't otherwise need $check_databse_exist
. The idea would be to to replace the for loop with a grep.
if mysql -u root --password=root -Bse 'show databases' | grep -qw test_traffic; then
# Database exists.
fi
grep -q
produces no output, it merely returns success or failure. grep -w
is optional but a good practice; it prevents a table like test_traffic2
from matching.
Upvotes: 3
Reputation: 25337
Change it to:
check_databse_exist=`mysql -u root --password=root -Bse 'show databases' | egrep -v 'information_schema|mysql'`
for db in $check_databse_exist; do
if [ "$db" == "test_traffic" ] ; then
exist=1
fi
done
if [ $exist -eq 1 ] ; then
#do other stuff
fi
exit 0
Upvotes: 1