I have created one user confirmation process that will ask for user input. I have created one func for it. The issue is if i call it as normal then it works fine but if i am calling it in another script(in while loop) . It is going in infinite loop and not asking for user input.
_prompt () {
while true
do
echo "$1"
read var
[[ $var == "yes" ]] && break
done }
_prompt "Is Process is completed?"
while true
do
##some stuff
##some stuff
_prompt "Parameter files is updated?"
done < file
Yes, it will come as you have put break there, which will only help script to come out from that condition NOT from while loop which you are already running as in TRUE condition. So once user enters "yes" to come out of script, it will never end then because of TRUE condition in loop.
So to avoid this kind of situations, use exit 1 or exit in place of break and let me know how it goes then.
EDIT: If you don't want to come out of script then exit will not be a good option, you could rather try && continue in place of break and let me know how it goes then.
Hello Ravinder , none of those working , if i use exit whole script is exiting and if i use continue or break it is going in infinite.
_prompt () {
while true
do
echo "$1"
read var
[[ "$var" == 'yes' ]] && continue
done }
#_prompt "Is parameter file is updated?"
while true
do
echo "enter in while loop"
_prompt "entered inside while loop"
done < file
echo "outside while loop"
You have two while loops and need to break out of both. Please note that break only works locally, so break 2 in the function won't break the main loop. Use return to pass back a value and use this in the main loop, e.g. in an if construct.
hi , I have replaced while with for loop in mail program , and now it is working fine. I don't know what is going on internally. Please suggest if for loop is correct at this place.
_prompt () {
while true
do
read -p"$1" var
[[ $var == 'yes' ]] && break
done
}
for WfName in $( cat "$dir"/ListOfWf)
do
#some stuff
_prompt "Please check if parameter file is updated ? "
#some stuff
done
I guess you want one to read from the terminal and the other from the file.
The redirection of the outer while loop will make the inner loop also read from the file.
You can use another file descriptor to distinguish between the two input streams.
_prompt () {
while true
do
read -p"$1" var
[[ $var == 'yes' ]] && break
done
}
while read WfName <&3
do
#some stuff
_prompt "Please check if parameter file is updated ? "
#some stuff
done 3<"$dir"/ListOfWf
When the loop starts it will open the file and associate with file descriptor 3 (not 1 as a simple < does).
And you tell the read to read from that descriptor.
You can break out of your loop inside your function but not in the main loop. You need to use 'return <some_value>' and detect the function's return code inside the second loop.
Would it not be better practice to avoid the while true ..... break altogether and code the function like this:-
_prompt () {
var=no
while [ "$var" != "yes" ]
do
echo "$1"
read var
done }
This is assuming that yes is the only allowed input, not Yes , YES , y or yes! Are any variations to be allowed?
I hate break almost as much as goto or switch in other languages because inserting extra code are change the whole logic and (as you have found out) it's hard to work out where it will break to.
Or keep the original order of commands but make the read part of the loop condition; if it fails the loop will end.
_prompt () {
while
echo "$1"
read var && [ "$var" != "yes" ]
do
:
done
}
The shell allows a code list between while - do (and if - then ) - just like the usual code list between do - done (and then - fi ). The code list may not be empty, at least must have a nop command like : .