I am new in bash scripting. I want to create an interactive script which is prompt user to enter data for editing a file line by line.
The scenario:
- Read a file and iterate over each line, I use for in
Ask user whether to edit the line or not
If yes, do the editing
If not, continue to the next line
End the interaction after everything finish.
My approach:
# --> get file contents and convert them to an array
readarray thearray < ips.info
# --> Iterate the array and do interactive editing
for item in ${!thearray[@]}; do
if [[ "$item" == 0 ]]; then
echo -e "First line: ${thearray[$item]}. Change this line? (y/n)"
read Useranswer
if [ $Useranswer = y]; then
echo "Please type any string:"
read Firststring
elif [ $Useranswer = n]; then
# not sure what to write here to resume
fi
fi
done
echo "Everything done!"
Is there any mistake in my code above and how to resume if the user press n
on their keyboard?
[ $Useranswer = n];
will throw an error, the closing]
needs to have a space before it, i.e.[ $Useranswer = n ];