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 ] ]
3
  • 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 (which has few other options for how to do the test). Commented Aug 4, 2020 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. Commented Aug 4, 2020 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. Commented Aug 4, 2020 at 16:36

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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 [[ ... ]].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.