[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Shell script: How can I populate a shell script variable with result of sql statement

Posted on 2009-02-09
6
Medium Priority
?
594 Views
Last Modified: 2013-12-26
I want to populate a shell script variable with the result of a select count(*) sql statement. At the moment I can select the results and write them to a file but now I just want to get the count and populate a shell script varaiable with the value

i.e. I want the variable 'COUNT' to be populated with the result of 'Select count(*) from Table'

I'm a newcomer to shell scripting to so any help would be greatly appreciated

Thanks
0
Comment
Question by:GlobexCorp
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:mahome
ID: 23591455
COUNT=3 or export count=3, if you want call other scripts which want to use it. If you post your exisiting script we can give more hints.

$ COUNT=3
$ echo $COUNT
3

Open in new window

0
 
LVL 40

Accepted Solution

by:
omarfarid earned 2000 total points
ID: 23591457
try

COUNT=`sqlplus user/pass <<END
select count(*) from Table
END`

0
 
LVL 2

Author Comment

by:GlobexCorp
ID: 23591597
Thanks for the reply

I'm not sure of the username/password for the db, I'm working from someone elses code. I'm already accessing the DB so is there anyway of using that code without the inclusion of the user/pass?

Thanks
0
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
LVL 40

Expert Comment

by:omarfarid
ID: 23591694
I am not aware of any other method to access database without username and password
0
 
LVL 2

Author Comment

by:GlobexCorp
ID: 23591802
At the moment I'm successfully outputting a sql select to a file without using username/password:

'CODE=echo "select max(col) from TestTable where col like 'A%'" | ac_bl -qs -|  ~/developers/bin/runparallel -l20000 -n30","' >> Test_File.csv

I'm not using the user/pass here so I figured it was possible to read from the db the same way and instead of writing to a file populate my variable
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 23591867
I think either ac_bl or ~/developers/bin/runparallel has the username and password
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
Suggested Courses

830 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