How can i use single quotes for SQL command in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can i use single quotes for SQL command in shell script
# 1  
Old 05-08-2009
MySQL How can i use single quotes for SQL command in shell script

Hi.

please help me to write the following query in a shell script.

the Query is :select no,salary from emp_info where name='$var_name'

the following is my code.

#! /bin/sh

var_name=$1

sqlplus -s user/pwd@DB << EOF

select no,salary from emp_info where name="'$var_name'";

exit

EOF

Note: I can use the code for the number comparision.
please help me to get the sigle quotes in that query..

Thankyou all for your valuable answers..
# 2  
Old 05-08-2009
Hi little_wonder,

Just remove the double quotes it will work fine in normal case ( where the names wont hav single quotes in it)

Code:
 
select no,salary from emp_info where name='$var_name';


Last edited by panyam; 05-08-2009 at 04:47 AM..
# 3  
Old 05-08-2009
Some Problem with the suggestion.

Hi Panyam,

Thankyou so much for the given solution.

But this i have tried already. but with this i feel a issue that the filed reterived from that query is with spaces and new linr char..

For example when you try to display the result.

#! /bin/sh

var_name=$1

no=`

sqlplus -s user/pwd@DB << EOF

select no,salary from emp_info where name="'$var_name'";

exit

EOF`

echo "No is $no"



The result is like,

no is
123



please give me a solution.
# 4  
Old 05-08-2009
Your query is returning name as well as number , but your output shows only number value.

how ever try the following , which is working fine for me.


Code:
#! /bin/sh

var_name=$1

op=`sqlplus -s user/pwd<< EOF

set head off
set feed off
set echo off
set line 999


 
 select no,salary from emp_info where name='$var_name';

exit

EOF`

no=`echo $op |awk '{ print $1}'`
salary=`echo $op |awk '{ print $2}'`

# 5  
Old 05-10-2009
MySQL yah!! its working 5n for me..

Hi Panyam,
Thankyou so much..
Its working fine for me..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add line breaks to perl command with large text in single quotes?

Below code extracts multiple field values from XML into array and prints all in one line. perl -nle '@r=/(?: jndiName| authDataAlias| value| minConnections| maxConnections| connectionTimeout| name)="(+)/g and print join ",",$ENV{tIPnSCOPE},$ENV{pr ovider},$ENV{impClassName},@r' server.xml ... (4 Replies)
Discussion started by: kchinnam
4 Replies

2. Shell Programming and Scripting

Issue with quotes when running SQL command from within su -c

RHEL 6.2/Bash shell root user will be executing the below script. It switches to oracle user logs in using sqlplus and tries to run the below UPDATE statement. All the commands after su -c are enclosed in a single quote delimited by semicolon. The execution has failed because the quotes... (3 Replies)
Discussion started by: omega3
3 Replies

3. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

4. Shell Programming and Scripting

Bash shell adding extra single quotes

AIX 6.1 bash shell #!/bin/bash -x STATEMENT="cvs commit -m \"This is\" ../PBP/EIR.ENTRY" echo $STATEMENT exit 0 This is the output + STATEMENT='cvs commit -m "This is" ../PBP/EIR.ENTRY' + echo cvs commit -m '"This' 'is"' ../PBP/EIR.ENTRY cvs commit -m "This is" ../PBP/EIR.ENTRY + exit... (26 Replies)
Discussion started by: hpodhrad
26 Replies

5. Shell Programming and Scripting

shell script - to append single quotes and comma

file1 ---- 34556745 32678343 31576776 31455566 21356666 I want to assign the record values to a variable in the below format, so that I can use output in .sql file for querying in database. ('34556745', '32678343', '31576776', '31455566', '21356666') ----------- below is the... (11 Replies)
Discussion started by: rajivrsk
11 Replies

6. UNIX for Dummies Questions & Answers

Unable to send single and double quotes to command

Hi Unix experts, Believe me, this forum has been really great help and I searched for many things that were already answered before open new post that were just new versions of old one, but with this one, I just can't simply move any forward. This must be quite easy, but I cant find where I... (1 Reply)
Discussion started by: manolain
1 Replies

7. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

8. Shell Programming and Scripting

How to alias an awk command with single and double quotes?

Hi, I am trying to write the following command as an alias in my .bashrc file. bjobs -u all | awk '{if (NR > 1) {username++;}}END{{print"\nJOBS BY USER:\n"} for (i in username) {print username,i;}{print"\n Total Jobs=",NR-1,"\n" }}' The command simply puts how many jobs each user is... (2 Replies)
Discussion started by: jacekmaciek
2 Replies

9. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies

10. Shell Programming and Scripting

Executing Multiple .SQL Files from Single Shell Script file

Hi, Please help me out. I have around 700 sql files to execute in a defined order, how can i do it from shell script (3 Replies)
Discussion started by: anushilrai
3 Replies
Login or Register to Ask a Question