www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2009/06/29/14:03:29

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-2.6 required=5.0 tests=BAYES_00,SPF_HELO_PASS,SPF_PASS
X-Spam-Check-By: sourceware.org
Message-ID: <24258969.post@talk.nabble.com>
Date: Mon, 29 Jun 2009 11:03:07 -0700 (PDT)
From: grvs <grvsinghal AT gmail DOT com>
To: cygwin AT cygwin DOT com
Subject: Re: Problem with single quotes
In-Reply-To: <4A48AD1C.9050408@byu.net>
MIME-Version: 1.0
References: <24249272 DOT post AT talk DOT nabble DOT com> <4A48AD1C DOT 9050408 AT byu DOT net>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

Thanks Eric for your reply.
Yeah you are right, I am actually new to shell scripting and I downloaded
cygwin only to learn shell scripting. And this script was one of the
assignment question of the book I am reading to learn shell scripting. 
Actually now it may seem funny but I mistakenly used single quotes instead
of back quotes (key left to 1 in the keyboard) which I figured out just now.
If I use back quotes, my code also works. :)

And thanks again, I got to know many new things from your such a exhaustive
and well framed reply, and a work around of the problem I was trying to
solve.

Regards



Eric Blake wrote:
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> According to grvs on 6/29/2009 1:00 AM:
>> Hi all
>> I am new in cygwin as well as linux and I am trying to learn shell
>> scripting
>> I tried to write following script which doesn't give me appropriate
>> result.
> 
> Your question is not cygwin-specific.  You would be better off getting a
> good shell scripting tutorial rather than trying to learn shell scripting
> from this list.
> 
>> 
>> x=3
>> y='[ $x -eq 10 ]'
> 
> Here, the single quoting tells the variable assignment that the variable
> contains text that would otherwise be split into multiple words by the
> shell.  But the single quotes are not assigned to the variable.  So y
> holds the text "[ $x -eq 10 ]", not "'[ $x -eq 10 ]'".
> 
>> z='[ $x -lt 10 ]'
>> echo x=&x y=$y z=$z
>> 
>> and the output is:
>> x=5 y=[ $x -eq 10 ] z=[ $x -lt 10 ]
> 
> Correct (although you got lucky that you didn't have consecutive
> whitespace, which would have been eaten by your underquoted echo
> statement).
> 
>> 
>> I expected
>> x=5 y=0 z=1
>> or x=5 y=1 z=0 ( I am not sure till now that whether 0 is true or 1 is
>> true)
> 
> In shell scripting, true tests return 0 (success), false tests return
> non-zero (usually 1, but can be 2-255).  Yes, that's backwards from C
> conventions.
> 
> Oh, so you wanted indirect evaluation and command substitution, and you
> wanted the exit status of running the command.  Use eval, as in:
> 
> eval echo x="$x" y='$('"$y"'; echo $?)' z='$('"$z"'; echo $?)'
> 
> Be careful, though - indirect evaluation, if used incorrectly, is a big
> cause of security holes in shell scripts.
> 
> - --
> Don't work too hard, make some time for fun as well!
> 
> Eric Blake             ebb9 AT byu DOT net
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (Cygwin)
> Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> 
> iEYEARECAAYFAkpIrRwACgkQ84KuGfSFAYDcaACgzhjUpbckZSQwVBs6+5yvGJVT
> 3pcAoJXi5mkyZXFsBB5zRbDNAvi0nVEt
> =a+aQ
> -----END PGP SIGNATURE-----
> 
> --
> Problem reports:       http://cygwin.com/problems.html
> FAQ:                   http://cygwin.com/faq/
> Documentation:         http://cygwin.com/docs.html
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Problem-with-single-quotes-tp24249272p24258969.html
Sent from the Cygwin list mailing list archive at Nabble.com.


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019