0
function read_num(){
            echo "Enter a lower limit"
            read lower_limit
            echo "Enter a upper limit"
            read upper_limit
            while [ [ $lower_limit -lt 1 ] || [ $lower_limit -gt $upper_limit ] ]
            do
            echo "Please enter again."
            read_num
            done 
        }
    
    read_num

when I enter the two numbers lower and upper limit it gives the following output.

check.sh: line 6: [: too many arguments 

And line number 6 is while loop

while [ [ $lower_limit -lt 1 ] || [ $lower_limit -gt $upper_limit ] ]
  • 1
    Remove the outer square brackets. `[ ]` expressions don't nest, and you can't use `||` inside a `[ ]` test expression (but you can *between* them). This is a near-duplicate of [this question](https://stackoverflow.com/questions/14328606/bash-if-block-syntax) (which has few other options for how to do the test). – Gordon Davisson Aug 04 '20 at 16:19
  • `[` is a command. You are trying to invoke the `[` command with arguments `[`, `$lower_limit`, `-lt`, `1`, and `]`. That's too many arguments. These kinds of error are easily avoided if you use `test` instead of `[`. It is less likely to be confused as a part of the grammar. – William Pursell Aug 04 '20 at 16:25
  • 1
    aside from the syntax errors, you've mispelled ```read uper_limit``` - should be ```upper_limit``` - otherwise you'll get into the infinite recursive call. – vgersh99 Aug 04 '20 at 16:36

1 Answers1

0

Here you go, this works for me:

#!/bin/bash

function read_num(){
    echo "Enter a lower limit"
    read lower_limit
    echo "Enter a upper limit"
    read uper_limit
    while [[ $lower_limit -lt 1 ]] || [[ $lower_limit -gt $upper_limit ]]
    do
    echo "Please enter again."
    read_num
    done
}

read_num

Reference: Bash scripting, multiple conditions in while loop

  • 1
    Hi please be sure to provide a description as to how this solution corrects the submitted OP's question. A good description here would be what was wrong with OP's code snippet and how you addressed those issues in your answer. – jkdba Aug 04 '20 at 16:55
  • 1
    If you are going to use `bash` extensions, use `(( lower_limit < 1 || lower_limit > upper_limit))`. There's virtually no reason to use `-lt` et al. with `[[ ... ]]`. – chepner Aug 04 '20 at 17:12