1

I'm having trouble with my nested while loops. My problem is that my inner while loop only executes once but my outer loop is able to continue to repeat. Here's my code:

while [[ "$input" != "exit" && "$input" != "Exit" && "$input" != "EXIT" ]]
do

  echo 'Enter "name" to find files by name'

  read input


  if [[ "$input" = "name" || "$input" = "Name" || "$input" = "NAME" ]]; then


     while [[ "$a" != "f" && "$a" != "s" ]]
     do
        read -p "Want to search inside only the current folder (f) or include subfolders (s)" a
        if [ "$a" = "f" ]; then
           echo 'f'

        elif [ "$a" = "s" ]; then
           echo 's' 

        elif [[ "$a" != "f" && "$a" != "s" ]]; then
           echo 'you have entered an invalid option, please try again'

        fi
     done
  fi
done

Here's a sample output:

Enter "name" to find files by name

Enter "size" to find files with a specified size

Enter "date" to find files by date

Enter "string" to find files containing a particular string

Enter "exit" to exit the script.

name

Want to search inside only the current folder (f) or include subfolders (s)t

you have entered an invalid option, please try again

Want to search inside only the current folder (f) or include subfolders (s)s

s

Enter "name" to find files by name

Enter "size" to find files with a specified size

Enter "date" to find files by date

Enter "string" to find files containing a particular string

Enter "exit" to exit the script.

name # right here is where my inner loop failed to run but I don't see why

Enter "name" to find files by name

Enter "size" to find files with a specified size

Enter "date" to find files by date

Enter "string" to find files containing a particular string

Enter "exit" to exit the script.

0

1 Answer 1

3

Your code does not seem to match the printout you present. It's also not clear what your problem is. But the code has the distinct problem that it compares the value of input before reading it. You need to read the input, then compare it to something. Fortunately, this is not hard to do, because you can have multiple commands in the while condition. The other problem seems to be that you never break out of the inner loop.

The following is also refactored to use the more-elegant case statement instead of the repetitive comparisons.

while read -p 'Enter "name" to find files by name' input
      [[ ! $input =~ [Ee]xit|EXIT ]]
do
     case $input in
       [Nn]ame | NAME )
         while read -p "Want to search inside only the current folder (f) or include subfolders (s)" a
             case $a in
               f)
                 echo 'f'
                 break ;;   # success: break out of while loop
               s)
                 echo 's'
                 break ;;
               *)
                 echo 'you have entered an invalid option, please try again' ;;
             esac
         done ;;
     esac
 done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.