Solved

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

Posted on 2014-11-04
7
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…

728 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