psql exit status varies for scripts on STDIN

From: "Jeffrey W(dot) Baker" <jwbaker(at)acm(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: psql exit status varies for scripts on STDIN
Date: 2004-09-14 04:50:17
Message-ID: 1095137416.4060.1.camel@localhost.localdomain
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

I just noticed something unexpected when using psql. I had written a
shell script to bulk-load some hundreds of files into a database, and
move each file to success/ or failure/ directories depending on the exit
status.

The problem is psql exit status differs depending on if the SQL script
is provided with -c or on STDIN. Try this for example:

$ echo "your mom" | psql
ERROR: syntax error at or near "your" at character 1
$ echo $?
0

The script failed, but the exit status is 0 (success) anyway.

$ psql -c "your mom"
ERROR: syntax error at or near "your" at character 1
$ echo $?
1

If the same command is given with -c, the exit status indicates failure.

The workaround in this case was to provide "COPY table FROM STDIN;" as
the -c COMMAND and to pipe the data in on STDIN. But simply adding the
COPY statement to the top of the input doesn't work right, with respect
to the exit status.

psql should probably give the same exit status in this case. Do you
agree?

-jwb

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2004-09-14 05:03:34 Re: how to use geometric shapes?
Previous Message Chester Kustarz 2004-09-14 04:46:24 Re: Corrupted Data?