Quoting issue: Trouble with bash strings in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quoting issue: Trouble with bash strings in script
# 1  
Old 02-12-2010
Quoting issue: Trouble with bash strings in script

I can do this on the command line:

Code:
sqsh -S 192.168.x.x -o tmp -U user -P fakepass -D horizon -C "\
select second_id
from borrower
where btype like '%wsd%'
"

I can also just leave the SQL at the end intact on one line ....
... However, when I throw this in a script like:
Code:
$SQSH -o SQSHout -S $SERVER -U $USER -P $PASS -D $DB -C '"SELECT second_id FROM borrower WHERE btype like "%wsd%""'

When I run the script I get the SQSH help page. So my question is whether
this is an oddity of bash quoting or a weird sqsh thing.

You can do stuff like this with isql by piping the string or using here
docs. sqsh uses the -C option to allow the SQL at the end. sqsh is much better for the most part, but I can't get the string part to work. I've tried numerous quoting combos. I've already looked at the sqsh man page more
than I care to mention. Is this my bash ignorance or a limitation of sqsh?

I gotta go do something else before I throw myself under a train.

Thanks for reading ~

Bub
# 2  
Old 02-12-2010
With
Code:
$SQSH -o SQSHout -S $SERVER -U $USER -P $PASS -D $DB -C '"SELECT second_id FROM borrower WHERE btype like "%wsd%""'

you passed the following query to sqsh:
Code:
"SELECT second_id FROM borrower WHERE btype like "%wsd%""

# 3  
Old 02-12-2010
**** UPDATE ~ binlib points out an easier ( more correct ) way to quote the expression in the post following this one. ******

How do I hide quotes from sqsh and pass the SQL?

---------- Post updated at 06:58 PM ---------- Previous update was at 06:02 PM ----------

Solution ....finally.

Problem:
Protect quoted expression "%wsd%" from bashes prying eyes. sqsh must see it in quotes.

Answer:
Code:
SQL="SELECT second_id FROM borrower WHERE btype like '"'%wsd%'"'"

Victory is sweet. Now if I could just get out of this damn valentines banquet I get
conned into every year .....[SIGH]

Thanks ~

Bub

PS: sqsh rocks if you need to deal with Sybase or MS SQL server.

---------- Post updated at 07:14 PM ---------- Previous update was at 06:58 PM ----------

I've seen other SQSH users in this mess while Googling, so here's a tip for
passing queries to SQSH without resorting to external SQL scripts.

Define your query:
Code:
SQL="SELECT second_id FROM borrower WHERE btype like '"'%wsd%'"'"

Note carefully the quoting in the expression %wsd%. The expression is surrounded by single quoted double quotes: eg., '"'
while the whole query is surrounded by single quotes ...making
for a quote mess on the end ...eg., '"'" ( thats: single - double - single - double ).

***** EDIT ***** Please see the following two posts ...esp. from Binlib. You can quote the last expression with a single set of single quotes.
Thanks binlib! Tested and true.

Next, run that filthy query like so:
Code:
$SQSH="/usr/bin/sqsh"
$SERVER="server.whatever.org"
$USER="username"
$PASS="secrets"
$DB="nameOfDatabase"

$SQSH -S "$SERVER" -U "$USER" -P "$PASS" -D "$DB" -C "$SQL" > $OUT

You don't necessarily need all the variables, but it's a bit easier on
the eyes of those stuck reading it later. I usually define the sqsh
binary for cron, otherwise you'd likely have to define it for the
cron environment. Could be mistaken ...I always try to cron-proof
all but the most common commands. Easier than fixing every cron
environment path variables.

Later ...

Bub

***** UPDATED **** Please see following two posts for simpler quoting option.

Last edited by Bubnoff; 02-15-2010 at 02:43 AM.. Reason: Red herring: single quotes work. It was an issue in the way I declared sqsh. Damn I hate that!
# 4  
Old 02-13-2010
Code:
"SELECT second_id FROM borrower WHERE btype like '"'%wsd%'"'"

is one argument made of three parts:
Code:
SELECT second_id FROM borrower WHERE btype like '

quoted by double quotes, followed by
Code:
%wsd%

quoted by single quote, followed by
Code:
'

quoted by double quotes.
Since the %wsd% part doesn't need to be single quoted, it can be simply embedded with the other parts. As in
Code:
"SELECT second_id FROM borrower WHERE btype like '%wsd%'"

which is simpler. In fact, it even works if you want the wsd part to be a variable. On the other hand, if the wsd part has dollar signs, then your method of single quoting it is better.
# 5  
Old 02-15-2010
You're right!

I had declared sqsh like this at the top:

Code:
declare -rx SQSH=/usr/bin/sqsh

When I removed the 'declare' part and just said:

Code:
SQSH="/usr/bin/sqsh"

...it worked. This coincided with the last quoting concoction
I had tried ...thus I mistakenly concluded that it was the
quoting that fixed it.

This issue was strange because sqsh was running but complaining about
syntax errors in the options. So basically I was chasing the proverbial
'red herring'.

I thought that the 'declare -rx' syntax was perfectly legal. I have used this
form before with no issues. I sync with rsync this way. I wonder what the deal is with sqsh?

Thanks for pointing out my mistake!!

Bub
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Bash quoting

I am trying to write a BASH script that will prompt a user to enter a number of days, then calculate the date. My problem is the date command uses single or double quotes. For Example.. date -d "7 days" Here is an example of some same code I am trying to work through. echo "when do you... (4 Replies)
Discussion started by: javajockey
4 Replies

2. Homework & Coursework Questions

Linux/UNIX Bash Shell Script trouble help needed!!

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 2. Shell Bash Script 3. !/bin/bash if echo no directory then mkdir -p /home/AC_Drywall elif ; then echo "$dir already exist" fi (4 Replies)
Discussion started by: TomFord1
4 Replies

3. Shell Programming and Scripting

Tricky BASH quoting question

I have some data files that I can identify by a certain pattern in the names using find. Every one of those data files has an XML file associated with it (can be multiple data files per XML file). The XML file is always up one directory from the data file(s) in a folder calledRun##### -... (12 Replies)
Discussion started by: Michael Stora
12 Replies

4. Shell Programming and Scripting

Having trouble with My Bash Script, need Help debugging

Hello Friends I am having trouble with my script below. I will describe the problems below the code box. I am hoping that some of the experts here can help me. #!/bin/bash #========================================================================================================= # Rsync File... (8 Replies)
Discussion started by: jdavis_33
8 Replies

5. Shell Programming and Scripting

Osascript quoting issue

I am at a loss on this one. I am trying to run this command on a bunch of (OS 10.7.4) macs:osascript -e "tell application \"System Events\" to return name of every process whose frontmost is true" On some, it works fine. On others, I get this error: I have also tried (note the single quotes):... (3 Replies)
Discussion started by: nextyoyoma
3 Replies

6. Shell Programming and Scripting

Find associated strings in a bash shell script

Hi together, unfortunately I am not a shell script guru - the following might touch the depths of awk, substr, split, regexps, where I am still fighting with - but as always the boss needs a fast solution :-( So: I have the following USER/PASSWORD-installation-config-file, from where I want to... (10 Replies)
Discussion started by: Sofie
10 Replies

7. Shell Programming and Scripting

bash: correct quoting with find and exiv2?

Hi, I need to embed a metatag to image files which contain qrcodes, i usually do this with exiv -M "set Exif.Image.DocumentName `zbarimg -q -Sdisable -Sqrcode.enable --raw image.tif`" image.tif which works fine. However I need to do this recursivly for whole directory and subdiretory... (4 Replies)
Discussion started by: mcframe
4 Replies

8. Shell Programming and Scripting

Bash Script: Trouble unable to run

I am trying to create a menu, and the script fails on ln 38 (Files in pwd). Any idea on where the problem is?? Thanks for the help Rob #!/bin/bash # Cool Script for Weekly Assignment 2 (#3) that creates a menu to act as a ui # and run some popular commands. clear while : do ... (9 Replies)
Discussion started by: rchirico
9 Replies

9. Shell Programming and Scripting

BASH quoting behavior

The block below isn't a surprise:$ ls file1 file2 file3 $ x=* $ echo $x file1 file2 file3 $ echo '$x' $x $ echo "$x" * $But I found this block a bit bewildering:$ echo $x' >' * $I'm wondering why substitution wasn't performed on the $x, since it was unquoted (as far as I can tell).... (5 Replies)
Discussion started by: na5m
5 Replies

10. Shell Programming and Scripting

Trouble grepping for multiple strings

I am having a heck of a time trying to write a script that will grep for multiple strings in a single file. I am really at my wits end here and I am hoping to get some feedback here. Basic information: OS: Solaris 9 Shell: KSH Oracle Database server I was trying to grep through a file... (5 Replies)
Discussion started by: thecoffeeguy
5 Replies
Login or Register to Ask a Question