public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* RE: dumb escaping question when using Cygwin +  NT commands
@ 2002-09-19  9:00 Scott Prive
  2002-09-19  9:08 ` Igor Pechtchanski
  0 siblings, 1 reply; 6+ messages in thread
From: Scott Prive @ 2002-09-19  9:00 UTC (permalink / raw)
  To: Randall R Schulz, cygwin



> -----Original Message-----
> From: Randall R Schulz [mailto:rrschulz@cris.com]
> Sent: Wednesday, September 18, 2002 6:30 PM
> To: cygwin@cygwin.com
> Subject: Re: dumb escaping question when using Cygwin + NT commands
> 
> 
> Scott,
> 
> At 15:15 2002-09-18, Scott Prive wrote:
> 
> >Hello,
> >
> >I get this odd problem when calling NT commands from Cygwin. I am 
> >single-quoting the data, but the way I'm doing things 
> (probably wrong...) 
> >does not like passing $1 function arguments to NT commands. 
> If I hardcode 
> >the arguments internally, everything works.
> >
> >The two example functions below are intended to behave identical.
> >
> >#!/bin sh
> >
> >mount_drive () {
> >    # Syntax: net 'use' '*' '\\redhat\foo' 'foo' '/user:foo'
> >    net 'use' 'F:' '\\redhat\foo' 'foo' '/user:foo'
> >
> >    echo "The command returned $?"
> >    return $?;
> >}
> 
> Note that the status ($?) you're returning from the 
> "mount_drive" shell 
> procedure is that of the "echo" command, not that printed 
> _by_ the echo 
> command.
> 
> The only arguments in this example for which quoting changes the net 
> argument passed to the underlying command is the one that 
> includes "redhat" 
> and the asterisk. The others contain no special characters requiring 
> quoting or escaping to inhibit special interpretation.
> 
> 
> >mount_drive2 () {
> >    net '$1' '$2' '$3' '$4' '$5'
> >    echo "we saw in mount_drive2: '$1' '$2' '$3' '$4' '$5' "
> >
> >    echo "The command returned $?"
> >    return $?;
> >}
> 
> The same "$?" issue exists here, of course.
> 
> You need to be aware of the difference between 'single 
> quotes' and "double 
> quotes." Variable expansion is inhibited in single-quoted 
> arguments, but 
> not in double-quoted ones. Furthermore, double quoted 
> arguments protect 
> single quotes, making the non-special. So you've probably 
> confused yourself 
> into thinking that in this example the "net" command saw the 
> arguments you 
> passed to the "mount_drive2" procedure. It did not. It saw 
> arguments each 
> consisting of a dollar sign followed by a digit. Then you 
> echoed a single 
> argument composed of some fixed text, some single quote marks 
> and some 
> expanded positional parameters.

Doh!

Thanks. A good nights sleep and coffee got me thinking about this on the way to work, and then I read your post. 

I misled myself because the ECHO command "worked". A debugging habit from Perl is I would print out my variables. Since the echo worked, I never questioned what I was doing with quotes.

I assumed quotes controlled how data gets sent to commands, but apparently that's an oversimplification: quotes protect data being sent to a NEW PROCESS.. and builtins like "echo" are NOT a new process (`type echo). This explains why the echo command understood what the heck was inside '$2', but the echo command did not. 

Of course you know this; I'm just filling in the blanks for the benefit of mailing list and Google searches. For all of last night, I actually believed the problem was due to mixing NT commands and Cygwin.

Thanks again.

> 
> 
> >#
> >mount_drive
> >mount_drive2 'use' 'G:' '\\redhat\foo' 'foo' '/user:foo'
> >############# END SCRIPT
> >
> >
> >the output I get from mount_drive2 is standard "usage info", 
> indicating I 
> >passed arguments incorrectly. However the debug echo *looks* correct.
> >
> >Someone please point out my mistake, else I'm doomed to some 
> ugly hackish 
> >workarounds ;-)
> >
> >Thanks,
> >
> >Scott
> 
> 
> Randall Schulz
> Mountain View, CA USA
> 
> 
> --
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> Bug reporting:         http://cygwin.com/bugs.html
> Documentation:         http://cygwin.com/docs.html
> FAQ:                   http://cygwin.com/faq/
> 
> 

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 6+ messages in thread
* RE: dumb escaping question when using Cygwin +  NT commands
@ 2002-09-19  9:28 Scott Prive
  0 siblings, 0 replies; 6+ messages in thread
From: Scott Prive @ 2002-09-19  9:28 UTC (permalink / raw)
  To: cygwin

Yes that helped; thanks.

> -----Original Message-----
> From: Igor Pechtchanski [mailto:pechtcha@cs.nyu.edu]
> Sent: Thursday, September 19, 2002 12:00 PM
> To: Scott Prive
> Cc: cygwin@cygwin.com
> Subject: RE: dumb escaping question when using Cygwin + NT commands
> 
> 
> On Thu, 19 Sep 2002, Scott Prive wrote:
> 
> > I assumed quotes controlled how data gets sent to commands, but
> > apparently that's an oversimplification: quotes protect 
> data being sent
> > to a NEW PROCESS.. and builtins like "echo" are NOT a new 
> process (`type
> > echo). This explains why the echo command understood what 
> the heck was
> > inside '$2', but the echo command did not.
> 
> Not quite.  In most shells, the builtin commands are 
> subjected to the same
> quoting rules as the external commands (there are some 
> exceptions, but I
> believe echo isn't one of them).  What was happening in your 
> case was that
> on the echo command line, the shell was getting single quotes inside
> double quotes, and thus treated them as regular characters 
> (expanding the
> vars within the DOUBLE quotes).  When you called your program, on the
> other hand, the shell was getting the variables in SINGLE quotes only,
> thus avoiding the expansion.
> 
> You should have called both echo and net in the same way:
> 
>    net "$1" "$2" "$3" "$4" "$5"
>    echo "we saw in mount_drive2:" "$1" "$2" "$3" "$4" "$5"
> 
> or, better yet,
> 
>    [ $# -eq 5 ] || (echo "Invalid number of arguments" >&2 && exit 2)
>    net "$@"
>    echo "we saw in mount_drive2: $@"
> 
> Hope this helps.
> 	Igor
> 
> On Thu, 19 Sep 2002, Scott Prive wrote:
> 
> > > -----Original Message-----
> > > From: Randall R Schulz [mailto:rrschulz@cris.com]
> > > Sent: Wednesday, September 18, 2002 6:30 PM
> > > To: cygwin@cygwin.com
> > > Subject: Re: dumb escaping question when using Cygwin + 
> NT commands
> > >
> > >
> > > Scott,
> > >
> > > At 15:15 2002-09-18, Scott Prive wrote:
> > >
> > > >Hello,
> > > >
> > > >I get this odd problem when calling NT commands from Cygwin. I am
> > > >single-quoting the data, but the way I'm doing things
> > > (probably wrong...)
> > > >does not like passing $1 function arguments to NT commands.
> > > If I hardcode
> > > >the arguments internally, everything works.
> > > >
> > > >The two example functions below are intended to behave identical.
> > > >
> > > >#!/bin sh
> > > >
> > > >mount_drive () {
> > > >    # Syntax: net 'use' '*' '\\redhat\foo' 'foo' '/user:foo'
> > > >    net 'use' 'F:' '\\redhat\foo' 'foo' '/user:foo'
> > > >
> > > >    echo "The command returned $?"
> > > >    return $?;
> > > >}
> > >
> > > Note that the status ($?) you're returning from the
> > > "mount_drive" shell
> > > procedure is that of the "echo" command, not that printed
> > > _by_ the echo
> > > command.
> > >
> > > The only arguments in this example for which quoting 
> changes the net
> > > argument passed to the underlying command is the one that
> > > includes "redhat"
> > > and the asterisk. The others contain no special 
> characters requiring
> > > quoting or escaping to inhibit special interpretation.
> > >
> > >
> > > >mount_drive2 () {
> > > >    net '$1' '$2' '$3' '$4' '$5'
> > > >    echo "we saw in mount_drive2: '$1' '$2' '$3' '$4' '$5' "
> > > >
> > > >    echo "The command returned $?"
> > > >    return $?;
> > > >}
> > >
> > > The same "$?" issue exists here, of course.
> > >
> > > You need to be aware of the difference between 'single
> > > quotes' and "double
> > > quotes." Variable expansion is inhibited in single-quoted
> > > arguments, but
> > > not in double-quoted ones. Furthermore, double quoted
> > > arguments protect
> > > single quotes, making the non-special. So you've probably
> > > confused yourself
> > > into thinking that in this example the "net" command saw the
> > > arguments you
> > > passed to the "mount_drive2" procedure. It did not. It saw
> > > arguments each
> > > consisting of a dollar sign followed by a digit. Then you
> > > echoed a single
> > > argument composed of some fixed text, some single quote marks
> > > and some
> > > expanded positional parameters.
> >
> > Doh!
> >
> > Thanks. A good nights sleep and coffee got me thinking 
> about this on the
> > way to work, and then I read your post.
> >
> > I misled myself because the ECHO command "worked". A debugging habit
> > from Perl is I would print out my variables. Since the echo 
> worked, I
> > never questioned what I was doing with quotes.
> >
> > I assumed quotes controlled how data gets sent to commands, but
> > apparently that's an oversimplification: quotes protect 
> data being sent
> > to a NEW PROCESS.. and builtins like "echo" are NOT a new 
> process (`type
> > echo). This explains why the echo command understood what 
> the heck was
> > inside '$2', but the echo command did not.
> >
> > Of course you know this; I'm just filling in the blanks for 
> the benefit
> > of mailing list and Google searches. For all of last night, 
> I actually
> > believed the problem was due to mixing NT commands and Cygwin.
> >
> > Thanks again.
> >
> > >
> > >
> > > >#
> > > >mount_drive
> > > >mount_drive2 'use' 'G:' '\\redhat\foo' 'foo' '/user:foo'
> > > >############# END SCRIPT
> > > >
> > > >
> > > >the output I get from mount_drive2 is standard "usage info",
> > > indicating I
> > > >passed arguments incorrectly. However the debug echo 
> *looks* correct.
> > > >
> > > >Someone please point out my mistake, else I'm doomed to some
> > > ugly hackish
> > > >workarounds ;-)
> > > >
> > > >Thanks,
> > > >
> > > >Scott
> > >
> > >
> > > Randall Schulz
> > > Mountain View, CA USA
> > >
> > >
> > > --
> > > Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> > > Bug reporting:         http://cygwin.com/bugs.html
> > > Documentation:         http://cygwin.com/docs.html
> > > FAQ:                   http://cygwin.com/faq/
> > >
> > >
> >
> > --
> > Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> > Bug reporting:         http://cygwin.com/bugs.html
> > Documentation:         http://cygwin.com/docs.html
> > FAQ:                   http://cygwin.com/faq/
> >
> >
> 
> -- 
> 				http://cs.nyu.edu/~pechtcha/
>       |\      _,,,---,,_		pechtcha@cs.nyu.edu
> ZZZzz /,`.-'`'    -.  ;-;;,_		igor@watson.ibm.com
>      |,4-  ) )-,_. ,\ (  `'-'		Igor Pechtchanski
>     '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!
> 
> "Water molecules expand as they grow warmer" (C) Popular 
> Science, Oct'02, p.51
> 
> 

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 6+ messages in thread
[parent not found: <7BFCE5F1EF28D64198522688F5449D5AD63A01@xchangeserver2.stor igen.com>]
[parent not found: <7BFCE5F1EF28D64198522688F5449D5AC1E21A@xchangeserver2.stor igen.com>]
* dumb escaping question when using Cygwin +  NT commands
@ 2002-09-18 15:40 Scott Prive
  0 siblings, 0 replies; 6+ messages in thread
From: Scott Prive @ 2002-09-18 15:40 UTC (permalink / raw)
  To: Cygwin


Hello,

I get this odd problem when calling NT commands from Cygwin. I am single-quoting the data, but the way I'm doing things (probably wrong...) does not like passing $1 function arguments to NT commands. If I hardcode the arguments internally, everything works.

The two example functions below are intended to behave identical.

#!/bin sh

mount_drive () {
   # Syntax: net 'use' '*' '\\redhat\foo' 'foo' '/user:foo'
   net 'use' 'F:' '\\redhat\foo' 'foo' '/user:foo'

   echo "The command returned $?"
   return $?;
}

mount_drive2 () {
   net '$1' '$2' '$3' '$4' '$5'
   echo "we saw in mount_drive2: '$1' '$2' '$3' '$4' '$5' "

   echo "The command returned $?"
   return $?;
}

# 
mount_drive
mount_drive2 'use' 'G:' '\\redhat\foo' 'foo' '/user:foo'
############# END SCRIPT


the output I get from mount_drive2 is standard "usage info", indicating I passed arguments incorrectly. However the debug echo *looks* correct.

Someone please point out my mistake, else I'm doomed to some ugly hackish workarounds ;-)

Thanks,

Scott

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2002-09-19 16:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-19  9:00 dumb escaping question when using Cygwin + NT commands Scott Prive
2002-09-19  9:08 ` Igor Pechtchanski
  -- strict thread matches above, loose matches on Subject: below --
2002-09-19  9:28 Scott Prive
     [not found] <7BFCE5F1EF28D64198522688F5449D5AD63A01@xchangeserver2.stor igen.com>
2002-09-19  9:25 ` Randall R Schulz
     [not found] <7BFCE5F1EF28D64198522688F5449D5AC1E21A@xchangeserver2.stor igen.com>
2002-09-18 16:17 ` Randall R Schulz
2002-09-18 15:40 Scott Prive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).