If then else - Retry operation

I need to read a file line by line, then depending on the contents of each line, type in a code that will get written to an array.

The problem I have is when I ask the user to confirm the input code, if it is wrong, how do i Return to ask again?
Any thing I try increments the file to the next line where I need to retry the previous incorrectline.?

I cannot find any examples where aquestion asked but the fail mode os to go back and retry to original option. They all show do one thing or another, but not do one thing or retry.

Thanks
Ken

the contents of FILE.EXT

"D001","50M Run","50M"
"D003","100M Run","100M"
"D004","200M Run","200M"

My script to do the work

#!/bin/bash
# Put Values in an myarrayname and read them back
file="FILE.EXT"     # input filename

while read line ;do
      wardevent=${line:3:2}                        # find the wardells Event code 
      eventnam=$(echo $line | cut -d '"' -f4)        # Extract the event

read -p "Enter the code for the Center event $eventnam  - " mmevent </dev/tty

read -p "Is this Event Code correct? y/n " yes </dev/tty
    if [ "$yes" == "y" ]
        then myarrayname["$wardevent"]="$mmevent"    # Write new line to Array and Continue to next entry
        else exit                                    #Ask again to redo line
    fi 
done <"$file"

You may need to loop it up until you get response as "y" like the following.

#!/bin/bash
# Put Values in an myarrayname and read them back
file="FILE.EXT"     # input filename

yes="n"
while read line ;do
      wardevent=${line:3:2}                        # find the wardells Event code 
      eventnam=$(echo $line | cut -d '"' -f4)        # Extract the event

#read -p "Enter the code for the Center event $eventnam  - " mmevent </dev/tty

#read -p "Is this Event Code correct? y/n " yes </dev/tty
#    if [ "$yes" == "y" ]
#        then myarrayname["$wardevent"]="$mmevent"    # Write new line to Array and Continue to next entry
#        else exit                                    #Ask again to redo line
#    fi 
     until [ "$yes" == "y" ]
     do
       read -p "Enter the code for the Center event $eventnam  - " mmevent </dev/tty
       read -p "Is this Event Code correct? y/n " yes </dev/tty

     done
    myarrayname["$wardevent"]="$mmevent"
    yes="n" 
done <"$file"

Added lines in blue and commented unwanted lines.

1 Like
#!/bin/bash
# Put Values in an myarrayname and read them back
file="file.txt"     # input filename

while read line ;do
      wardevent=${line:3:2}                        # find the wardells Event code
      eventnam=$(echo $line | cut -d '"' -f4)        # Extract the event
      t=1
      while [ $t == "1" ]
      do
            read -p "Enter the code for the Center event $eventnam  - " mmevent </dev/tty
            read -p "Is this Event Code correct? y/n " yes </dev/tty
            if [ "$yes" == "y" ]; then
                myarrayname["$wardevent"]="$mmevent"    # Write new line to Array and Continue to next entry
                t=0
            fi
      done
done <"$file"
1 Like

thank you bot and they do work, Except they run another iteration after the final line in the file.

thus they ask the user for input on a blank lime and write a Blank entry in the array

Any easy way out of that except Querying in line = blank first?

Ken

@krishmaths:
test wants = not ==
And the variable preset can happen once when placed just before the loop

yes="n"
until [ "$yes" = "y" ]
2 Likes
grep -v ^$ $file |while read line ;do
# something here.
done

thanks I just figured it out.

My original input file had an extra blank line in it that was causing the issue

Thanks For you help

Ken