Solved

storing response from SQL in an unix (csh) environment variable

Posted on 2014-11-04
7
297 Views
Last Modified: 2014-11-10
I'm trying to run a piece of SQL within a csh unix script (has to be csh, unfortunately) and have it set up as this:

set my_table_exists=`sqlplus -s uid/pwd << END\
set pagesize 0 feedback off verify off heading off echo off;\
select count(*) from all_objects where object_name = 'mytable'\
and OWNER = 'myowner';\
exit;\
END`

Open in new window


when I run the script it just returns:

Badly placed ()'s

I'm guessing it doesn't like the count(*) (?) but how can I get round this?

Any help much appreciated!
0
Comment
Question by:yelbow
  • 4
  • 3
7 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40423046
Are you sure that you get "Badly placed ()'s" with "count(*)" appearing inside the heredoc?

Normally, inside a heredoc, csh should not care about "()". Instead, "count(*)" should disappear as a whole, because csh tries to interpret it as a wildcarded filename.

A way around this (again, inside a heredoc) is enclosing the whole thing in double quotes:

set my_table_exists="`sqlplus -s uid/pwd << END\
set pagesize 0 feedback off verify off heading off echo off;\
select count(*) from all_objects where object_name = 'mytable'\
and OWNER = 'myowner';\
exit;\
END`"

Outside a heredoc enclosing 'count(*)' in single quotes should be sufficient.
0
 

Author Comment

by:yelbow
ID: 40423070
Hi,

Yep - definitely getting it whilst within the heredoc.

Tried it with surrounding the whole thing in double quotes and the error is the same.

If I amend it to be:

set my_table_exists="`sqlplus -s uid/pwd << END\
set pagesize 0 feedback off verify off heading off echo off;\
select owner from all_objects where object_name = 'mytable'\
and OWNER = 'myowner';\
exit;\
END`"

i.e. replace the count(*) with a field name - the error is different I get:

select: Command not found.
END: Command not found.

I'm stumped
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40423076
Please try without the backslashes at the line ends, they're not necessary.
Well, they shouldn't do any harm either, but who knows ...

It seems that only the first line (set pagesize ...) is read by sqlplus, the next line is interpreted by csh.
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40423083
I tried to reproduce your issue, but to no avail so far.
OK, my csh is rather old, tomorrow I'll try with a newer version.

My day is over now, sorry!  See you in a few hours!
0
 

Author Comment

by:yelbow
ID: 40423322
Thanks for your help, its much appreciated.

Interestingly, I ran the same script on a solaris server and all worked OK - its only when running on a redhat server that I'm having this trouble.

I may have to abandon doing it this way, but it really should work :)
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40423860
OK,

the fact that you're on Redhat (CentOS/Fedora would be the same) explains a lot.
"csh" in these distributions is actually "tcsh", which behaves differently from csh in some aspects.

Anyway, under tcsh I was able to reproduce your issue - and couldn't find a solution!

Taking my limited "tcsh" knowledge into account I must assume that running a heredoc inside backticks is not possible under that shell.

I fear you will have to find another way - perhaps you could create a temporary SQL file like this

echo "SQL statements" > temporary.sql
echo "more SQL" >> temporary.sql
...

and then try

my_table_exists=`sqlplus -s uid/pwd @temporary.sql`
rm temporary.sql

Sorry, that's all I can do for the moment, but perhaps there's a real tcsh expert around here?
0
 

Author Closing Comment

by:yelbow
ID: 40433820
It seems odd behaviour (to my untrained eye), but - yep - have changed it so that it creates temp sql and runs that instead.

Works a treat.

Many thanks for all your efforts :)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question