Using shell scripting for making queries on postgres sql


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using shell scripting for making queries on postgres sql
# 1  
Old 05-05-2014
Using shell scripting for making queries on postgres sql

I have a situation where I have a list of airplanes that make a series of flights.

Therefore I am looking up the different flights that each airplane makes based on a postgres sql query:

select flightid from plane where airplane='DELTAx'

As a result I get a series of flight numbers from this query.

Flight
------
123
567
8976
245
230
140

I currently have a script that puts these flights into an array:
Unix_Array=($(psql -h $PSQL_HOST $PSQL_RDB $PSQL_USER << EOFF
BEGIN TRANSACTION;
select flightid from plane where airplane='Deltax';
END TRANSACTION;
\q
EOFF
))
#echo ${Unix_Array[3]}
#echo ${Unix_Array[4]}
#echo ${Unix_Array[5]}
#echo ${Unix_Array[6]}



which gives an output of:

123
567
8976


Therefore Unix_Array[3]=123
Unix_Array[4]=567 and so on.

Now the second part of the script takes the flight numbers (Unix_Array[]) from the first script and should be getting a flight status from another data base called FlightReport

while [ $counter -lt $count ]
do
echo $counter
echo "flightid = ${Unix_Array[$counter]}"
Flight_Array=($(psql -h $PSQL_HOST $PSQL_RDB $PSQL_USER << EOFF
BEGIN TRANSACTION;
select status from FlightReport where flightid=${Unix_Array[$counter]};
END TRANSACTION;
\q
EOFF
))
counter=`expr $counter + 1`
done
echo "Status is ${Flight_Array[3]}"


The echo status should give me the output of the query

select status from FlightReport where flightid=123, since the value of Unix_Array[3]=123.

I am not getting any value here. Any ideas on what is going on here?

I want to be able to get flight status from FlightReport based on all the flightids that I have obtained from the plane data base. The flightids in plane and Flightreport are the same.

If anyone could help me with this that would greatly be appreciated.

Thanks,

Jason
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

2. Shell Programming and Scripting

Taking information from a postgres sql query and putting it into a shell script array

I have a postgres sql statement that is the following: select age from students; which gives me the entries: Age --- 10 15 13 12 9 14 10 which is about 7 rows of data. Now what I would like to do with this is use a shell script to create an array age. As a results... (3 Replies)
Discussion started by: JSNY
3 Replies

3. Shell Programming and Scripting

run sql queries from UNIX shell script.

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX? :confused: (1 Reply)
Discussion started by: 24ajay
1 Replies

4. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

5. Shell Programming and Scripting

Executing set of sql queries from shell script

Hi All, I tried executing set of queries from shell script but not able to capture the input query in the log file. The code looks something similar to below sqlplus user/pwd@dbname << EOF > output.log $(<inputfile.txt) EOF The above code is capturing the output of queries into... (9 Replies)
Discussion started by: loggedin.ksh
9 Replies

6. Shell Programming and Scripting

How to store results of multiple sql queries in shell variables in ksh?

Hi, I have a script where I make a sqlplus connection. In the script I have multiple sql queries within that sqlplus connection. I want the result of the queries to be stored in shell variables declared earlier. I dont want to use procedures. Is there anyway else. Thanks in advance.. Cheers (6 Replies)
Discussion started by: gonchusirsa
6 Replies

7. Shell Programming and Scripting

How to write cron job for calling sql function database is postgres

Hi, Please help me to write cron job for calling sql function daily. I have Postgres database. (1 Reply)
Discussion started by: kulbhushan
1 Replies

8. Shell Programming and Scripting

Nested SQL queries within Shell script

Hi, Would someone know if I can fire nested sql queries in a shell script? Basically what I am trying to do is as follows: my_sql=$(sqlplus -s /nolog<<EOF|sed -e "s/Connected. *//g" connect... (2 Replies)
Discussion started by: shrutihardas
2 Replies

9. UNIX for Dummies Questions & Answers

shell script for sql queries

Hi All, I have written 4 sql queries . Now I want to write one SHELL SCRIPTING program for all these queries... i.e 1.select * from head; 2. select * from detail; 3. delete from head; 4. delete from detail; Please let me know how to write a shell script... Thank you (1 Reply)
Discussion started by: user71408
1 Replies

10. UNIX for Dummies Questions & Answers

Sh Shell Script executing remote SQL queries

Hi there folks, I am trying to execute remote sql queries on an Oracle server. I would like to save the result of the executed sql queries on a text file, and send that text file as an attachment to an email address. Could anyone give me an idea on how the above could be achieved? Any help... (2 Replies)
Discussion started by: Javed
2 Replies
Login or Register to Ask a Question