delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2009/06/29/08:01:40

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=AWL,BAYES_00,SPF_SOFTFAIL
X-Spam-Check-By: sourceware.org
Message-ID: <4A48AD1C.9050408@byu.net>
Date: Mon, 29 Jun 2009 06:01:32 -0600
From: Eric Blake <ebb9 AT byu DOT net>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.22) Gecko/20090605 Thunderbird/2.0.0.22 Mnenhy/0.7.6.666
MIME-Version: 1.0
To: cygwin AT cygwin DOT com, grvsinghal AT gmail DOT com
Subject: Re: Problem with single quotes
References: <24249272 DOT post AT talk DOT nabble DOT com>
In-Reply-To: <24249272.post@talk.nabble.com>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.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

-----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

- Raw text -


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