Skip to main content
added 357 characters in body
Source Link
JohnMudd
  • 260
  • 2
  • 8

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

$ output=$(psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres"select postgresxow()" 2>&1 | cat) $ echo $? 0 $ echo "$output" ERROR: databasefunction "rx30"xow() alreadydoes existsnot 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 "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres"select postgresxow()" 2>&1; echo rc=$?; } | cat) $ echo $? 0 $ echo "$output" ERROR: databasefunction "rx30"xow() alreadydoes existsnot 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 "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres"select postgresxow()" 2>&1; echo rc=$?; } | cat); echo "$output" | grep 'rc=0'; rc2=$?; echo "$output"; exit $rc2 ) $ echo $? 1 $ echo "$output" ERROR: databasefunction "rx30"xow() alreadydoes existsnot 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()" -U postgres postgres 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 $ 

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

$ output=$(psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1 | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists $ 

This includes the exit code in the captured output.

$ output=$({ psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1; echo rc=$?; } | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists 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 "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1; echo rc=$?; } | cat); echo "$output" | grep 'rc=0'; rc2=$?; echo "$output"; exit $rc2 ) $ echo $? 1 $ echo "$output" ERROR: database "rx30" already exists rc=1 $ 

Here is the same returning a zero exit for a successful psql command.

$ output=$( output=$({ psql -c "select now()" -U postgres postgres 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 $ 

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 $ 
added 122 characters in body
Source Link
JohnMudd
  • 260
  • 2
  • 8

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

$ output=$(psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1 | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists $ 

This includes the exit code in the captured output.

$ output=$({ psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1; echo rc=$?; } | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists 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 "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1; echo rc=$?; } | cat); echo "$output" | grep 'rc=0'; rc2=$?; echo "$output"; exit $rc2 ) $ echo $? 1 $ echo "$output" ERROR: database "rx30" already exists rc=1 $ 

Here is the same returning a zero exit for a successful psql command.

$ output=$( output=$({ psql -c "select now()" -U postgres postgres 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 $ 

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

$ output=$(psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1 | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists $ 

This includes the exit code in the captured output.

$ output=$({ psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1; echo rc=$?; } | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists rc=1 $ 

This returns a non-zero exit code if psql command fails.

$ output=$( output=$({ psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1; echo rc=$?; } | cat); echo "$output" | grep 'rc=0'; rc2=$?; echo "$output"; exit $rc2 ) $ echo $? 1 $ echo "$output" ERROR: database "rx30" already exists rc=1 $ 

Here is the same returning a zero exit for a successful psql command.

$ output=$( output=$({ psql -c "select now()" -U postgres postgres 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 $ 

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

$ output=$(psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1 | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists $ 

This includes the exit code in the captured output.

$ output=$({ psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1; echo rc=$?; } | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists 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 "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1; echo rc=$?; } | cat); echo "$output" | grep 'rc=0'; rc2=$?; echo "$output"; exit $rc2 ) $ echo $? 1 $ echo "$output" ERROR: database "rx30" already exists rc=1 $ 

Here is the same returning a zero exit for a successful psql command.

$ output=$( output=$({ psql -c "select now()" -U postgres postgres 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 $ 
deleted 390 characters in body
Source Link
JohnMudd
  • 260
  • 2
  • 8

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

$ output=$(psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1 | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists $ 

This includes the exit code in the captured output.

$ output=$({ psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1; echo rc=$?; } | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists rc=1 $ 

This returns a non-zero exit code if psql command fails.

$ output=$( output=$({ psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1; echo rc=$?; } | cat); echo "$output" | grep 'rc=0'; rc2=$?; echo "$output"; exit $rc2 ) $ echo $? 1 $ echo "$output" ERROR: database "rx30" already exists rc=1 $ 

Here is the same returning a zero exit for a successful psql command.

$ output=$( output=$({ psql -c "select now()" -U postgres postgres 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 $ 

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

$ output=$(psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1 | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists $ 

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

$ output=$(psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1 | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists $ 

This includes the exit code in the captured output.

$ output=$({ psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1; echo rc=$?; } | cat) $ echo $? 0 $ echo "$output" ERROR: database "rx30" already exists rc=1 $ 

This returns a non-zero exit code if psql command fails.

$ output=$( output=$({ psql -c "CREATE DATABASE rx30 ENCODING='UTF8'" -U postgres postgres 2>&1; echo rc=$?; } | cat); echo "$output" | grep 'rc=0'; rc2=$?; echo "$output"; exit $rc2 ) $ echo $? 1 $ echo "$output" ERROR: database "rx30" already exists rc=1 $ 

Here is the same returning a zero exit for a successful psql command.

$ output=$( output=$({ psql -c "select now()" -U postgres postgres 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 $ 
deleted 390 characters in body
Source Link
JohnMudd
  • 260
  • 2
  • 8
Loading
Source Link
JohnMudd
  • 260
  • 2
  • 8
Loading