5

I am running such a program:

    min_val=1
    max_val=100
    int=50  
    if [[ "$int" =~ ^-?[0-9]+$ ]]; then
        if [[ "$int" -ge "$min_val" && -le "$max_val" ]]; then
            echo "$int is within $min_val to $max_val."
        else
            echo "$int is out of range."
        fi
    else
        echo "int is not an integer." >&2
        exit 1
    fi

It report error

    $ bash test_integer3.sh
    test_integer3.sh: line 12: conditional binary operator expectedtest_integer3.sh: 
    line 12: syntax error near `"$max_val"'test_integer3.sh: 
    line 12: `    if [[ "$int" -ge "$min_val" && -le "$max_val" ]]; then'

I have examined carefully line-by-line.

What might be the problem?

1
  • 3
    Add a shebang and then paste your script there: shellcheck.net
    – Cyrus
    Apr 3, 2018 at 5:35

2 Answers 2

5
if [[ "$int" -ge "$min_val" && -le "$max_val" ]]; then

You will have to compare against $int in both comparisons:

if [[ "$int" -ge "$min_val" ]] && [[ "$int" -le "$max_val" ]]; then

or,

if (( int >= min_val )) && (( int <= max_val )); then
4

Your -le doesn't have a left operand.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .