dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* "if [ s1 > s2 ]" broken, writing a s2 file
@ 2014-12-08 17:32 solsTiCe d'Hiver
  2014-12-08 17:39 ` Eric Blake
  0 siblings, 1 reply; 3+ messages in thread
From: solsTiCe d'Hiver @ 2014-12-08 17:32 UTC (permalink / raw)
  To: dash

hello,
folowing that bug
https://bugs.launchpad.net/ubuntu/+source/update-notifier/+bug/1400357,
I follow through to investigate and I found out
that whatever I try, when comparing 2 strings I always end up with a
file written to disk

From the man page
test expression
     [ expression ]
[...]
s1 > s2       True if string s1 comes after s2 based on the ASCII
value of their characters.

when I try to use it:
a="ert"
b="aze"
if [ $a > $b ] ; then
echo yes
fi
if [ "aer" > "azer" ] ;then
echo yes
fi

I got 2 yes printed on screen whereas only one should and most
importantly I got 2 empty files written to disk: one called "aze", and
another one "azer"

so this "if syntax" is broken or I don't knwo how to use it.

Also it is really dangerous  to use a syntax similar to file
redirection and this is exactly what is happening here.

this is happening with 0.5.8 too

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: "if [ s1 > s2 ]" broken, writing a s2 file
  2014-12-08 17:32 "if [ s1 > s2 ]" broken, writing a s2 file solsTiCe d'Hiver
@ 2014-12-08 17:39 ` Eric Blake
  2014-12-08 17:46   ` solsTiCe d'Hiver
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Blake @ 2014-12-08 17:39 UTC (permalink / raw)
  To: solsTiCe d'Hiver, dash

[-- Attachment #1: Type: text/plain, Size: 1663 bytes --]

On 12/08/2014 10:32 AM, solsTiCe d'Hiver wrote:
> hello,
> folowing that bug
> https://bugs.launchpad.net/ubuntu/+source/update-notifier/+bug/1400357,
> I follow through to investigate and I found out
> that whatever I try, when comparing 2 strings I always end up with a
> file written to disk
> 
>>From the man page
> test expression
>      [ expression ]
> [...]
> s1 > s2       True if string s1 comes after s2 based on the ASCII
> value of their characters.

You HAVE to escape the > so that it is interpreted as an argument and
not a redirection operator.  The bug is not in dash, but in your usage.

> 
> when I try to use it:
> a="ert"
> b="aze"
> if [ $a > $b ] ; then

Wrong.  Use:

if [ "$a" ">" "$b" ]; then


> 
> so this "if syntax" is broken or I don't knwo how to use it.

The latter.

> 
> Also it is really dangerous  to use a syntax similar to file
> redirection and this is exactly what is happening here.

POSIX is proposing the addition of the shell builtin [[ ]], where
because it is a syntactical part of the shell, it would have safe
semantics (that is, [[ $a > $b ]] would be perfectly safe and do the
right thing). But until the POSIX standardization is complete, dash does
not implement [[; and as long as only '[' is portable (with its
unfortunate but historically-mandated semantics of operating as if it
were NOT a builtin, in that shell parsing happens before test sees its
arguments), then you have to quote anything that might otherwise be
misinterpreted during parsing.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 539 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: "if [ s1 > s2 ]" broken, writing a s2 file
  2014-12-08 17:39 ` Eric Blake
@ 2014-12-08 17:46   ` solsTiCe d'Hiver
  0 siblings, 0 replies; 3+ messages in thread
From: solsTiCe d'Hiver @ 2014-12-08 17:46 UTC (permalink / raw)
  To: Eric Blake; +Cc: dash

thank you to enlighten me on that. I just learned another thing today. ;-)

2014-12-08 18:39 GMT+01:00 Eric Blake <eblake@redhat.com>:
> On 12/08/2014 10:32 AM, solsTiCe d'Hiver wrote:
>> hello,
>> folowing that bug
>> https://bugs.launchpad.net/ubuntu/+source/update-notifier/+bug/1400357,
>> I follow through to investigate and I found out
>> that whatever I try, when comparing 2 strings I always end up with a
>> file written to disk
>>
>>>From the man page
>> test expression
>>      [ expression ]
>> [...]
>> s1 > s2       True if string s1 comes after s2 based on the ASCII
>> value of their characters.
>
> You HAVE to escape the > so that it is interpreted as an argument and
> not a redirection operator.  The bug is not in dash, but in your usage.
>
>>
>> when I try to use it:
>> a="ert"
>> b="aze"
>> if [ $a > $b ] ; then
>
> Wrong.  Use:
>
> if [ "$a" ">" "$b" ]; then
>
>
>>
>> so this "if syntax" is broken or I don't knwo how to use it.
>
> The latter.
>
>>
>> Also it is really dangerous  to use a syntax similar to file
>> redirection and this is exactly what is happening here.
>
> POSIX is proposing the addition of the shell builtin [[ ]], where
> because it is a syntactical part of the shell, it would have safe
> semantics (that is, [[ $a > $b ]] would be perfectly safe and do the
> right thing). But until the POSIX standardization is complete, dash does
> not implement [[; and as long as only '[' is portable (with its
> unfortunate but historically-mandated semantics of operating as if it
> were NOT a builtin, in that shell parsing happens before test sees its
> arguments), then you have to quote anything that might otherwise be
> misinterpreted during parsing.
>
> --
> Eric Blake   eblake redhat com    +1-919-301-3266
> Libvirt virtualization library http://libvirt.org
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-12-08 17:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-08 17:32 "if [ s1 > s2 ]" broken, writing a s2 file solsTiCe d'Hiver
2014-12-08 17:39 ` Eric Blake
2014-12-08 17:46   ` solsTiCe d'Hiver

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).