I'm having issues parsing the outputs of a postgres sql query from bash script.
Here is the output from a command line query to postgres:
root@API:/home# psql -U postgres -h pgipaddress -c "SELECT 1 AS __ROW__ FROM pg_roles WHERE rolname='postgres'"
__row__
---------
1
(1 row)
Here is what the variable $userexists looks like before you look at the conditions:
__row__ --------- 1 (1 row)
Here are my attempts at string conditional checking:
userexists=$(psql -U postgres -h ${pgip} -c "SELECT 1 AS __ROW__ FROM pg_roles WHERE rolname='postgres'")
if [ "${userexists}" == "__row__\n---------\n1 \n(1 row)"* ] ; then
printf "Database User exists\n"
else
printf "Database User does not exists\n"
fi
userexists=$(psql -U postgres -h ${pgip} -c "SELECT 1 AS __ROW__ FROM pg_roles WHERE rolname='postgres'")
if [ "${userexists}" == "__row__ --------- 1 (1 row)"* ] ; then
printf "Database User exists\n"
else
printf "Database User does not exists\n"
fi
All result in database user does not exist.
--quiet -tAflags, which would reduce your output to just1and nothing else?count()?