DAT BOI
DAT BOI

Reputation: 183

nested while loops (bash scripting in linux)

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.

Upvotes: 1

Views: 14858

Answers (1)

tripleee
tripleee

Reputation: 189397

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

Upvotes: 3

Related Questions