Continue problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Continue problem
# 1  
Old 06-14-2012
Continue problem

Hey all. First-time poster, long-time reader

I'm on Mac, and I've written a long script to open up a maximum of 20 Terminal windows and run a subscript with a different input in each of them. When each of these sub-scripts finishes, it changes the value of a variable ("$windows") by -1, which lets the main script know that it can open a new window. It's basically a brute force approach to parallel processing on Mac.

The problem is, when it reaches the max number of windows, it doesn't wait for the "$windows" variable to go down before moving to the next input. It just does the check and then moves on to the next input. For example, if I'm running a script called "subscript" on 50 files, it'll run the first 20, then spit out "Waiting for a window to free up" 30 times before exiting.

Can anyone tell me why it's not working the way I want it to? There is a piece of Applescript in there, btw. Here is the relevant excerpt from my script:


Code:
for input in $inputlist
do
	if [ $windows -lt $maxwindows ]	
	then
	    windows=`cat ${mypwd}Windows.txt`
	    windows=`expr $windows + 1`
	    echo $windows > ${mypwd}Windows.txt
	    echo $var | pbcopy
	    osascript -e 'tell app "Terminal" 
do script "subscript `pbpaste`
end tell'
	    shift
	    sleep .1
	    continue
	else 
	    windows=`cat ${mypwd}Windows.txt`
	    echo $windows
	    echo "Waiting for a window to free up"
	    sleep .4
	    windows=`cat ${mypwd}Windows.txt`
	    continue
	fi
    done
done


Last edited by methyl; 06-14-2012 at 09:23 PM.. Reason: please use code tags
# 2  
Old 06-15-2012
How about this (simulated your osascript with random sleep).

Things strange in your script:
1. shift command does nothing usefull
2. extra done one end
3. should refresh windows at top of loop to trap finished sub-processes

Code:
inputlist=$(seq 1 50)
mypwd=./
maxwindows=20
windows=0
echo 0 > ${mypwd}Windows.txt
for input in $inputlist
do
    windows=`cat ${mypwd}Windows.txt`
    [ $windows -ge $maxwindows ]  && echo "Waiting for a window to free up"
    while [ $windows -ge $maxwindows ]
    do
        sleep .8
        windows=`cat ${mypwd}Windows.txt`
    done
 
    windows=`cat ${mypwd}Windows.txt`
    windows=`expr $windows + 1`
    echo $windows > ${mypwd}Windows.txt
    echo "Starting window $windows"
    (   sleep $(( 9 + (  RANDOM / 8192 ) ))
        lwin=`cat ${mypwd}Windows.txt`
        lwin=`expr $lwin - 1`
        echo $lwin > ${mypwd}Windows.txt
    ) &
    sleep .1
done


Last edited by Chubler_XL; 06-15-2012 at 01:14 AM..
# 3  
Old 06-15-2012
Thanks, Chubler. That works perfectly. I'm new to shell scripting (started grad school a year ago and have been teaching myself shell scripting to do brain image analysis). About your notes:

1) I got this script working so that it took the inputs in the Terminal window (you'd have to type in "Parallel 1 2 3 4 5..." into Terminal), but it only worked with the first shift command, for some reason. The second one was so that I didn't have thousands of "Waiting to open window" notices, but you solved that problem.

2) Yeah, that "done" was from a for loop I have embedded earlier in the script.

3) That makes perfect sense.

One thing I don't understand about your changes, though, is why [ $windows -ge $maxwindows ] is in there twice. Does the first one need to be there for the while loop to run?

Coming up with the script I had so far had been a great learning experience, but I learned more from understanding the changes you made than I have in weeks of reading message boards and guides. Thanks a million.
# 4  
Old 06-15-2012
Quote:
Originally Posted by Diabadass
One thing I don't understand about your changes, though, is why [ $windows -ge $maxwindows ] is in there twice. Does the first one need to be there for the while loop to run?
The first one has nothing to do with the loop, it's a statement all by itself.

Code:
[ $windows -ge $maxwindows ]  && echo "Waiting for a window to free up"

If $windows is greater or equal to $maxwindows, it prints the message. && is short form for 'if the first succeeds, run the second', very useful for short things where you don't need to bother with an entire code block.

Try these statements for instance:
Code:
true && echo "This will always print"
false && echo "But this never will"

true || echo "This shouldn't print"
false || echo "But this should"

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 06-15-2012
Just fooled around with those statements. Interesting. It sounds almost like an "if... then" test.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case statement - continue

I have a case statement. IS "continue" working in case? for file in ls dir/* case $file in a) do something continue ;; b) do something continue ;; esac It is a Bourne shell (13 Replies)
Discussion started by: digioleg54
13 Replies

2. Shell Programming and Scripting

Continue the loop if the value is not found

Dear Help, Is it possible to continue the loop by going to the next available value, if the 'expected value' is not found. I have a list of values which might not get incremented by fixed value and hence the loop could break and the script could terminate. Any suggestion is appreciated. ... (1 Reply)
Discussion started by: Indra2011
1 Replies

3. Shell Programming and Scripting

If else continue to check value until it it is right

i have script which get Input via READ value and compare it from file. when found do some stuff...if not found again ask for Input until you dont enter Right value. #!/bin/ksh echo "SID must be in oratab file" echo "Enter ORACLE_SID of Database:\c " read ORACLE_SID x=`cat /etc/oratab|... (3 Replies)
Discussion started by: tapia
3 Replies

4. Shell Programming and Scripting

Break vs Continue

Okay so I am having trouble understand what the computer will do with a code like this if ; then echo echo "Found the file" blah blah blah for i in `blah blah blah` ; do ... (2 Replies)
Discussion started by: shade917
2 Replies

5. Shell Programming and Scripting

call b.sh from a.sh, and continue to a.sh after use exit 0 from b.sh

Hi, I have two sh file. a.sh and b.sh b.sh is command and used by other sh's. I want to add below line to b.sh. When it is done with b.sh I want to continue to process a.sh. But when I use exit 0 in b.sh it is exit from b.sh and a.sh How can I make it to continue to process? a.sh ... (2 Replies)
Discussion started by: ctuncer
2 Replies

6. Shell Programming and Scripting

Restart and then continue script

How can I get a script to complete a update, varifiy completion, resboot, and continue with script? Is it possbile to get script to add itself to the "startup application" list #!/bin/bash clear sudo apt-get update #Verify/test the update completed #Reboot #Start/comtinue... (9 Replies)
Discussion started by: wolfgangcs
9 Replies

7. Shell Programming and Scripting

Are you sure you want to continue connecting (yes/no) need a way to pass in the value yes without

Are you sure you want to continue connecting (yes/no) need a way to pass in the value yes without use the except command. I am creating a script to send down files to an application servers every time it reboots as it picks up the newest image. I do not want to manual connect to each server... (1 Reply)
Discussion started by: 3junior
1 Replies

8. Shell Programming and Scripting

awk continue

I want to print entire row of file awk '{print $0}' inputfile but sometime before every row have space characters. Example: " HVLR is not in service on AP 54" How can i print entire row without space characters ? thanks (3 Replies)
Discussion started by: anhtt
3 Replies

9. Shell Programming and Scripting

continue example

The following code for search a pattern in file name (or entire file name) and look at its size, code is derived from an ebook about scripting. It is working in HP Unix but I am unable to run in Linux (Ubuntu) Please advise me what is wrong for Linux? And besides , how can I get rid of errors in... (7 Replies)
Discussion started by: xramm
7 Replies

10. UNIX for Advanced & Expert Users

Continue an 'scp' tranfser?

I was in the middle of transferring a file over a slow link (768k) using 'scp'. The system on this end got rebooted by an overzealous co-worker... I've got 500+ megs of the file here and 725 megs to go. I'd really hate to start over. I fear that the answer to my question is that I will have to... (3 Replies)
Discussion started by: deckard
3 Replies
Login or Register to Ask a Question