1

When I run codacy-analysis-cli analyze command for the next line of script:

if [[ "$lexer_date" > "$lexer_ts_date" ]]; then
    generate_grammar
fi

I got the next warning: Found [Warning] `In POSIX sh, [[ ]] is undefined.` in scripts/grammar.sh:20 (shellcheck_SC2039)

How can I fix it?

Nizami
  • 35
  • 8
  • Don't use `[[` with `/bin/sh`? The warning is true. `[[` is a feature the `sh` standard doesn't specify, so `/bin/sh` isn't guaranteed to provide it. If you want features that are only added in extended shells like ksh or bash, you need to use an extended shell like ksh or bash. – Charles Duffy Feb 10 '21 at 23:41
  • Beyond that, it's not clear what kind of answer you want. – Charles Duffy Feb 10 '21 at 23:41
  • 1
    ...that said, you might find [What is the difference between `[` and `[[` in bash?](https://stackoverflow.com/questions/3427872/whats-the-difference-between-and-in-bash) useful. – Charles Duffy Feb 10 '21 at 23:43
  • Thank you @CharlesDuffy. I take a look your link. I just wanna to fix warning and looking for alternatives. – Nizami Feb 10 '21 at 23:46
  • 1
    The _best_ alternative is to stop trying to use `/bin/sh` if you need features it doesn't offer, and switch to ksh or bash instead. `[[` exists for a reason (in that many of the features it offers over `[` are impossible without syntax extensions, and `[` is required to be parsed like a regular command, not like syntax). – Charles Duffy Feb 10 '21 at 23:47

1 Answers1

2

Use [ instead. Note that for alphanumeric comparisons you need to quote the comparison operator; thus:

if [ "$lexer_date" ">" "$lexer_ts_date" ]; then
    generate_grammar
fi
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441