Problem with single quotes

View: New views
3 Messages — Rating Filter:   Alert me  

Problem with single quotes

by grvs :: Rate this Message:

| View Threaded | Show Only this Message

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.

x=3
y='[ $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 ]

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)

Re: Problem with single quotes

by Eric Blake :: Rate this Message:

| View Threaded | Show Only this Message

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


Re: Problem with single quotes

by grvs :: Rate this Message:

| View Threaded | Show Only this Message

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