Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

why does this always pass?

28 views
Skip to first unread message

T

unread,
Apr 10, 2018, 2:50:54 AM4/10/18
to
Hi All,

I have a #!/bin/bash mystery to solve:

Why does this always pass?

if [ ${Mem} > 1500000000 ]; then

For example: ${Mem} is 1373466624 passes.


Many thanks,
-T


Full script:

#!/bin/bash

Pwd=$(pwd)
Pid=$(pidof /opt/redacted.bin)
Mem=$(pmap -x ${Pid} | awk '/total/ { print $4 * 1024 }')
echo "Cim Memory is $Mem"
echo ""

if [ ${Mem} > 1500000000 ]; then
echo "redacted is at ${Mem}"
if [ ! -f /home/linuxutil/core.${Pid} ]; then
gcore ${Pid}
echo ""
echo "Created ${Pwd}/core.${Pid}"

else
echo "The file core.${Pid} already exists"
exit
fi

else
echo "Memory usage is not past 1.5 GB."
exit
fi

echo ""
echo "Redacted Memory is $Mem"
echo ""

Kaz Kylheku

unread,
Apr 10, 2018, 3:01:59 AM4/10/18
to
On 2018-04-10, T <T...@invalid.invalid> wrote:
> Hi All,
>
> I have a #!/bin/bash mystery to solve:
>
> Why does this always pass?
>
> if [ ${Mem} > 1500000000 ]; then

You're executing this command:

[ ${Mem} ]

whose output is redirected to a file called 1500000000.

The "greater than" inequality operator in the [ ... command syntax
is called -gt not >.

The [ ... command isn't special shell syntax; it's just a command
with arguments. The apparently closing ] is just an argument to the [
command. (That said, the [ ... ] is built into Bash and many other
shells).

A > file redirection can occur anywhere in a file. These all
mean the same thing:

$ > file echo "foo"
$ echo > file "foo"
$ echo "foo" > file

hence

[ ${Mem} > 1500000000 ]

is the same as:

[ ${Mem} ] > 1500000000

Bash suppots the expression syntax [[ ... ]] which is true syntax.
That syntax supports < inequality:

if [[ ${Mem} > 150000000 ]] ; then ...

This syntax comes from the Korn shell; it's not in POSIX.

T

unread,
Apr 10, 2018, 3:26:25 AM4/10/18
to
Hi Kaz,

AH POOP!!! And I knew that too!

[ ${Mem} \> 1500000000 ]

fixed it.

Thank you for the second pair of eyes.

-T

To finish things off:
rm 1500000000


Geoff Clare

unread,
Apr 10, 2018, 8:41:06 AM4/10/18
to
T wrote:

>>> Why does this always pass?
>>>
>>> if [ ${Mem} > 1500000000 ]; then

> [ ${Mem} \> 1500000000 ]
>
> fixed it.

I doubt it. Try it with Mem=2 for example.

Seems unlikely you intended a lexical comparison. For a numerical
comparison you need [ ${Mem} -gt 1500000000 ]

--
Geoff Clare <net...@gclare.org.uk>

Kaz Kylheku

unread,
Apr 10, 2018, 2:05:27 PM4/10/18
to
That's now a string lexicographical comparison. You know

"apple" > "banana"

"apple" > "application"

Let me mention -gt once again in case you missed it.

Jorgen Grahn

unread,
Apr 10, 2018, 5:26:08 PM4/10/18
to
And also the man page, which is very short and very clear:

% man [

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

T

unread,
Apr 10, 2018, 7:02:22 PM4/10/18
to
On 04/10/2018 05:32 AM, Geoff Clare wrote:
> T wrote:
>
>>>> Why does this always pass?
>>>>
>>>> if [ ${Mem} > 1500000000 ]; then
>
>> [ ${Mem} \> 1500000000 ]
>>
>> fixed it.
>
> I doubt it. Try it with Mem=2 for example.
>
> Seems unlikely you intended a lexical comparison. For a numerical
> comparison you need [ ${Mem} -gt 1500000000 ]
>

$ rpm -qa bash
bash-4.4.19-1.fc27.x86_64

<code>
#!/bin/bash

if [ 2 \> 3 ]; then echo "2 greater than 3"; else echo "2 less than 3"; fi

if [ 3 \> 2 ]; then echo "3 greater than 2"; else echo "3 less than 2"; fi
</code>


2 less than 3
3 greater than 2

T

unread,
Apr 10, 2018, 7:04:28 PM4/10/18
to
No did not miss it. Did not need it. All I need to do
was escape the redirect so it turned back into a "greater
than". I am comparing numbers, not letters.

Jorgen Grahn

unread,
Apr 10, 2018, 8:07:32 PM4/10/18
to
On Tue, 2018-04-10, T wrote:
> On 04/10/2018 11:05 AM, Kaz Kylheku wrote:
>> On 2018-04-10, T <T...@invalid.invalid> wrote:
>>> Hi Kaz,
>>>
>>> AH POOP!!! And I knew that too!
>>>
>>> [ ${Mem} \> 1500000000 ]
>>>
>>> fixed it.
>>
>> That's now a string lexicographical comparison. You know
>>
>> "apple" > "banana"
>>
>> "apple" > "application"
>>
>> Let me mention -gt once again in case you missed it.
>>
>
> No did not miss it. Did not need it. All I need to do
> was escape the redirect so it turned back into a "greater
> than". I am comparing numbers, not letters.

You can lead a horse to water, but you cannot force it to drink.

Ben Bacarisse

unread,
Apr 10, 2018, 9:44:59 PM4/10/18
to
Try

if [ 22 \> 3 ]; then echo "22 greater than 3"; else echo "22 less than 3"; fi

--
Ben.

T

unread,
Apr 10, 2018, 9:59:06 PM4/10/18
to
Oh poop!

#!/bin/bash

if [ 22 \> 3 ]; then echo "22 greater than 3"; else echo "22 less than
3"; fi
if [ 22 -gt 3 ]; then echo "22 greater than 3"; else echo "22 less than
3"; fi
if [ 2 \> 3 ]; then echo "2 greater than 3"; else echo "2 less than 3"; fi
if [ 3 \> 2 ]; then echo "3 greater than 2"; else echo "3 less than 2"; fi


22 less than 3
22 greater than 3

T

unread,
Apr 10, 2018, 10:09:55 PM4/10/18
to
Figured out where I was so slow on the uptake. I was mixing
Perl with Shell.

Here I am comparing numbers:
$ perl6 -e 'if ( 22 > 3 ) {say "22 > 3" } else { say "22 <= 3" };'
22 > 3


Again, comparing numbers.
$ perl6 -e 'if ( "22" > "3" ) {say "22 > 3" } else { say "22 <= 3" };'
22 > 3


Here I am comparing strings
$ perl6 -e 'if ( 22 gt 3 ) {say "22 > 3" } else { say "22 <= 3" };'
22 <= 3


Again, comparing strings:
$ perl6 -e 'if ( "22" gt "3" ) {say "22 > 3" } else { say "22 <= 3" };'
22 <= 3


Shell uses ">" for strings and "gt" for numbers.

Perl uses ">" for numbers and "gt" for strings and will
lexically convert when it sees ">" or "gt, ge, lt, le, eq"



T

unread,
Apr 10, 2018, 10:13:15 PM4/10/18
to
On 04/10/2018 05:07 PM, Jorgen Grahn wrote:
> You can lead a horse to water, but you cannot force it to drink.

see my last two posts above

Kaz Kylheku

unread,
Apr 10, 2018, 10:20:37 PM4/10/18
to
On 2018-04-10, T <T...@invalid.invalid> wrote:
> if [ 2 \> 3 ]; then echo "2 greater than 3"; else echo "2 less than 3"; fi
>
> if [ 3 \> 2 ]; then echo "3 greater than 2"; else echo "3 less than 2"; fi
></code>
>
>
> 2 less than 3
> 3 greater than 2

Wow, a born computer scientist here!

If you want to test the strength of hypothesis, you have to look for
ways to make it **fail**, not try a some cherry-picked instances where
it works.

Do you know what "lexicographic" means? Loosely speaking, it means
comparing character strings like dictionary words.

Try this case:

if [ 2 > 11 ] ; then echo oops ; fi

The character string "11" is lexicographically anterior with respect
to "2".

Just like the word "eel" comes before the letter "d" in alphabetic order.

Yes; for certain pairs of strings which are the images of decimal
integers. lexicographic order does agree with numeric order. It will
certainly be that way if we compare strings of digits of equal length.

Kaz Kylheku

unread,
Apr 10, 2018, 10:23:11 PM4/10/18
to
On 2018-04-11, Jorgen Grahn <grahn...@snipabacken.se> wrote:
> On Tue, 2018-04-10, T wrote:
>> No did not miss it. Did not need it. All I need to do
>> was escape the redirect so it turned back into a "greater
>> than". I am comparing numbers, not letters.
>
> You can lead a horse to water, but you cannot force it to drink.

Aha; the T stands for "Troll".

Kaz Kylheku

unread,
Apr 10, 2018, 10:34:15 PM4/10/18
to
Oh POOP!!

That makes two of us.

I also took myself for a ride there.

I just thunk over what I'd dun' said there and found it untrue;
the Korn-style [[ ]] syntax doesn't alter the lexicographic
semantics of < and >.

(According to the Bash manual though, it in fact does: Bash
says that under [[ ]], these lexicographic operators compare
strings according to the current locale.)

T

unread,
Apr 10, 2018, 11:52:00 PM4/10/18
to
Oh ya, and this is Perl 6: death to parenthesis!

$ perl6 -e 'if 22 > 3 {say "22 > 3" } else { say "22 <= 3" };'
22 > 3



Kenny McCormack

unread,
Apr 11, 2018, 1:14:14 AM4/11/18
to
In article <201804101...@kylheku.com>,
Yes, the T troll is one of the more entertaining and effective trolls we've
ever seen.

He's been doing this for a long time, primarily in other comp.unix.* groups,
but this is the first I've seen him in action in comp.unix.shell.

Cambridge Analytica would be proud to have him on stuff.

--
A 70 year old man who watches 6 hours of TV a day, plays a lot of golf
and seems to always be in Florida is a retiree, not a president.

Kenny McCormack

unread,
Apr 11, 2018, 1:17:35 AM4/11/18
to
In article <201804101...@kylheku.com>,
Kaz Kylheku <157-07...@kylheku.com> wrote:
...
>Just like the word "eel" comes before the letter "d" in alphabetic order.
................................^^^^^^

I think you'll find that e comes after d.

--

Prayer has no place in the public schools, just like facts
have no place in organized religion.
-- Superintendent Chalmers

Kenny McCormack

unread,
Apr 11, 2018, 1:22:37 AM4/11/18
to
In article <201804101...@kylheku.com>,
Kaz Kylheku <157-07...@kylheku.com> wrote:
>On 2018-04-10, Kaz Kylheku <157-07...@kylheku.com> wrote:
>> On 2018-04-10, T <T...@invalid.invalid> wrote:
>> Bash suppots the expression syntax [[ ... ]] which is true syntax.
>> That syntax supports < inequality:
>>
>> if [[ ${Mem} > 150000000 ]] ; then ...
>>
>> This syntax comes from the Korn shell; it's not in POSIX.
...
>I just thunk over what I'd dun' said there and found it untrue;
>the Korn-style [[ ]] syntax doesn't alter the lexicographic
>semantics of < and >.

My testing agrees with this, and shows that [[ ]] still need -gt to do a
numeric comparison. But, FWIW, the > doesn't need to be escaped (as it
does in simple [ ]).

>(According to the Bash manual though, it in fact does: Bash
>says that under [[ ]], these lexicographic operators compare
>strings according to the current locale.)

Interestly, $(( )) does the right thing:

$ echo $((10 > 2))
1
$ echo $((10 > 22))
0
$

--
There are many self-professed Christians who seem to think that because
they believe in Jesus' sacrifice they can reject Jesus' teachings about
how we should treat others. In this country, they show that they reject
Jesus' teachings by voting for Republicans.

Hikaru Ichijyo

unread,
Apr 11, 2018, 8:19:41 AM4/11/18
to
YOU...SHALL...NOT...PASS!!!! <thud>

Sorry...couldn't resist.

--
He that would make his own liberty secure must guard even his enemy from
oppression; for if he violates this duty, he establishes a precedent
that will reach to himself.
--Thomas Paine

Barry Margolin

unread,
Apr 11, 2018, 11:55:51 AM4/11/18
to
In article <pak62q$qk9$3...@news.xmission.com>,
gaz...@shell.xmission.com (Kenny McCormack) wrote:

> Interestly, $(( )) does the right thing:
>
> $ echo $((10 > 2))
> 1
> $ echo $((10 > 22))
> 0
> $

That's because $(( )) is specifically for evaluating *arithmetic*
expressions. If you try to use strings in it you'll get errors.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Barry Margolin

unread,
Apr 11, 2018, 11:57:25 AM4/11/18
to
In article <pak5pc$qk9$2...@news.xmission.com>,
gaz...@shell.xmission.com (Kenny McCormack) wrote:

> In article <201804101...@kylheku.com>,
> Kaz Kylheku <157-07...@kylheku.com> wrote:
> ...
> >Just like the word "eel" comes before the letter "d" in alphabetic order.
> ................................^^^^^^
>
> I think you'll find that e comes after d.

In *your* dictionary, maybe. :)

Kaz Kylheku

unread,
Apr 11, 2018, 4:38:00 PM4/11/18
to
On 2018-04-11, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article <pak5pc$qk9$2...@news.xmission.com>,
> gaz...@shell.xmission.com (Kenny McCormack) wrote:
>
>> In article <201804101...@kylheku.com>,
>> Kaz Kylheku <157-07...@kylheku.com> wrote:
>> ...
>> >Just like the word "eel" comes before the letter "d" in alphabetic order.
>> ................................^^^^^^
>>
>> I think you'll find that e comes after d.
>
> In *your* dictionary, maybe. :)

Oh POOP!!!

I just checked the the small print on the inside cover of my
Oxford E. D.; it says says "limited-run hashed edition"!

I thought it was a good deal on the discount rack at Chapters.

--
TXR Programming Lanuage: http://nongnu.org/txr
Music DIY Mailing List: http://www.kylheku.com/diy
ADA MP-1 Mailing List: http://www.kylheku.com/mp1
0 new messages