473,847 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to abort psql when backtick (shell) commands return non-zero?


I searched all through Google Groups, Google, and the Postgres docs, but
to no avail. I hope someone can help me out here!

I have a file that contains SQL, and some Postgres-specific slash commands.
This file calls an external shell script via the backtick mechanism, and
saves the value into a psql variable, for latter insertion into a table. It
looks something like this:

\set password '\'' `/dir/genPass user1` '\''


INSERT INTO TEST (password) VALUES (:password);

However, in some error scenarios, the /dir/genPass application fails, and
returns non-zero. However, in these cases, the stderr of the application
just gets stored in the variable, and hence inserted into my table as the
password. This is bad, and causes trouble latter... :)

BTW, I launch my "sql script" like so:

psql MY_DB < populatePasswor ds.sql

Question: how can I cause my populatePasswor ds.sql script to fail with an
error if the /dir/genPass command returns an error? I'm okay with any sort
of failure, as long as populatePasswor ds.sql stops executing and never gets
to the INSERT statement. It would also be nice if some sort of error would
print to the user ... ideally the stderr from the shell command (genPass).

Thank you for your time,
David Roche
Nov 23 '05 #1
0 1747

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Will Seay | last post by:
At the end of this message I've pasted a script we're trying to modify slightly. I don't believe it is VBscript or javascript but these are the closest groups I could find with my limited programming knowledge. Basically, we are trying to add a few lines to this script that will execute a few shell commands (see comments at the very end of the code). We think this may be ActionScript2 but aren't sure. If you can offer any help, or know...
by: David Garamond | last post by:
Can psql be told to exit immediately after an error (especially when doing commands from a file, -f)? This is the default behaviour of the mysql client, except when we give it -f option ("force"). The problem is, when restoring a dump, a failure at the some point might cause the subsequent commands to produce wrong results (e.g. I redefine a builtin function with a plruby function with different behaviour, but plruby failed to be...
by: Dino Vliet | last post by:
Hi folks, probably this is a question you've heard so many times but I wasn't able to find a solution to it. I'm using a shell script to create a textfile for me. It looks like #!/usr/local/bin/bash psql -c "select foo from bar;" -d database1 -t psql -c "\q" -d database1 exit 0
by: Ron St-Pierre | last post by:
I'm having a problem suppressing output from some of my cron scripts and java code. One file of sql scripts (eod-misc.sql) is called by a shell script (update.sh). Within eod-misc, various sql commands and home rolled functions are called eg SELECT * FROM myFunction(); which generates a lot of output (77000 lines for one of them), which gets logged and mailed to the cron user. If I change the line in the sql script to SELECT * FROM...
by: Ennio-Sr | last post by:
Hi all! I'm writing a script that presents the user with a numbered lines menu, each line corresponding to a <case n> which executes a psql command. As the psql-commands are very similar to each other (all of them have the structure: 1.- psql mydb -x -c "SELECT * FROM tb_nm WHERE col_nm LIKE '%$k_r%'" ) I thought it was possible to shorten it, initializing a str with said command at the beginning of the script and limiting the...
by: Russ Brown | last post by:
Hello, Today I tried connecting to my database locally via psql. I got the usual welcome & basic help messages, but it never got to the prompt: it just hung. So I checked top and the psql process was increasing in size at quite a rate (up to a gig in under 30 seconds). I'd been using psql with no problems only a couple of hours ago, and I haven't installed anything for at least a couple of days that I can think of.
by: John Sidney-Woollett | last post by:
With the advent of postgres v8, would it be possible to change the default behaviour of psql from AUTOCOMMIT=ON to AUTOCOMMIT=OFF ? Although this might break backward compatibility, it might be acceptable on the basis that v8 is such a major release. Also adding a new command line parameter to control the AUTOCOMMIT setting for those users that will experience broken scripts executed (especially using the -c command) might help ease...
by: Kevin Murphy | last post by:
This is a tip for the record in case it helps somebody else in the future. I have an import script that relies on a stored procedure that runs as a trigger on inserts into a temporary table. The script looks like this: -- create table -- ... -- define procedure and trigger
by: damacy | last post by:
hello, everyone. i am trying to write a program which executes SQL commands stored in ..sql files. i wrote a function called psql() whose contents look like the following. .... os.popen(command)
by: Bin Chen | last post by:
Hi, I want to do following: get a user input regex, then pass this as a parameter to grep, and then get the result from grep. Any code snip to implement the similar function? I am a python newbie. Thanks a lot. Bin
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.