quoting problem with Bourne shell

quoting problem with Bourne shell

Post by Griff Miller I » Sat, 07 Sep 2002 05:40:02



This is on Solaris 2.6 .

Given this program (which is not the actual program, but serves to
illustrate the problem I am having):

#!/bin/sh

MKISOFS='mkisofs -r -l -f -v -graft-points "patient_1318/=/data/study/patient_1318"'
$MKISOFS >/dev/null

...I get this when I run it:

./runprog
mkisofs 1.15a09 (sparc-sun-solaris2.6)
mkisofs: No such file or directory. Invalid node - /data/study/patient_1318"

Somehow mkisofs is getting the 2nd '"' into its argv[6]. Why? Obviously,
the first '"' is getting removed by the shell before passing the args to
mkisofs - why doesn't the 2nd?

This question is more academic than anything - I can work around it. In fact,
I'll have to! I just want to understand what is happening.

--
Griff Miller II                   |                                           |
Manager of Information Technology | "Do Lipton employees take coffee breaks?" |
Positron Corporation              |                                           |

 
 
 

quoting problem with Bourne shell

Post by David Thompso » Sat, 07 Sep 2002 06:36:58



Quote:> This is on Solaris 2.6 .

> Given this program (which is not the actual program, but serves to
> illustrate the problem I am having):

> #!/bin/sh

> MKISOFS='mkisofs -r -l -f -v -graft-points

"patient_1318/=/data/study/patient_1318"'
Quote:> $MKISOFS >/dev/null

> ...I get this when I run it:

> ./runprog
> mkisofs 1.15a09 (sparc-sun-solaris2.6)
> mkisofs: No such file or directory. Invalid node -

/data/study/patient_1318"

Quote:

> Somehow mkisofs is getting the 2nd '"' into its argv[6]. Why? Obviously,
> the first '"' is getting removed by the shell before passing the args to
> mkisofs - why doesn't the 2nd?

> This question is more academic than anything - I can work around it. In
fact,
> I'll have to! I just want to understand what is happening.

The single quotes prevent all interpolation.  So, your interior double
quoted
substring is not evaluated, so the double quotes are (naturally) never
removed.

The quickest solution is simple,

  eval $MKISOFS >/dev/null

which will cause the interior double-quoted substring to be interpolated,
thus removing the double quotes.

But, do you really need double quotes there?

You knowledge might benefit from some tests,

  X='""'
  echo $X

should print "", but using eval,

  eval echo $X

prints a blank line.  Furthermore,

  X=""""
  echo $X

prints a blank line.  Why?  (HINT: interpolation, interpolation,
interpolation)

Btw, since echo simply prints its argv[] array to stdout, it is the
perfect tool for you to experiment/solve/prove your solution.

--
David Thompson


 
 
 

quoting problem with Bourne shell

Post by Griff Miller I » Sat, 07 Sep 2002 23:08:31



> The single quotes prevent all interpolation.  So, your interior double quoted
> substring is not evaluated, so the double quotes are (naturally) never removed.

I'm afraid I must not have made it clear. The single quotes are there
simply to get the double quotes stored into the string, for the
purposes of the demonstration.

This does the same thing:

#!/bin/sh

MKISOFS=mkisofs\ -r\ -l\ -f\ -v\ -graft-points\ \"patient_1318/=/data/study/patient_1318\"
$MKISOFS >/dev/null

...as does this:

#!/bin/sh

MKISOFS="mkisofs -r -l -f -v -graft-points \"patient_1318/=/data/study/patient_1318\""
$MKISOFS >/dev/null

In all three cases, the contents of the MKISOFS variable is the same.
What I don't understand is how the trailing double quote is getting into
mkisofs's argv[6] :

mkisofs 1.15a09 (sparc-sun-solaris2.6)
mkisofs: No such file or directory. Invalid node - /data/study/patient_1318"

OTOH, if I just run this from the command line:

mkisofs -r -l -f -v -graft-points "patient_1318/=/data/study/patient_1318" >/dev/null

...it works fine.

Quote:> The quickest solution is simple,

>   eval $MKISOFS >/dev/null

> which will cause the interior double-quoted substring to be interpolated,
> thus removing the double quotes.

But I don't want the double quotes removed. I wanted them there to
allow for the possibility that there might be spaces in the paths
that I pass to mkisofs .

--
Griff Miller II                   |                                           |
Manager of Information Technology | If you're too open-minded,                |
Positron Corporation              | your brains might fall out.               |

 
 
 

quoting problem with Bourne shell

Post by Griff Miller I » Sat, 07 Sep 2002 23:25:29



> mkisofs 1.15a09 (sparc-sun-solaris2.6)
> mkisofs: No such file or directory. Invalid node - /data/study/patient_1318"

Ah-hah! I've figured it out. When I invoke mkisofs that way, *neither*
double quote is removed by the shell, so both of them get passed into
mkisofs's argv[6]. This is proved by this C program (printargs.c):

#include <stdio.h>

int i;

int main(argc,argv)

         int argc;
         char *argv[];

{
         for (i=0; i<argc; i++ ) {
              fprintf(stderr,"%s\n",argv[i]);
         }

         exit(0);

Quote:}

When I now run this script:

#!/bin/sh

MKISOFS="printargs -r -l -f -v -graft-points \"patient_1318/=/data/study/patient_1318\""
$MKISOFS >/dev/null

...I get:

/path/to/printargs
-r
-l
-f
-v
-graft-points
"patient_1318/=/data/study/patient_1318"

So it's mkisofs that's stripping off the leading " while missing the
trailing " and not the shell. I didn't imagine that mkisofs would do that,
and so incorrectly assumed that the shell was messing me up.

--
Griff Miller II                   |                                           |
Manager of Information Technology | "This space has been blankly              |
Positron Corporation              |  left intentional."                       |

 
 
 

quoting problem with Bourne shell

Post by those who know me have no need of my nam » Thu, 12 Sep 2002 19:09:33


in comp.unix.shell i read:

Quote:>Ah-hah! I've figured it out. When I invoke mkisofs that way, *neither*
>double quote is removed by the shell, so both of them get passed into
>mkisofs's argv[6].

that's what david said.

Quote:>So it's mkisofs that's stripping off the leading " while missing the
>trailing "

nope.  mkisofs is prepared to graft the file/directory:

     /data/study/patient_1318"

onto the iso image at:

     "patient_1318/

but since the former doesn't exist it dies before anything else can happen.
drop just the trailing quote and you'd see the silly directory in your
image.

--
bringing you boring signatures for 17 years

 
 
 

quoting problem with Bourne shell

Post by Griff Mill » Sun, 15 Sep 2002 04:25:21



Quote:> in comp.unix.shell i read:

> >Ah-hah! I've figured it out. When I invoke mkisofs that way, *neither*
> >double quote is removed by the shell, so both of them get passed into
> >mkisofs's argv[6].

> that's what david said.

Yes, indirectly. Nothing about his explanation was incorrect; it just didn't
manage to line up with what turned out to be the actual misunderstanding on my
part, which is manifested in this sentence:

: Obviously, the first '"' is getting removed by the shell before passing the
: args to mkisofs - why doesn't the 2nd?

In fact, (as you know) the first '"' wasn't being removed by the shell. David
didn't directly challenge this (incorrect) assumption of mine, though he did
provide examples that exhibited that my "Obviously, ..." statement was wrong.
I'm sorry, I just didn't get it through my thick skull.  :^}  But I am
grateful for the attempt!

Quote:> >So it's mkisofs that's stripping off the leading " while missing the
> >trailing "

> nope.  mkisofs is prepared to graft the file/directory:

>      /data/study/patient_1318"

> onto the iso image at:

>      "patient_1318/

> but since the former doesn't exist it dies before anything else can happen.
> drop just the trailing quote and you'd see the silly directory in your
> image.

Smack! (sound of hand striking forehead) You're absolutely right. I see it all
now. Thanks, *that* made it all clear to me.

Griff

 
 
 

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

Hopefully an easy one but I'm tearing my hair out.

Keeping a long story short, I have a NetAPP filer with (lets say) root
access of

username : root
password : nobby'   (the end ' is supposed to be there).

Our backup software using NDMP has thism information in its
configuration.

Meanwhile I need to move data from the filer above to anothjer, for
which I would enter - on a command line from the second filer -

ndmpcopy -sa root:"nobby'" filer1:/vol/vol0/data/fred
/vol/vol0/data/fred

where the double quotes indicate that the strting within is the
password.

I can only run one such line at a time from the actual command line
and the filer permits only one login at a time.

However I can rsh commands to the filer - but my trouble here is
simply how to encompass that   root:"nobby'"   bit in a (bourne) shell
script such that the command issues the password as it should be ie
nobby'  but the shell script doesn't get confused over either the
double quotes or the single quote.

I can't change the password to something simopler #cos that will break
the backups!!

How can I acheive what I need in the script?

ie how can I script

rsh ndmpcopy -sa root:"nobby'" filer1:/vol/vol0/data/fred
/vol/vol0/data/fred

so that it runs like it would on the second filer and doesn't get
confused over the various quotes

Ian

2. who to 'loop' with multiple arguments in a string....

3. Light Speed Bourne Shell! (was: Bourne shell tricks)

4. Solaris 8 malloc problem?

5. Bourne shell dosen't seem to rescan for quotes in variables.

6. Linux print -> HP network-based printer(TCP/IP) ?

7. bourne shell quote

8. FAQ: comp.lang.awk: pointer

9. bourne shell quoting, solaris, and ufsrestore

10. Function arguments and quoting in Bourne-ish shells

11. sh & awk: Quoting Awk Syntax in Bourne shell?

12. Bourne Shell compatible shells (was: Request: which shells)

13. Problem in sed using double quotes and single quotes