Sponsored Content
Top Forums Shell Programming and Scripting Using shell scripting for making queries on postgres sql Post 302900228 by JSNY on Monday 5th of May 2014 03:15:43 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
TAP::Parser::SourceHandler::pgTAP(3pm)			User Contributed Perl Documentation		    TAP::Parser::SourceHandler::pgTAP(3pm)

Name
       TAP::Parser::SourceHandler::pgTAP - Stream TAP from pgTAP test scripts

Synopsis
       In Build.PL for your application with pgTAP tests in t/*.pg:

	 Module::Build->new(
	     module_name	=> 'MyApp',
	     test_file_exts	=> [qw(.t .pg)],
	     use_tap_harness	=> 1,
	     tap_harness_args	=> {
		 sources => {
		     Perl  => undef,
		     pgTAP => {
			 dbname   => 'try',
			 username => 'postgres',
			 suffix   => '.pg',
		     },
		 }
	     },
	     build_requires	=> {
		 'Module::Build'		     => '0.30',
		 'TAP::Parser::SourceHandler::pgTAP' => '3.19',
	     },
	 )->create_build_script;

       If you're using "prove":

	 prove --source Perl 
	       --ext .t --ext .pg 
	       --source pgTAP --pgtap-option dbname=try 
			      --pgtap-option username=postgres 
			      --pgtap-option suffix=.pg

       If you have only pgTAP tests, just use "pg_prove":

	 pg_prove --dbname try --username postgres

       Direct use:

	 use TAP::Parser::Source;
	 use TAP::Parser::SourceHandler::pgTAP;

	 my $source = TAP::Parser::Source->new->raw('mytest.pg');
	 $source->config({ pgTAP => {
	     dbname   => 'testing',
	     username => 'postgres',
	     suffix   => '.pg',
	 }});
	 $source->assemble_meta;

	 my $class = 'TAP::Parser::SourceHandler::pgTAP';
	 my $vote  = $class->can_handle( $source );
	 my $iter  = $class->make_iterator( $source );

Description
       This source handler executes pgTAP tests. It does two things:

       1.  Looks at the TAP::Parser::Source passed to it to determine whether or not the source in question is in fact a pgTAP test
	   ("can_handle").

       2.  Creates an iterator that will call "psql" to run the pgTAP tests ("make_iterator").

       Unless you're writing a plugin or subclassing TAP::Parser, you probably won't need to use this module directly.

   Testing with pgTAP
       If you just want to write tests with pgTAP <http://pgtap.org/>, here's how:

       o   Build your test database, including pgTAP. It's best to install it in its own schema. To build it and install it in the schema "tap",
	   do this (assuming your database is named "try"):

	     make TAPSCHEMA=tap
	     make install
	     psql -U postgres -d try -f pgtap.sql

       o   Write your tests in files ending in .pg in the t directory, right alongside your normal Perl .t tests. Here's a simple pgTAP test to
	   get you started:

	     BEGIN;

	     SET search_path = public,tap,pg_catalog;

	     SELECT plan(1);

	     SELECT pass('This should pass!');

	     SELECT * FROM finish();
	     ROLLBACK;

	   Note how "search_path" has been set so that the pgTAP functions can be found in the "tap" schema. Consult the extensive pgTAP
	   documentation <http://pgtap.org/documentation.html> for a comprehensive list of test functions.

       o   Run your tests with "prove" like so:

	     prove --source Perl 
		   --ext .t --ext .pg 
		   --source pgTAP --pgtap-option dbname=try 
				  --pgtap-option username=postgres 
				  --pgtap-option suffix=.pg

	   This will run both your Perl .t tests and your pgTAP .pg tests all together. You can also use pg_prove to run just the pgTAP tests like
	   so:

	     pg_prove -d try -U postgres t/

       o   Once you're sure that you've got the pgTAP tests working, modify your Build.PL script to allow ./Build test to run both the Perl and
	   the pgTAP tests, like so:

	     Module::Build->new(
		 module_name	    => 'MyApp',
		 test_file_exts     => [qw(.t .pg)],
		 use_tap_harness    => 1,
		 configure_requires => { 'Module::Build' => '0.30', },
		 tap_harness_args   => {
		     sources => {
			 Perl  => undef,
			 pgTAP => {
			     dbname   => 'try',
			     username => 'postgres',
			     suffix   => '.pg',
			 },
		     }
		 },
		 build_requires     => {
		     'Module::Build'			 => '0.30',
		     'TAP::Parser::SourceHandler::pgTAP' => '3.19',
		 },
	     )->create_build_script;

	   The "use_tap_harness" parameter is optional, since it's implicitly set by the use of the "tap_harness_args" parameter. All the other
	   parameters are required as you see here. See the documentation for "make_iterator()" for a complete list of options to the "pgTAP" key
	   under "sources".

	   And that's it. Now get testing!

METHODS
Class Methods "can_handle" my $vote = $class->can_handle( $source ); Looks at the source to determine whether or not it's a pgTAP test and returns a score for how likely it is in fact a pgTAP test file. The scores are as follows: 1 if it's not a file and starts with "pgsql:". 1 if it has a suffix equal to that in the "suffix" config 1 if its suffix is ".pg" 0.8 if its suffix is ".sql" 0.75 if its suffix is ".s" The latter two scores are subject to change, so try to name your pgTAP tests ending in ".pg" or specify a suffix in the configuration to be sure. "make_iterator" my $iterator = $class->make_iterator( $source ); Returns a new TAP::Parser::Iterator::Process for the source. "$source->raw" must be either a file name or a scalar reference to the file name -- or a string starting with "pgsql:", in which case the remainder of the string is assumed to be SQL to be executed inside the database. The pgTAP tests are run by executing "psql", the PostgreSQL command-line utility. A number of arguments are passed to it, many of which you can affect by setting up the source source configuration. The configuration must be a hash reference, and supports the following keys: "psql" The path to the "psql" command. Defaults to simply "psql", which should work well enough if it's in your path. "dbname" The database to which to connect to run the tests. Defaults to the value of the $PGDATABASE environment variable or, if not set, to the system username. "username" The PostgreSQL username to use to connect to PostgreSQL. If not specified, no username will be used, in which case "psql" will fall back on either the $PGUSER environment variable or, if not set, the system username. "host" Specifies the host name of the machine to which to connect to the PostgreSQL server. If the value begins with a slash, it is used as the directory for the Unix-domain socket. Defaults to the value of the $PGDATABASE environment variable or, if not set, the local host. "port" Specifies the TCP port or the local Unix-domain socket file extension on which the server is listening for connections. Defaults to the value of the $PGPORT environment variable or, if not set, to the port specified at the time "psql" was compiled, usually 5432. "pset" Specifies a hash of printing options in the style of "pset" in the "psql" program. See the psql documentation <http://www.postgresql.org/docs/current/static/app-psql.html> for details on the supported options. See Also o TAP::Object o TAP::Parser o TAP::Parser::IteratorFactory o TAP::Parser::SourceHandler o TAP::Parser::SourceHandler::Executable o TAP::Parser::SourceHandler::Perl o TAP::Parser::SourceHandler::File o TAP::Parser::SourceHandler::Handle o TAP::Parser::SourceHandler::RawTAP o pgTAP <http://pgtap.org/> Support This module is managed in an open GitHub repository <http://github.com/theory/tap-parser-sourcehandler-pgtap/>. Feel free to fork and contribute, or to clone "git://github.com/theory/tap-parser-sourcehandler-pgtap.git" and send patches! Found a bug? Please post <http://github.com/theory/tap-parser-sourcehandler-pgtap/issues> or email <mailto:bug-tap-parser-sourcehandler- pgtap@rt.cpan.org> a report! Author David E. Wheeler <dwheeler@cpan.org> Copyright and License Copyright (c) 2010-2011 David E. Wheeler. Some Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2012-06-10 TAP::Parser::SourceHandler::pgTAP(3pm)
All times are GMT -4. The time now is 03:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy