tdenniston / bish

Bish is a language that compiles to Bash. It's designed to give shell scripting a more comfortable and modern feel.
MIT License
1.48k stars 36 forks source link

How to compare strings in ifs? #6

Closed richq closed 9 years ago

richq commented 9 years ago

When using if, bish seems to always uses -eq for string comparisons. This makes it impossible (?) to compare strings. I wanted to write a "direxists" that made sure the dir arg wasn't empty, but couldn't do it:

def direxists(dir) {
    if (dir == "") {
    return false;
    }
    @(test -d $dir);
    return success();
}

produces:

function bish_direxists () {
    if [[ $1 -eq "" ]]; then
        echo 0; exit;
    fi;
    test -d $1;
    echo $(bish_success); exit;
}

But this gives an error: line 7: [[: /dirname: syntax error: operand expected (error token is "/dirname"). Line 7 is the if with -eq on it.

tdenniston commented 9 years ago

This is an issue with code generation. Currently it always emits "-eq" for "==" because it does not check the type of the operands. Should be fairly straightforward to fix.

tdenniston commented 9 years ago

Should be fixed with b40db23. Thanks for reporting the issue!