Your shell script question answered
Gregory Nowak
greg at romuald.net.eu.org
Fri Mar 30 02:52:54 EDT 2012
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Thanks for your great explanation. I suspected that what was happening
was happening because the script ended up comparing nothing to 1. When
I saw the other examples in this thread, I realized that it might be
because of what you describe. When I changed my script to run the
command, and then check $? in the if statement on the next line,
things worked as i expected. Yes, as I said, the fragment I provided
was to illustrate the problem. A script that expects /bin/true to
return 1 wouldn't be very useful after all.
Thanks again to everyone for your help.
Greg
On Thu, Mar 29, 2012 at 09:00:04PM -0600, D. Curtis Willoughby wrote:
> Hi Greg
>
> Others have told you other ways to skin the cat. I will try to explain
> why what you did did what it did, and suggest a way to make what
> you did work.
>
> I have to say, your script is a little strange, but I assume you did it to
> illustrate something which you are doing in another script.
>
> The true command produces no output on standard out, but only
> returns an exit status of zero.
>
> Since its exit status is zero, the "echo $?" after the || is
> not evaluated. (not executed)
>
> Thus there is no output from the command(s) between the grave accent (`)
> characters.
>
> When the shell evaluates the null string standing alone, it is
> treated as nothing at all, and thus the first non-blank character
> after the open bracket ([) is the equal sign (=). Your error message
> is a complaint from the shell that an equal sign is not allowed
> as the first character inside an open bracket.
>
> When I run your script fragment, i get:
>
> unary operator expected
>
> which means the same thing.
>
> If you want the shell to see that null string as a separate string,
> you must enclose it in quotation marks like this:
>
> if [ "`/bin/true || echo $?`" = 1 ] ; then
> echo "returned 1"
> fi
>
> which does in fact produce no output.
>
> Of course, if you want the script to tell the truth, you should
> do this:
>
> if [ "`/bin/true || echo $?`" = 1 ] ; then
> echo "returned 0" i.e. true
> else
> echo "returned 1" i.e. false
> fi
>
> The zero from the true command is in fact not equal to 1.
>
> I hope this is more informative than confusing.
>
> D. Curtis Willoughby
>
- --
web site: http://www.romuald.net.eu.org
gpg public key: http://www.romuald.net.eu.org/pubkey.asc
skype: gregn1
(authorization required, add me to your contacts list first)
- --
Free domains: http://www.eu.org/ or mail dns-manager at EU.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
iEYEARECAAYFAk91WEUACgkQ7s9z/XlyUyDxsQCfQlCMBa05sza60p0a5wYCzkPr
mLEAmQHrQSJ0ZCE2mizKI+1f4yJfUeXv
=xxLF
-----END PGP SIGNATURE-----
More information about the Speakup
mailing list