Problem with single quotes
Eric Blake
ebb9@byu.net
Mon Jun 29 14:38:00 GMT 2009
-----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@byu.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
More information about the Cygwin
mailing list