3

How, in a bash script, could I tell whether an ingres database contains a particular table?

I think I want something like this:

sql $db << ENDSQL | grep 'TRUE' > /dev/null
select 'TRUE' from iitables where table_name like '$tablename%'
\p\g
ENDSQL
result=$?

but this is always returning 1 (not found). A literal 'mytable%' works, so I think it's something in my quoting (or lack of it).

The \p prints my SQL statement, and the $tablename is expanded even though it's inside the single quotes, so it's not that.

Any suggestions appreciated.

rojomoke
  • 3,765
  • 2
  • 21
  • 30
  • Your script worked for me, providing db was set. However it always returned 0 because of the \p - you're printing out the query, which includes the string TRUE which you're grepping for. – PaulM May 22 '15 at 16:24
  • @PaulM, but my problem is it's always returning 1, not 0. Taking the \p out makes no difference. – rojomoke May 26 '15 at 10:42

1 Answers1

0

Here document syntax is very picky, try a here statement instead (it's no less messy):

sql $db <<< "select 'TRUE' from iitables where table_name like '$tablename%'
\p\g" | grep 'TRUE' > /dev/null
result=$?

Note the <<< and the position of the double quotes.

cdarke
  • 42,728
  • 8
  • 80
  • 84