shellscrip -how to connect db using shellscript


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shellscrip -how to connect db using shellscript
# 1  
Old 09-12-2011
shellscrip -how to connect db using shellscript

Hi All,

Using shell script i want to run simple query (Eg: select count (*) from ord where ordtimestamp>='2011-09-09'; and need to catch it into variable . how is it possible? NOTE- i am using Mysql query browser

I have tried below code

==================================================
Code:
#! /bin/bash
# initialization of dates
 
mysql -u root -D rddatabase
 
$a= 'select count(*) from orders where OrderTimestamp > 20110819';
 
echo $a

===================================================

above code is not working properly please help me to do so.


Thanks

Last edited by pludi; 09-12-2011 at 08:12 AM..
# 2  
Old 09-12-2011
Code:
user="root"
db="rddatabase"
result=$(mysql -u $user $db -sN -e "'select count(*) from orders where OrderTimestamp > 20110819'")

# 3  
Old 09-12-2011
thanks itkamraj

=====================================
Code:
user="root"
db="redeyedatabase"
result=$(mysql -u $user $db  -e "select count(*) from orders where OrderTimestamp > 20110819")

=====================================
tried above code but getting an output

Code:
count(*) 3862

I want only 3862

???

Last edited by pludi; 09-12-2011 at 08:58 AM..
# 4  
Old 09-12-2011
can you just try this,
before the query can you just include this line,
"set heading off"
This User Gave Thanks to latika For This Post:
# 5  
Old 09-12-2011
or alternatively you can
Code:
echo $result | awk '{print $2}'

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 6  
Old 09-12-2011
Try this...
Code:
result=$(mysql -N -r -B -u $user $db  -e "select count(*) from orders where OrderTimestamp > 20110819")

This User Gave Thanks to itkamaraj For This Post:
# 7  
Old 09-12-2011
Here's an example that works using SQL/Plus against an Oracle database. The same principle could apply here. Namely construct the query so the output looks like "variable=<selected_value>" then eval it to create a shell variable.

Code:
#!/bin/ksh
##  The command in $() returns COUNT=<nbr> which is then eval'd to create
##  and set a shell variable called COUNT.
eval $(print "select 'COUNT='||count(*) as count from schema.table_name;"|sqlplus -s / |grep COUNT=)

print "The count is $COUNT"


Last edited by gary_w; 09-12-2011 at 11:17 AM..
This User Gave Thanks to gary_w For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Connect direct - SFTP - List of servers that I can connect

Greetings Experts, I am working for a bank client and have a question on connect-direct and SFTP. We are using Linux RedHat servers. We use connect-direct to transfer (NDM) files from one server to another server. At times, we manually transfer the files using SFTP from one server to another... (2 Replies)
Discussion started by: chill3chee
2 Replies

2. Cybersecurity

When i start CSF i cant connect VPS or download any data into it It appears i cant connect Linux VP?

It appears i cant connect linux VPS server via SSH or i cant SCP any file to it and i cant wget any file TO it (from inside it) while CSF (Config Server Firewall, LFD is running. Just after isntall in default configuration and after changing TESTING mode to LIVE mode. Trying to wget & install... (1 Reply)
Discussion started by: postcd
1 Replies

3. Shell Programming and Scripting

Help with shellscript

I am new in shell script i want to convert .txt file in the format axsjdijdjjdk to a x s j d i j d j j d k (5 Replies)
Discussion started by: sreejithalokkan
5 Replies

4. AIX

AIX Remote Connect Fail With “No more multiple IP addresses to connect” Error

We have a production server at a client site running AIX. And recently when users are trying to connect to it via telnet, it prompts "No more multiple IP addresses to connect". Can I know what does this error mean? and how to rectify this? Thanks. (2 Replies)
Discussion started by: a_sim
2 Replies

5. UNIX for Dummies Questions & Answers

How can I do aliasing in shellscript?

#Example.sh alias rmv 'sh Example2.sh' when i execute exapme.sh alias name not working. how i solve this problem?? (9 Replies)
Discussion started by: arun508.gatike
9 Replies

6. Shell Programming and Scripting

Automation of UI using shellscript

Hi, I want to do automation on UI using shellscript. eg: 1) Drop down menu contains assign , investigate, closed. now there is one id want assign it using assign tab then need to investigate it and lastly close. Sometimes the id can't assign to perticular user. there are so many... (11 Replies)
Discussion started by: aish11
11 Replies

7. Shell Programming and Scripting

Need help with shellscript

Hello. I am a novince at writing shell scripts but here is the question. I have to write a shell script that does the following: Once executed via crontab, the script should do the following: a. get date/time stamp in for format 10-MAR-05 and b. execute shell script my_script.sh (which... (2 Replies)
Discussion started by: jigarlakhani
2 Replies

8. Programming

Shellscript for MQSeries

Iam new to shellscript. 1)How to strart QUERYMANAGER using shellscript. 2)How to put and get messages in MQSeries using shellscripts. 3)iam using local queues . Thanks lot. (0 Replies)
Discussion started by: ram2s2001
0 Replies

9. Shell Programming and Scripting

Another shellscript question

Folks; on a unix server I have a mapping file which holds a list mountpoints of all databases and their mountpoints. tab delimited or colon deliminted..I needed to copy the datafiles from the pristine mountpoints to test's mountpoints in this case. I needed to do this by passing sid name using... (18 Replies)
Discussion started by: jigarlakhani
18 Replies
Login or Register to Ask a Question