Quotes in quotes

Quotes in quotes

Post by Matthew Wis » Sat, 13 Jun 1998 04:00:00



The backup command bru takes an optional Label argument like this:

bru -L "Useful label information" -cvf /dev/nst0 /dir1

In a sh script I'm writing I want to take /dir from the command line
(easy) and put it into the label field with the time. Something along the
lines of:

#!/bin/sh
DIR=$1
LABEL="$(date) $DIR"
BRUCMD="bru -L $LABEL -cvf /dev/nst0 $DIR"

echo $BRUCMD

$BRUCMD

the echo gives back exactly what I want to see:

bru -L "Fri Jun 12 20:59:42 EDT 1998 /dir1" -cvf /dev/nst0 /dir1

but when the command executes the quote marks have disappeared and bru
chokes on everything after Fri.

I'm sure this is something quite simple I'm missing, but I've already
wasted too much time on it.

Any suggestions?

thanks in advance,
Matt

 
 
 

Quotes in quotes

Post by Roel Sleger » Sun, 14 Jun 1998 04:00:00


Try combining single and double quotes, for example like this:
    ...
    BRUCMD='bru -L "$LABEL" -cvf /dev/nst0 $DIR'
    ...

Hope this helps

 
 
 

Quotes in quotes

Post by Christian Bauernfei » Sun, 14 Jun 1998 04:00:00




Quote:> The backup command bru takes an optional Label argument like this:

> bru -L "Useful label information" -cvf /dev/nst0 /dir1

> In a sh script I'm writing I want to take /dir from the command line
> (easy) and put it into the label field with the time. Something along the
> lines of:

> #!/bin/sh
> DIR=$1
> LABEL="$(date) $DIR"
> BRUCMD="bru -L $LABEL -cvf /dev/nst0 $DIR"

> echo $BRUCMD

> $BRUCMD

> the echo gives back exactly what I want to see:

> bru -L "Fri Jun 12 20:59:42 EDT 1998 /dir1" -cvf /dev/nst0 /dir1

Not when I try it. And I wouldn't have expected it.
Both pairs of "" are stripped before the assignment is performed.

Two notes:

$() only works if you /bin/sh is in fact ksh. Might as well admit it
in the first line.

To insert double quotes into your command string, protect them with
backslashes:
BRUCMD="bru -L \"$LABEL\" -cvf /dev/nst0 $DIR"

Look up the way ksh parses its command line and the effect of quotes,
either in the man page ot a good book, like "Learning the Korn Shell"
from O'Reilly.

That was three notes. Sorry.

Btw, you don't need eval, as somebody else suggested, in this case.
But to judge that you have to know when the shell expands variables.

Christian
--
Christian Bauernfeind
Not speaking for Siemens
Not even working for IBM

 
 
 

Quotes in quotes

Post by Peter Samuels » Sun, 14 Jun 1998 04:00:00



Quote:> > LABEL="$(date) $DIR"
> > BRUCMD="bru -L $LABEL -cvf /dev/nst0 $DIR"


Quote:> $() only works if you /bin/sh is in fact ksh. Might as well admit it
> in the first line.

Yes ... though I'm guessing this is on Red Hat Linux, since
  - /dev/nstx is a Linuxism for SCSI tape with no rewind-on-close, and
  - Red Hat ships Bru standard.

So $() will work since /bin/sh in Linux is bash.  (I'm not even sure
a true Bourne shell is available in Linux, not counting ash from BSD.)
That doesn't mean you shouldn't start the script with #!/bin/bash,
however....

Quote:> Btw, you don't need eval, as somebody else suggested, in this case.
> But to judge that you have to know when the shell expands variables.

This is true ... but I generally use eval in those cases anyway, just
because it looks cleaner.

--
Peter Samuelson
<sampo.creighton.edu ! psamuels>

 
 
 

Quotes in quotes

Post by Christian Bauernfei » Tue, 16 Jun 1998 04:00:00






>>Two notes:

>>$() only works if you /bin/sh is in fact ksh. Might as well admit it
>>in the first line.

> $() is a POSIX feature derived from the Korn shell. I have seen it supported
> in modern versions of /bin/sh, such as the HP-UX /bin/posix/sh (which is
> /bin/sh in 10.x).

Still, backticks or explicitly calling /bin/ksh remain more protable.

Quote:

>>Btw, you don't need eval, as somebody else suggested, in this case.

> I suggested that, and I did mention that it wasn't needed in this
> case, but that it's more general.

No. Since it forces another round of substitutions, it will
strip the quotes. Like I said, you have to know what you are doing,
there is no foolproof method for this.

Christian

--
Christian Bauernfeind
Not speaking for Siemens
Not even working for IBM

 
 
 

Quotes in quotes

Post by Matthew Wis » Tue, 16 Jun 1998 04:00:00


Thanks everybody for your reponse & learned discussion.

I negelected to point out that the value of LABEL (see below) and the
refernce in BRUCMD (ibid.) had the quote escaped. I'd tried that along
with several other things before eval was suggested & wasn't sure if
escaping the quotes was part of the problem or part of the solution.

BTW: yes, this is bash under Redhat 5.

Here's where I ended up:

#!/bin/sh
DIR=$1
LABEL="$(date) $DIR"
BRUCMD="bru -L \"$LABEL\" -cvf /dev/nst0"

(eval $BRUCMD > $LOGFILE) 2>&1

...cut! print! on to the next mess ...

Thanks again,
Matt


> The backup command bru takes an optional Label argument like this:

> bru -L "Useful label information" -cvf /dev/nst0 /dir1

> In a sh script I'm writing I want to take /dir from the command line
> (easy) and put it into the label field with the time. Something along the
> lines of:

> #!/bin/sh
> DIR=$1
> LABEL="$(date) $DIR"
> BRUCMD="bru -L $LABEL -cvf /dev/nst0 $DIR"

> echo $BRUCMD

> $BRUCMD

> the echo gives back exactly what I want to see:

> bru -L "Fri Jun 12 20:59:42 EDT 1998 /dir1" -cvf /dev/nst0 /dir1

> but when the command executes the quote marks have disappeared and bru
> chokes on everything after Fri.

> I'm sure this is something quite simple I'm missing, but I've already
> wasted too much time on it.

> Any suggestions?

> thanks in advance,
> Matt

 
 
 

Quotes in quotes

Post by Christian Bauernfei » Tue, 16 Jun 1998 04:00:00




Quote:

> The eval _is_ needed because without it, the quotes will not be
> stripped away. The processing of quotes takes place BEFORE the
> expansion of shell variables. Thus if you write

>    $BRUCMD

> it will expand into a bunch of tokens that will only be analyzed
> for whitespace separation only! The quotes will not be processed.
> So the resulting command line will be:

>    {BRU}{-L}{"Mon}{Jul}{15}{10:26:01}{GMT-8:00}{1998"}{-cvf}{/dev/nst0}

Ok, I goofed. I'm eating that article now.

What I should've suggested is

LABEL="\"$(date) $DIR\""
BRUCMD="bru -L $LABEL -cvf /dev/nst0"
$BRUCMD

Christian
--
Christian Bauernfeind
Not speaking for Siemens
Not even working for IBM

 
 
 

1. Q: sh: Can I quote '"' in a double quoted string?

I'm trying to reconcile some Bourne shell documentation (Solaris) with
Bourne shell's behavior.

The manpage says, regarding quoting:

"Inside a pair of double quote makrs (""), [...] \ quotes the
characters \, `,  , and $.  The pair \newline is removed before
parameter and command substitution.  If a backslash precedes
characters other than \, `,  , $, and newline, then the backslash
itself is quoted by the shell."

Note particularly the last sentence.  This would suggest that:

$ echo "   \" HELP! \"   "

...would give you:

   \" HELP! \"

...as output, right?  This would be because a double quote mark is not
one of those characters which a backslash quotes, according to the
documentation.  This seems like a load of hooey to me, because
everyone knows that if you type the example above you really get:

   " HELP! "

...instead.

OR: is it possible that what's going on here is the following:

1. The string consisting of three spaces, '\"', one space, 'HELP!',
one space, '\"', three spaces is quoted by the shell.  Since there are
no backslashed \, `,  , or $ characters, the shell protects the
whitespace and passes the string as one argument to the echo command.
Thus, echo gets
<space><space><space>\"<space>HELP!<space>\"<space><space><space> as
its sole arguement.

2. The subshell that echo is running under does processing on the one
argument to echo, translating the '\"' sequences to '"'.

Unless I hear otherwise, I'm going to assume that you CAN backslash a
double quote mark inside a double quoted string in the Bourne shell,
and that the documentation is simply wrong not to mention this.

Cheers,
Laird

--
laird j. nelson                                           (617) 252-5147

nets, incorporated                                       25 first street
http://www.amherst.edu/~ljnelson                     cambridge, ma 02141

2. Moving sendmail off the host.

3. replace single quote to double quote with an example

4. KDE menus messed up.

5. quoted strings within quoted strings

6. XF86 Font Problem...

7. Quoting quotes in m4?

8. keyboard screwup on pismo running debian sid

9. Double quotes within double quotes (no apostrophes)

10. Problem in sed using double quotes and single quotes

11. to quote or not to quote?

12. quoting quotes withion a (bourne shell) script line

13. quoting quotes, problem