Good morning folks,

I have to do this for my class assignment and I don't quite grasp how to.

The script will ask the user for two separate statements which will fill two variables, L and C. I need to check if L and C, both, are between the range from 1 to 3. If either L or C has a value lower than 1 and/or higher then 3 (outside the range), the script has to print a warning and ask for new values.

printf "Please, enter L and C: "
read L C
while [[ $L -ge 1 -a $L -le 3 ]]; do
    while [[ $C -ge 1 -a $C -ge 3 ]]; do

This works as intended (the loop). Is there any way to better handle this?




In the interest of readability, I would do something like:

checkrange() {
   if [[ "$1" -le 3 ]] && [[ "$1" -ge 1 ]]; then
      return 0
      return 1

prompt="Please enter values for L and C >"
while ! ( checkrange "$L" && checkrange "$C" ); do
   read -p "$prompt" L C
   prompt="Invalid input; please enter new values for L and C >"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.