Read function is going in infinite in another script having while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read function is going in infinite in another script having while loop
# 1  
Old 06-06-2017
Read function is going in infinite in another script having while loop

Hello Experts,

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.
Code:
_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

Kindly help me on this.
# 2  
Old 06-06-2017
Hello looney,

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.

Thanks,
R. Singh

Last edited by RavinderSingh13; 06-06-2017 at 05:06 PM..
# 3  
Old 06-06-2017
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.

Code:
_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"

# 4  
Old 06-07-2017
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.
# 5  
Old 06-07-2017
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.
Code:
_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


Last edited by looney; 06-07-2017 at 12:51 PM.. Reason: edit for and while
# 6  
Old 06-07-2017
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.
Code:
_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.
# 7  
Old 06-07-2017
I have made the same mistake very recently.

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.

Has AudioScope found a bug in bash 4.4.5?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Nmap shell script goes in infinite loop

My script’s output goes in infinite loop Below is my script: Nmap() { while read -r line do name="$line" echo "$name" count=$line nmap -oG output.txt -T4 -f -iL iplist.txt $line1 done < iplist.txt } Nmap ................................................................. ... (2 Replies)
Discussion started by: sk151993
2 Replies

2. Shell Programming and Scripting

Calling a script from a shell that needs to cancel out of infinite loop

I am writing a shell script that calls this oracle utility to get some information about the DB that I need for the script https://docs.oracle.com/cd/B16240_01/doc/em.102/e15294/options.htm This is the command that I am running: $ORACLE_HOME/OPatch/opatch lsinventory -details | grep -i... (1 Reply)
Discussion started by: guessingo
1 Replies

3. UNIX for Advanced & Expert Users

Help - Infinite Loop: Error in trap function

Hi, I was working on implementing error handling in my bash scripts, and decided to use trap to send myself an email incase of any errors. But it seems that somethings has gone wrong, and I am continuously getting same emails for an old error repeatedly (even though I have stopped/killed all... (1 Reply)
Discussion started by: cool.aquarian
1 Replies

4. Shell Programming and Scripting

Select command going to infinite loop after running the script

cd /opt/et/WAS/apps/8.0 find . -name "HostIntegration.properties" -o -name "HostSocket.properties" -o -name "environment.properties" 2> /dev/null | awk -F '' '{print $4}'|awk '!x++' | cat>/home/cbadmin/file1.txt cd /home/cbadmin/ PS3='Please enter a number from list of applications==>:' select... (3 Replies)
Discussion started by: bhas85
3 Replies

5. Shell Programming and Scripting

Infinite while loop script shows more than one process

Hi, I have a script which triggers an infinite loop. #!bin/bash trig=`ls /home/trig.tch |wc -l` function callj { some commands... } while do callj & done The number of process after doing a ps -ef |grep Mon.sh returns processes even after the script is killed by deleting the... (4 Replies)
Discussion started by: chetan.c
4 Replies

6. Shell Programming and Scripting

Script with infinite loop stops after sometime

Hi I am working on a server that is set up and maintained by a third party. It seems whenever I run bash scripts in the background (with a &) with while loops in them they seem to me killed in around 2.5 hours. ( I am running them as a normal user with no special privileges ) . Is there a... (3 Replies)
Discussion started by: pkabali
3 Replies

7. Shell Programming and Scripting

Infinite while loop

what is the difference between while:,while true and while false? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

8. Programming

__read_nocancel Function Causes Infinite Loop

Does anyone know what __read_nocancel does and why it would go into an infinite loop? What I have gathered in my searches is that it pertains to server code. Yet, I'm not running this application in server mode. NOTE: There are server functions in the shared object, but the specific code... (3 Replies)
Discussion started by: marcus121
3 Replies

9. Shell Programming and Scripting

Script to run infinite loop

Hi all, I have a script which triggers batch admin manager and gets the top 10 jobs and their status info. the output of this script is the list of all these jobs. I want to run this in infinite loop which will show top 100 jobs' status. the script is as follows #!/bin/sh exec &> capture1.txt... (1 Reply)
Discussion started by: digitalrg
1 Replies

10. Shell Programming and Scripting

Running a script in INFINITE LOOP

Hi All, I have a requirement as below. I supposed to get a file from Source system once in a month. But we dont know when the source system will send the file. My script has to wait for that file in LOOP once it gets the file then it has to FTP the file. I thought of scheduling the job... (5 Replies)
Discussion started by: Raamc
5 Replies
Login or Register to Ask a Question