[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [lmi] Shell scripting bafflement
From: |
Vadim Zeitlin |
Subject: |
Re: [lmi] Shell scripting bafflement |
Date: |
Wed, 26 Feb 2020 23:37:26 +0100 |
On Wed, 26 Feb 2020 22:27:11 +0000 Greg Chicares <address@hidden> wrote:
GC> Vadim--Sorry to bother you with this, but I just can't see why
GC> these two nearly-identical commands behave oppositely, and I'm
GC> sure you'll know why.
I think I do, but I feel like I'm missing something crucial here: why do
you say that these commands are nearly-identical when it looks to me that
they're not identical at all?
GC> This does exactly what I expect, as [non]gnu.org is certainly
GC> reachable on my machine:
GC>
GC> /home/greg[0]$if curl https://git.savannah.nongnu.org:443 >/dev/null 2>&1 ;
then
GC> echo true
GC> else
GC> echo false
GC> fi
GC>
GC> true
This checks the exit code of curl command, which is 0 in this case, and so
is considered to be "true".
GC> But if I enclose the command in "[ $(...) ]", it "fails":
This tests... nothing. $(...) expands to nothing because there is no
output of curl, as you had taken care to suppress it, and so this is
equivalent to "[ ]". To be honest, I didn't know what does testing nothing
does, but it turns out that it always returns false. It can be seen by
doing
$ [ ] || echo false
false
or
$ test || echo false
false
GC> /home/greg[0]$
GC> /home/greg[0]$if [ $(curl https://git.savannah.nongnu.org:443 >/dev/null
2>&1) ]; then
GC> echo true
GC> else
GC> echo false
GC> fi
GC>
GC> false
So this doesn't seem surprising to me at all. But I can't figure out what
was your intention here, i.e. _why_ do you use the command expansion shell
construct and what exactly do you expect to obtain from it?
Please let me know if I'm missing something obvious here,
VZ
pgpq1bTzuiexH.pgp
Description: PGP signature