I am running this bash script in order to connect to a PostgreSQL database, run some query, and simply check whether it returns an empty result or not. Then, I want to print out whether any rows were returned.
#!/bin/sh
DATABASE=dbname
USERNAME=user
HOSTNAME=somehost.com
export PGPASSWORD=password
queryResult () {
psql -h $HOSTNAME -U $USERNAME -d $DATABASE <<SQL
SELECT * FROM myTable WHERE valueA > 5.0 OR valueB > 5.0;
IF @@ROWCOUNT > 0 THEN 1 ELSE 0;
SQL
}
valuesFound=$(queryResult)
echo $valuesFound
There are two issues I'm having with this:
1) It stores the result of the first query (SELECT * FROM myTable...) into valuesFound and prints it, and I don't want that. All I care about is whether the IF statement returns 1 or 0.
2) The second query (IF @@ROWCOUNT...) throws a syntax error: Syntax error at or near IF