bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: If Conditional Logic Error in Bash


From: Chet Ramey
Subject: Re: If Conditional Logic Error in Bash
Date: Wed, 23 Nov 2022 10:32:07 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.5.0

On 11/23/22 5:31 AM, Neko-san via Bug reports for the GNU Bourne Again SHell wrote:
Example:
https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=unreal-engine&id=c866141788f5c52cd32ddfaea3e8f5f5ca57b3ae#n135

There's a logic bug at play in Bash where having a value optionally be either a 
string (in this case true/false) or an integer, checked by an if statement, can 
throw an error claiming that an integer is expected even though a || operator 
is used to make integers optional.

This is just bad code.

The line in question is:

if [ "${_use_system_clang}" == true ] || [ "${_use_system_clang}" -eq 1 ]; then

Consider what happens if `_use_system_clang' is set to any non-integer
value other than `true' (e.g. `false', as in this report).

The first test command fails, so the || list executes the second test,
which expands to

[ false -eq 1 ]

Since the `-eq' operator requires integer operands, you get this error:

line 135: [: false: integer expression expected.


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]