5

I have a script that runs several different psql statements. I'm trying to capture the error output from psql when password entered is incorrect. The password is entered in before the check (and when correct, the psql statements execute successfully)

I've tried the following:

pwcheck=`psql -q -U postgres -h $ip -d $database;`
echo "error message: $pwcheck"

When I enter an incorrect password to check it, the error messages are output but the variable is empty.

psql: FATAL:  password authentication failed for user "postgres"
FATAL:  password authentication failed for user "postgres"
error message:

Ideally, I'd like to save the error message to a variable and not print my own error message/prompt and not display the psql errors at all.

How can I store either of these error messages in a bash variable?

6

You can't, directly. At least, not without either commingling it with or discarding standard output. However, there is a way!

#!/bin/bash
errorlog=$(mktemp)
trap 'rm -f "$errorlog"' EXIT
pwcheck="$(psql -q -U postgres -h $ip -d $database 2> "$errorlog")"
if [[ 0 -ne $? ]]; then
    echo "Something went wrong; error log follows:"
    cat "$errorlog"
fi
| improve this answer | |
0

This captures the stdout & stderr but hides the exit code.

$ output=$(psql -c "select xow()" 2>&1 | cat)
$ echo $?
0
$ echo "$output"
ERROR:  function xow() does not exist
LINE 1: select xow()
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
$ 

This includes the exit code in the captured output.

$ output=$({ psql -c "select xow()" 2>&1; echo rc=$?; } | cat)
$ echo $?
0
$ echo "$output"
ERROR:  function xow() does not exist
LINE 1: select xow()
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
rc=1
$ 

This returns a non-zero exit code if psql command fails. NOTE: This is the exit code from grep, not from psql. In this case the values are the same but that's just a coincidence.

$ output=$( output=$({ psql -c "select xow()" 2>&1; echo rc=$?; } | cat); echo "$output" | grep 'rc=0'; rc2=$?; echo "$output"; exit $rc2 )
$ echo $?
1
$ echo "$output"
ERROR:  function xow() does not exist
LINE 1: select xow()
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
rc=1
$

Here is the same returning a zero exit for a successful psql command. NOTE: "rc=0" appears twice in the output.

$ output=$( output=$({ psql -c "select now()" 2>&1; echo rc=$?; } | cat); echo "$output" | grep 'rc=0'; rc2=$?; echo "$output"; exit $rc2 )
$ echo $?
0
$ echo "$output"
rc=0
              now              
-------------------------------
 2020-05-13 11:06:50.465941-04
(1 row)

rc=0
$ 
| improve this answer | |

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.