Reputation: 75
I get two files in the same directory:
foo.db
check.sh
in database foo.db
, there is a table called foo.db.1
, and in the check.sh
, I want to check whether the table exists:
#!/bin/sh
if ![ -e foo.db.1 ]; then
echo "foo.db.1 does not exist"
exit 1
fi
then I got an print to say foo.db.1 does not exist
. The script seems not working, but if I change the script to check whether the database exist instead of the table exist, it seems work:
#!/bin/sh
if ![ -e foo.db ]; then
echo "foo.db does not exist"
exit 1
fi
echo "foo.db does exist"
then I get the print to say 'foo.db does exist'
it seems I can check whether the database exist, but cannot check whether the table exist in the database, and I wonder how to do it?
Upvotes: 0
Views: 2633
Reputation: 141919
sqlite3 /database/path "SELECT EXISTS (SELECT * FROM sqlite_master WHERE type='table' AND name='<tableName>');"
will return either a 0
if the table does not exist or a 1
if it exists.
Upvotes: 2
Reputation: 247220
You need to use the sqlite3
program to read an sqlite3 db
if sqlite3 ./foo.db <<< .schema | grep -qi 'create table foo.db.1 '; then
echo "table foo.db.1 exists in foo.db"
fi
Upvotes: 1
Reputation: 75618
Use double brackets or place ! outside:
if [[ ! -e foo.db.1 ]]; then
echo "foo.db.1 does not exist"
exit 1
fi
if ! [ -e foo.db.1 ]; then
echo "foo.db.1 does not exist"
exit 1
fi
Upvotes: 1