[SGVLUG] Thanks for bash -c puzzle

Matt Campbell dvdmatt at gmail.com
Mon Apr 19 16:44:53 PDT 2010


Thanks Don,

 

I came to the same realization earlier today.  Rae actually came up with a
single-level solution:

 

echo  'command' | bash

 

instead of

 

bash -c 'command'

 

but echo runs into the same quoting quotes problem.

 

The full solution ended up being:

 

echo -e "echo 'ALTER TABLE \\\0140Components\\\0140 ORDER BY
\\\0140type\\\0140,\\\0140Home\\\0140,\\\0140Class\\\0140,\\\0140Make\\\0140
, \\\0140Model\\\0140,\\\0140Name\\\0140;ALTER TABLE \\\0140Documents\\\0140
ORDER BY
\\\0140type\\\0140,\\\0140Year\\\0140,\\\0140Make\\\0140,\\\0140Version\\\01
40,\\\0140Line\\\0140;ALTER TABLE \\\0140Layouts\\\0140 ORDER BY
\\\0140Date\\\0140;' | mysql -u 'matts' 'mattsInfo' -p'password' -h 'dbhost'
" | /bin/bash 2>&1

 

-e allows bash-echo to interpret backslashed octal characters which lets you
embed quotes in your strings.

 

It is being generated programmatically so the command is hidden from the
casual user.

 

Ok it's ugly, at least it works.. ;)

 

Matt

 

From: Gibbs, Donald E (316B) [mailto:donald.e.gibbs at jpl.nasa.gov] 
Sent: Monday, April 19, 2010 3:12 PM
To: Matt Campbell
Subject: Re: Thanks for bash -c puzzle

 

Hi, Matt

You know, I followed Rae's comment into the man pages for bash, down in the
"QUOTING" section, and it seems pretty clear that you cannot put single
quotes within other single quotes.  Period.  Backslashes won't help.  Looks
like the shell simply does not support what would otherwise seem like a
reasonable construction.

You can put double quotes within other double quotes using backslashes.  But
not so with single quotes.

 I'll nose around some more, but it looks pretty cut and dried to me.

  --Don


On 4/19/10 10:44 AM, "Matt Campbell" <dvdmatt at gmail.com> wrote:

Thanks Don, let me know if you come up with anything.
 
The base problem is 'How do you quote a quote in shell'?
 
The smallest test case is:
 
echo '\'hi\' '
 
I think it should produce: 'hi'
 
It does produce:  Unmatched '.
 
They sure look matched to me.  This fails the same way in tcsh and bash on
my Linux box.
 
Matt
 

From: Gibbs, Donald E (316B) [mailto:donald.e.gibbs at jpl.nasa.gov] 
Sent: Monday, April 19, 2010 10:37 AM
To: Matt Campbell
Subject: Re: Thanks for bash -c puzzle

Rae Yip's quote from the bash man page did not sound promising.  I can't
spend too much time on this, I've got other pressing deadlines looming, but
I'll poke around at it.

  --Don

On 4/19/10 10:29 AM, "Matt Campbell" <dvdmatt at gmail.com> wrote:
The problem can be simplified down to:
 
This works:
ssh -lmatt scar '/bin/bash -c "echo works"  '
 
No variation of this appears to:
ssh -lmatt scar '/bin/bash -c \'echo fails\'  '
 
The only difference is mixed quotes vs. quoted quotes.  Do you see a
solution?
 
I want to force bash on the far end so that shell redirection can be used to
capture error output back to the local host.
 
All the rest of the (much more complex) command works.
 
Matt
 

From: Gibbs, Donald E (316B) [mailto:donald.e.gibbs at jpl.nasa.gov] 
Sent: Monday, April 19, 2010 10:15 AM
To: Matt Campbell
Subject: Thanks for bash -c puzzle

Hi, Matt

I used to be more active in SGVLUG years ago but have become a mere lurker
lately.

Thanks for asking about that bash question.  I just started re-strengthening
my shell scripting skills and your problem was timely.

Not that I could answer your question, but it made some of the bash man page
become more 3D, if you know what I mean.

  --Don 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.sgvlug.net/pipermail/sgvlug/attachments/20100419/31fbfe28/attachment.html 


More information about the SGVLUG mailing list