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.
#!/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"
#!/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"