On Sun, Sep 3, 2017 at 5:06 PM, Mark Volkmann <r.mark.volkm...@gmail.com>
wrote:

> IIUC, there isn't a command in fish that can be used to compare two
> strings to see if one is greater than the other in sorting order. For
> example, this doesn't work:
>
> set v1 'foo';
> set v2 'bar';
> if test $v1 > $v2
>   ...
> end
>
> Is there a recommended workaround for this?
>

The POSIX test command doesn't support that and fish has generally avoided
adding incompatible extensions to it. See
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html. The
`test` command is perhaps the only area where fish is a bit too slavish
vis-a-vis POSIX compatibility where everywhere else feeling free to
deliberately not be POSIX compliant. Feel free to open an enhancement
request.

I can't think of a situation where I have needed such a feature in the past
couple of decades. At least not where using another language (e.g., awk,
perl or python) would not have been a better choice. Having said that you
could do this with a simple awk one liner then test the exit status:

function str_lt_str
    if test (count $argv) -ne 2
        echo str_lt_str: requires exactly two arguments
        exit 2
    end

    awk -v s1=$argv[1] -v s2=$argv[2] 'END { if (s1 < s2) exit 0; exit 1 }'
</dev/null
end

I used awk because there is no guarantee that `command test` implements
this feature. The greater-than case should be obvious.

P.S., Don't forget that `<` and `>` are special characters that perform
redirection. So even if the fish builtin `test` command supported those
operators with strings you would need to escape or quote the symbols.

-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Fish-users mailing list
Fish-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/fish-users

Reply via email to