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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Taking information from a postgres sql query and putting it into a shell script array
# 1  
Old 05-05-2014
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:

Code:
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 age[1]=10, age[2]=15, age[3]=13, age [4]=12, age[5]=9, age[6]=14, age[7]=10.

Any ideas on how to create a shell script.

I have been able to do the following:

Code:
Unix_Array=$(psql -h $PSQL_HOST $PSQL_RDB $PSQL_USER << EOFF
BEGIN TRANSACTION;
select age from students;
END TRANSACTION;
\q
EOFF
)
echo $Unix_Array

Unix_Array gives me all the ages at once like this:
Code:
10
15
13
12
9
14
10


but I cannot for example parse Unix_Array into individual variables such Unix_Array[1]=10, Unix[2]=15 and so on like I described above. Any ideas?

Thanks,

Jason


Last edited by Scrutinizer; 05-05-2014 at 07:04 AM.. Reason: code tags
# 2  
Old 05-05-2014
Code:
Unix_Array=($(psql -h $PSQL_HOST $PSQL_RDB $PSQL_USER << EOFF
BEGIN TRANSACTION;
select age from students;
END TRANSACTION;
\q
EOFF
))

print them using ${Unix_Array[0]}, ${Unix_Array[5]}
# 3  
Old 05-05-2014
taking array values for a new query

As I have previously mentioned I have a postgres sql statement:

I have a postgres sql statement that is the following:

select age from students;

which gives me the entries:

Code:
Age
---
10
15
13
12
9
14
10

which is about 7 rows of data.

I was able to get a reply as to how to set up an array so that age[1]=10, age[2]=15, age[3]=13, age[4]=12 and so on.

Now based on this array I want to make another query from another database that shares the field age with students.

Select grade level from characteristics where age=age[?].

These ages depend on the ages of the students in the students data base.

How can I set up a query in shell script to use age[1] trough age[7] from with the statement

Select grade level from characteristics where age=age[?]

where age[?] is age[1]-age[7]. I would want to do this query for the case that the age array would have had 50 or more entries as well.

Is there a while loop I could do. I did this attempt:

Code:
counter=1
While [counter -lt 7]
grade=$(psql -h $PSQL_HOST $PSQL_RDB $PSQL_USER << EOFF
BEGIN TRANSACTION;
      select grade_level from characteristcs where age=${age[$counter]}'
END TRANSACTION
\q
EOFF
)
counter = `expr $counter + 1`
done

Any ideas?

Last edited by Scrutinizer; 05-05-2014 at 07:06 AM.. Reason: code tags
# 4  
Old 05-05-2014
First, the array values start from 0
and, there are several ways of doing this

Code:
 
for i in $(seq 0 $((${#age[@]} - 1)))
do
grade_${i}=$(psql -h $PSQL_HOST $PSQL_RDB $PSQL_USER << EOFF
BEGIN TRANSACTION;
select grade_level from characteristcs where age=${age[$i]};
END TRANSACTION
\q
EOFF
)
done

The above approach is costly as it required one DB Connection for each value of the array age.

Rather, you can go with the below approach and run all the details into grade array
Code:
 
for i in $(seq 0 $((${#age[@]} - 1)))
do
echo "select grade_level from characteristcs where age=${age[$i]}"
done > query.sql
grade=($(psql -h $PSQL_HOST $PSQL_RDB $PSQL_USER -a -f query.sql))

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to execute sql query.

Hi Experts, Need your support. Not able to use sql query alias in shell script. Could you please help me in finding right way to use alias with sql query in shell script. Below is the code i am using. #!/bin/bash sqlplus -s abc/abc@abc << EOF> bcd.csv set trimspool on select zone_id... (4 Replies)
Discussion started by: as7951
4 Replies

2. Shell Programming and Scripting

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... (0 Replies)
Discussion started by: JSNY
0 Replies

3. Shell Programming and Scripting

How to embed sql query into our shell script?

Hi I would like to embed a sql query in my shell script. Also, before any the sql query is executed, i would like to validate username and password. (1 Reply)
Discussion started by: arghadeep adity
1 Replies

4. Shell Programming and Scripting

Convert the SQL Query in Shell Script

Hi All, I have a query with output below select 'create synonym "'||TABLE_NAME||'" for '||Table_owner||'."'||table_name||'"'||chr(59) from user_synonyms; ================== create synonym "RV_SBC_SIG" for WFCONTROLLER_TE."RV_SBC_SIG"; create synonym "AQM_TASK" for AWQM_TE."AQM_TASK";... (2 Replies)
Discussion started by: pvmanikandan
2 Replies

5. Red Hat

Sql query through shell script

hey , i am using this code to store value of a sql query and and then use it in other query but after some time , but it is not working. please help #!/bin/bash val_1=$( sqlplus -s rte/rted2@rel76d2 << EOF setting heading off select max(stat_id) from cvt_stats; exit EOF ) nohup... (5 Replies)
Discussion started by: ramsavi
5 Replies

6. UNIX for Dummies Questions & Answers

Regarding executing sql query in shell script

Hi, I have one SQL file prepared in UNIX and one script that is executing that. In SQL i have Update and create queries. I want to introduce conditions in SQL file (in UNIX) that if either of the create or update query failes whole transaction should be rollback. I just have 1 create... (2 Replies)
Discussion started by: abhii
2 Replies

7. Shell Programming and Scripting

$ symbol in sql query in shell script

Hi Team, Can you please help me to resolve this issue. Am unable to use this $ symbol in sql query in the shell script. For Example: # !/bin/sh export USER_NAME=XXX export PASSWORD=YYY export ORACLE_SID=xamdb echo $ORACLE_SID echo " Session Details ..." ... (1 Reply)
Discussion started by: indira_s
1 Replies

8. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

9. Shell Programming and Scripting

query sql using shell script

query sql using shell script, is it possible? my friend told me to do a file.sql and link to my shell script, but can i query sql using shell script? thanks in advance! (2 Replies)
Discussion started by: kingpeejay
2 Replies

10. UNIX for Dummies Questions & Answers

Executing a SQL query from a shell script

I cannot figure out how to run a SQL script, or just a sqlplus query, from a shell script (bash or ksh). Basically, I need to su - oracle from root and run a query, then test the exit status. (3 Replies)
Discussion started by: 98_1LE
3 Replies
Login or Register to Ask a Question