0

I am trying to determine whether a database has been initialised by executing a query and examining the exit code. If the exit code is 0 then I know that the database query has been accepted and the database should be initialised.

I cannot use pg_isready because the postgres version is prior to 9.3 so I have developed the following code:

while (( $(su -l postgres -c "psql -d db_name -c 'select count(1) from table_name' > /dev/null 2>&1") != 0 )) 
        do
            echo "waiting for database to initialise"
            sleep 10
        done

Running $(su -l postgres -c "psql -d db_name -c 'select count(1) from table_name' > /dev/null 2>&1" on its own returns an exit code of 0 however I am getting an error message when it is used within the loop:

((: != 0 : syntax error: operand expected (error token is "!= 0 ")
3

You should be able to use the return value directly.

while ! cmd
do
    ...
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.