3

I have the following bash script:

#!/bin/sh
psql -U postgres -c "CREATE DATABASE test TEMPLATE template0;"

if [ $? -eq 0 ]; then
    echo OK
else
    if [ $? = 'ERROR:  database "test" already exists' ]; then
         echo OK
    else
         echo FAIL
    fi
fi

It's currently failing with the following error:

testbox:/tmp# sh test.sh 
ERROR:  database "test" already exists
FAIL

I'm not sure where I've gone wrong. I need it to return "OK" for this specific error. Any other errors should print out a FAIL. Can you tell me where I've gone wrong?

Thanks.

EDIT 1

I've modified the code to capture the output, not just rc:

#!/bin/bash

output=$(psql -U postgres -c "CREATE DATABASE test TEMPLATE template0;")
ret=$?

if [[ $ret -eq 0 ]]; then
    echo OK
else
    if [[ $output == 'ERROR:  database "test" already exists' ]]; then
         echo OK
    else
         echo FAIL
    fi
fi

But I get this error:

ERROR:  database "test" already exists
sh: ERROR:  database "test" already exists: unknown operand
FAIL
3
  • 2
    $? represents just the integer exit code not the output of your psql command. You need to grab the output and check. Commented Dec 22, 2016 at 15:22
  • @anubhava ok. can you give me an example? Commented Dec 22, 2016 at 15:23
  • 1
    @Happydevdays: try adding echo $? immediately after the psql command, and you'll see what anubhava means (it'll just print a number, probably "1"). Then add a second echo $?, and you'll see that one print "0" because the first echo command succeeded, and therefore exited with a status code of 0. Commented Dec 22, 2016 at 15:31

3 Answers 3

5

$? represents just the integer exit code not the output of your psql command. You need to grab the output of psql command and check in if condition.

You can use:

#!/bin/bash

output=$(psql -U postgres -c "CREATE DATABASE test TEMPLATE template0;" 2>&1)
ret=$?

if [[ $ret -eq 0 ]]; then
    echo OK
else
    if [[ $output == *'already exists'* ]]; then
         echo OK
    else
         echo FAIL
    fi
fi
Sign up to request clarification or add additional context in comments.

5 Comments

I've changed first line to output=$(psql -U postgres -c "CREATE DATABASE test TEMPLATE template0;" 2>&1) so that we capture stderr also from psql command
That works now. Thank you. What can i google to better understand the "2>&1" at the end of the first line?
2>&1 redirects stderr to file descriptor 1 which is stdout
@anubhava I am having a similar issue but the return code is always 0 content=$(java -jar ${jarFilePath} -u ${username},${password} -n ${host} -c "${val_query}") rc=$? If the val_query has any syntax issue then how can I capture the error
0

The $? will output an integer based on exit code of last command executed. when the database exists it will be 1 returned. Thats why your script is failing.

Comments

0

A shorter way if checking if a database exists can be found here.

Based on that output you can create your database and return somehting meaningful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.