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
Due to a shell scripting quirk you need spaces around the square brackets. They’re not optional.
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.grep -qproduces no output, it merely returns success or failure.grep -wis optional but a good practice; it prevents a table liketest_traffic2from matching.