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

Re: Bash Question



On Thu, Jul 06, 2017 at 12:22:29AM +0200, Javier Barroso wrote:
Hi,

On Wed, Jul 5, 2017 at 11:12 PM, Rainer Dorsch <ml@bokomoko.de> wrote:
Hi,



can anybody help to explain what is going on here ?





rd@mohot:~$ echo $SHELL
/bin/bash
rd@mohot:~$ if [ "abc" > "dec" ]; then echo bad; fi
bad
rd@mohot:~$ if [ "abc" < "dec" ]; then echo good; fi
good
rd@mohot:~$

How can abc sort before and after dec at the same time?

You need to scape "<" and ">":

if [ "abc" \> "dec" ] ; then ... ;fi
if [ "abc" '>' "dec" ]; then ... ; fi
if [ "abc" ">" "dec" ]; then ... ; fi


Delete "dec" file, ( ">" is redirection in bash, so you was creating that file

That's a very good point. ">" and "<" are NOT the greater-than/less-than
operators in bash. [ "abc" > "dec" ] tests whether the result of "abc"
can be written to the file "dec". Note, however, the subtle difference
between, say [ echo "abc" > "dec" ] where echo executes and copies its
arguments to stdout, which is then written to the file - the success of
this operation is tested; [ "abc" > "dec" ] will try to run "abc". It
looks like bash will interpret the string as a command, so this is
equivalent to [ abc > dec ], i.e. running the command "abc" and
redirecting its output to the file "dec".

The correct operators are actually "-lt" and "-gt". However, these
operators only work on INTEGERs. If you really need to test for "sorts
before", then you may need to resort to using sort itself.

Regards


--
For more information, please reread.

Attachment: signature.asc
Description: PGP signature


Reply to: