Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16021

Re: numerical comparison missing in bash and expr

Path csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail
From "Joseph A. Russo" <jarusso@rochester.rr.com>
Newsgroups gnu.bash.bug
Subject Re: numerical comparison missing in bash and expr
Date Wed, 11 Mar 2020 09:38:28 -0400
Lines 53
Approved bug-bash@gnu.org
Message-ID <mailman.2467.1583933949.2412.bug-bash@gnu.org> (permalink)
References <bb02976e-6a47-57eb-9cb4-ba25cfd2855b@rochester.rr.com> <20200311132424.GD845@eeg.ccf.org> <98561a3f-d023-eb90-031b-2d2731b12dcd@rochester.rr.com>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace usenet.stanford.edu 1583933950 27706 209.51.188.17 (11 Mar 2020 13:39:10 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org, bash@packages.debian.org
Envelope-to bug-bash@gnu.org
X-Authority-Analysis v=2.3 cv=SZ2JicZu c=1 sm=1 tr=0 a=5mZlSMuCHeynFv3BzUaTVw==:117 a=5mZlSMuCHeynFv3BzUaTVw==:17 a=jpOVt7BSZ2e4Z31A5e1TngXxSK0=:19 a=IkcTkHD0fZMA:10 a=AjUqAuWvlPkexdZSJbwA:9 a=QEXdDO2ut3YA:10
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1
In-Reply-To <20200311132424.GD845@eeg.ccf.org>
Content-Language en-US
X-CMAE-Envelope MS4wfGzHEXjMzSoruMoPQUvZYDMUxR4n9u7sBPy752YIM71YaL1V6E6cs9eSyhZOn70YWU2JxOhYrgaZG3ZbcgMSbO3JsLBBrvezpK+mZ2dkkF87iWi4bXMH OBxQVoW3BGBzhzYKOr7BDRaeYOqBn+TH4qcBDdpc81goc0zU8HLEmLKz2KELwE+4ePK3lGhqQH4BKV9SsYxKV5TsE8zPHCCDwIU=
X-detected-operating-system by eggs.gnu.org: Genre and OS details not recognized.
X-Received-From 47.43.26.138
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <98561a3f-d023-eb90-031b-2d2731b12dcd@rochester.rr.com>
X-Mailman-Original-References <bb02976e-6a47-57eb-9cb4-ba25cfd2855b@rochester.rr.com> <20200311132424.GD845@eeg.ccf.org>
Xref csiph.com gnu.bash.bug:16021

Show key headers only | View raw


Hi,

Thank you

I appreciate the explanation and the prompt response.

Joe

On 3/11/20 9:24 AM, Greg Wooledge wrote:
> On Wed, Mar 11, 2020 at 07:29:38AM -0400, Joseph A. Russo wrote:
>> # the following two lines work	
>> [ 5 < 10 ] && echo true || echo false
>> [ 5 > 10 ] && echo true || echo false
> You've misunderstood the syntax here.  The < sign introduces a
> redirection of standard input, from a file named "10".
>
> What you've written is equivalent to
>
> [ 5 ] < 10 && echo true || echo false
>
> The command [ 5 ] tests whether the string "5" has a non-zero length.
> It does, and so the command returns "true" (exit status 0).
>
> The SECOND command uses > which introduces an output redirection, to
> a file named "10".  I believe you will find there is an empty file
> named "10" in your working directory now, assuming you didn't delete
> it after your tests.
>
>> # the next three lines fail
>> n=5
>> [ $n < 10 ] && echo true || echo false
>> [ $n > 10 ] && echo true || echo false
> These commands are identical to the previous set.  The only difference
> is whether the file named "10" existed or not.
>
> If these "failed", it's perhaps because you ran these commands first,
> before the file existed.  And you ran the first set of commands second,
> after the file was created, which suppresses the error you would have
> got from the input redirection when the file was missing.
>
> There are various correct ways to perform integer comparisons in
> bash.  From oldest to newest:
>
> test "$n" -lt 10
>
> [ "$n" -lt 10 ]
>
> [[ $n -lt 10 ]]
>
> ((n < 10))
>
> The first two are POSIX compatible.  The second two are bash extensions.

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: numerical comparison missing in bash and expr "Joseph A. Russo" <jarusso@rochester.rr.com> - 2020-03-11 09:38 -0400

csiph-web