Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8449633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:45:50+00:00 2026-06-10T10:45:50+00:00

I have a script which outputs some data from a postgres database: #!/bin/bash dbname=inventory

  • 0

I have a script which outputs some data from a postgres database:

#!/bin/bash

dbname="inventory"
username="postgres"
NOW=$(date +"%d-%m-%Y-%H%M%S")
OUTPUT="output.$NOW"

psql $dbname $username << EOF
\o | cat >> $OUTPUT
select count(*) from component;
select * from product where procode='AAA';
\q
\o
EOF

This happily exports the data I need to a text file if I run it on the local machine.

Now I want to do the same thing for 10 remote computers and have the output stored in one file on the machine I run the script from. I have the following code that will grep some information from the remote machines but I also need to add the postgres data above to my $FILE_OUT below:

#!/bin/bash

NOW=$(date +"%d-%m-%Y-%H%M%S")
dbname="inventory"
username="postgres"

echo "Enter the file name"
read FILE_IN
FILE_OUT="invcheck.$NOW"

for i in $(cat $FILE_IN); do
echo $i >> $FILE_OUT

ssh user@$i "ps -efw | grep TRADINGTIMEPLUS /opt/jrms/jrms.ini" >> $FILE_OUT

I have tried to put the postgres code in a line like:

ssh rmsasi@hj$i "psql $dbname $username << EOF \o | cat >> $FILE_OUT select * from component where code='1000';\q;\o; EOF"

but I get errors and the postgres data is not included in the $FILE_OUT

psql: warning: extra option o ignored
cat: from: No such file or directory
cat: component: No such file or directory
cat: where: No such file or directory
cat: code=1000: No such file or directory
bash: line 1: q: command not found
bash: line 1: o: command not found
bash: line 1: EOF: command not found

I am only new to scripting so hopefully I am just missing something simple.

My thanks in advance and if there is a better way to do what I am trying to achieve I appreciate any pointers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:45:52+00:00Added an answer on June 10, 2026 at 10:45 am

    psql‘s \ commands are terminated by newlines, not semicolons. A bit of a wart IMO.

    Because of that you need newlines in your command. Literal newlines are permissible within a quoted literal in the shell, so you could insert them in the command, but it’s better to just have your here-document locally and send it to psql‘s stdin via ssh‘s stdin. This’ll still leave the output on the remote machine, though:

    ssh rmsasi@hj$i "psql $dbname $username" << __EOF__
     \o | cat >> $FILE_OUT
    SELECT * from component where code='1000';
    \q
    \o
    __EOF__
    

    It’s much better to instead read from psql‘s stdout or redirect it, rather than using \o commands; that way the output comes back down the ssh session and you can redirect it locally:

    echo "SELECT * from component where code='1000';" |\
    ssh rmsasi@hj$i >> $FILE_OUT \
      "psql --quiet --no-align --no-readline --tuples-only -P footer=off --no-password $dbname $username"
    

    Alternately you could have psql connect over the network from the other machines.

    Since you’re driving psql from bash you may want to know that you can do so interactively with a co-process. You also don’t need to redirect output to a tempfile like that; just suppress psql‘s printing of prompts and headers (see linked answer) and you can simply read the results from psql‘s stdout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script which outputs lots of data, sample is as below.
I have one groovy script which print some statistics: println: ... now I have
I have a CSV export script (enrdata_arch.php) which calls information from an existing database
I have a bash script that processes some data using inotify-tools to know when
I have a shell script which outputs all of the .js and .html files
i have a script which is for virtual keyboard, i am facing some problem
I have VB Script which is connected with the my database (SQL Server 2008),
I'm creating a bash script to generate some output from a CSV file (I
I have written a Perl CGI script which fetches an html page from a
I have about 2000 documents from which I'm trying to pull metadata. Right now,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.