How to get script to wait until status is true before continuing?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get script to wait until status is true before continuing?
# 8  
Old 05-15-2013
I apoligize. As I said I am very new to this. I didn't know it was different for different shells, but I will be sure to include that information next time.
I wanted to let you all know that I got it to work with the following code:
Code:
while (0 == 0)
   ls | grep $name >& /dev/null
   if ($status == 0) then
      break
   else
      sleep 1
   endif
end

A buddy of mine told me that there problems may arise with just saying:
Code:
while (0 == 0)

and that something like:
Code:
x=0
while ($x == 0)

would be better. Can anyone explain why?
# 9  
Old 05-17-2013
You can test many ways in sh, ksh, bash using different delimiters: [] () (()) [[]]
  • () is same as cmd, a subshell, and the return (exit) 0 is true.
  • Basically, [ is test, see man test.
  • ksh/bash (()) are very nice for math, logic, numbers and bits, with operators + - * / % && & || | () and such like C.
  • bash [[]] allows regex testing.
Using builtins is much cheaper than fork/exec a command or two, and the test bits are now built in, so to find a file, it is far simpler to [ -f file ] than ( ls | grep file ), which is two fork/exec and a pipe. For the former, the shell just does an access() call.

Polling files is a sad way to flow control jobs. It harkens back to the days of tape. The shell has better ways to keep track of children, e.g., (proc1&proc2&proc3&wait;proc4), and many times pipes make flow control and intermediate flat files unnecessary while reducing latency by pipeline parallelism: proc4 <(proc1) <(proc2) <(proc3)
# 10  
Old 05-22-2013
Thank you for the information. You are very helpful. I will give this a try. Someone told me about waitpid and so I gave that a try, but I found out that the process is not a child process. I didn't know this before or I would've provided that info. Do you have a suggestion for a book I can buy or a web reference to learn a lot of this stuff? I feel I ask to many questions on here, and you have been very helpful, but I feel I'm in over my head. I'm a chemist by training so this is all new to me. So if you know of a good resource let me know.
# 11  
Old 05-23-2013
Well, usually you can arrange to be parent to things you want to monitor. Basic sh, ksh, bash have wait, which wait for all unless you provide one or more pid. Man Page for wait (all Section 1) - The UNIX and Linux Forums Wait will set $? according to the exit of the waited child, so I like to make a pid list and use a for loop to wait for and report on each. After all, if you are waiting for all N, it does not matter which you sense first. If you want to get the exit time, you can create a wait on each in the background, and wait on that list.
Code:
date "+%Y-%m-%d %H:%M:%S ${0##*/) ($$) Start apps."
( # subshell only waits for subshell's children
for prog in prog1 prog2 prog3
do
( $prog  ; date '+%Y-%m-%d %H:%M:%S ${0##*/) ($$) Exit $! $prog." )&
done
wait
) >logfile 2>&1   # subshell also collects all stdout, stderr for log

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

File validation prior to continuing script

Hi Guys, I am trying to find a way within a bash script to check a file that exists in the same directory to ensure every line starts with 44 and is 12 digits long. If it doesn't then print some sort of text advising of the error and stop the script from going any further. If all lines... (1 Reply)
Discussion started by: mutley2202
1 Replies

2. Shell Programming and Scripting

Script using Wait

Hi, written a script which uses wait as follows Main.sh #!/usr/bin/ksh nohup scrpit1 1 & pid_1=$! nohup scrpit1 2 & pid_2=$! wait $pid_1 wait $pid_2 nohup scrpit1 3 & pid_1=$! nohup scrpit1 4 & (1 Reply)
Discussion started by: krux_rap
1 Replies

3. Programming

Wait status

hi all! In my C++ program I have a parent process which forks 5 children processes.The processes do a job and then they have to do some sort of sleeping(not terminate) until the parent wakes them up again.There might be 1,2,5 or even 0 processes awake at any moment.The thing is that in the... (9 Replies)
Discussion started by: vlm
9 Replies

4. Shell Programming and Scripting

calling a shell script in background and wait using "wait" in while loop

Hi, I am facing a strange issue, when i call a script from my while loop in background it doesnt go in background, despite the wait i put below the whil loop it goes forward even before the process put in background is completed. cat abc.txt | while read -u4 line do #if line contains #... (2 Replies)
Discussion started by: mihirvora16
2 Replies

5. Shell Programming and Scripting

Trapping exit and continuing

Hello I need to source a script. But that script terminates with a trailing exit. Which exits my script. I'm using bash, and this doesn't work: trap 'echo disabled' EXIT source other_file trap '' EXIT Instead, it calls my trap, but then exits anyway. I could get disgusting and... (4 Replies)
Discussion started by: brsett
4 Replies

6. Shell Programming and Scripting

check/wait for files to exist before continuing

I'm attempting to write a pretty simple script. It opens a Filemaker file successfully. That Filemaker file takes around 30-90 seconds to finish. When it's done, it writes a few .xml files into the same directory where my shell script and the Filemaker script reside. In my script, how can I... (2 Replies)
Discussion started by: alternapop
2 Replies

7. Shell Programming and Scripting

make sure logged in as userx before continuing script

i have a bash script and I want to add to the begining of the script to make sure that the script is being ran as you are logged in as a certain user (userx) before continuing to run the script....how? (2 Replies)
Discussion started by: ajp7701
2 Replies

8. Shell Programming and Scripting

wait command - cat it wait for not-chile process?

Did not use 'wait' yet. How I understand by now the wait works only for child processes, started background. Is there any other way to watch completion of any, not related process (at least, a process, owned by the same user?) I need to start a background process, witch will be waiting... (2 Replies)
Discussion started by: alex_5161
2 Replies

9. Shell Programming and Scripting

why isn't the exit status true?

the code: do } ] || mkdir -p ${mk_backup_dir} && echo "ERROR: release backup directory creation failed -${mk_backup_dir}" && exit done echo "INFO: Backup directories created" the result: mkdir: "/cm/uat_releases/riab/uat/2345": Permission denied ERROR: release backup directory... (5 Replies)
Discussion started by: mjays
5 Replies
Login or Register to Ask a Question