Visit Our UNIX and Linux User Community


Breaking Loop by using another script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Breaking Loop by using another script
# 1  
Old 07-28-2009
Breaking Loop by using another script

Hi friends,
I have 2 scripts. 1) Master_Script.sh and 2) Sub_script.sh
We run Master_script.sh manually where as sub_script.sh keeps generating output in every 2 minutes (through crontab). The output generated by sub_script.sh can be 0 or 1.

As I told you, sub-script.sh keeps generating o/p in every 2 minutes, as soon as output of sub_script.sh ($sub_script_o/p) is 0 then somehow Master_script.sh should jump directly to "done" leving whatever it is doing in "for" loop.



# Master_script.sh
aa
bb
cc
dd
ee
for
ff
gg
hh
sh ii.sh #while this script is running if $sub_script_o/p is 1 then let ii.sh run but if $sub_script_o/p is 0 then directly jump to "done" so that loop will break and next item in loop will be executed.
jj
kk
done




# sub-script.sh
a
b
c
d
e
f
#it will generate output either 0 or 1 in variable $sub_script_o/p

Situation is such that Master_script.sh runs sh ii.sh in "for loop" as mentioned in my question. ii.sh gets terminated because of some error (Output of sub_script.sh i.e. $sub_script indicates whether ii.sh is running or not. 1=running, 0=terminated) and hence the loop gets stuck in between.

To solve this problem I am planning to write sub_script.sh which will make Master_script.sh to go to next iteration when ii.sh is stuck in between. This will help me keep running the Master_script.sh even if it faces some problem with a particular iteration.


Please advice.
Thank you for taking interest
Anushree

Last edited by anushree.a; 07-30-2009 at 03:19 AM..
# 2  
Old 07-28-2009
Code:
for var in something
do
 something
 something 1
 if [ "$sub_script_o/p" -eq "0" ]; then
  break
 else
  sh ii.sh
  do some more stuffs..
 fi
done

# 3  
Old 07-28-2009
Hi Anchal,
Thank you for the reply.

But how this script will make master_script.sh to jump to "done"?
# 4  
Old 07-28-2009
what do you mean by jumping to done? do you want to exit the loop or go to the next iteration? if you want to go to next iteration use "continue" instead of break in the code provided by anchal or you can try this as well.

Code:
while((1)); do
ps -ef|grep Sub_script.sh >/dev/null
[[ $? -eq 0 ]] && break 
done

# 5  
Old 07-28-2009
One way is to use the exit status of the sub script.

Code:
Within the master_script.sh loop, call sub_script.sh and test the reply:

sub_script.sh ; REPLY=*?
if [ ${REPLY} -eq 0 ]
then
       break
fi


At the end of sub_script.sh:

# Output value 0 = stop master_script.sh
# Output value 1 = keep going.
exit ${sub_script_o/p}

# 6  
Old 07-29-2009
Thank you Methyl, Ahmed, Anchal for your reply.
But the situation is such that Master_script.sh runs sh ii.sh in "for loop" as mentioned in my question. ii.sh gets terminated because of some error (Output of sub_script.sh i.e. $sub_script indicates whether ii.sh is running or not. 1=running, 0=terminated) and hence the loop gets stuck in between.

To solve this problem I am planning to write sub_script.sh which will make Master_script.sh to go to next iteration when ii.sh is stuck in between. This will help me keep running the Master_script.sh even if it faces some problem with a particular iteration.

I am sorry if I am confusing but to understand my problem better please read my original question and this suppliment.

Thank you to take pains.
Waiting for your reply

Anushree
# 7  
Old 07-30-2009
Can somebody please guide me.

Previous Thread | Next Thread
Test Your Knowledge in Computers #534
Difficulty: Medium
All programming languages have an explicit Boolean type.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop breaking when using "ssh" command inside

Hi , I am trying to read a list of hosts from a config file and trying to get file list from that host. For this I have used one while loop. cat "$ARCHIVE_CFG_FILE" | sed '/^$/d' | sed '/^#/d' | while read ARCHIVE_CFG do SOURCE_SERVER_NAME=`echo "$ARCHIVE_CFG" | awk -F '|' '{ print... (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

2. Shell Programming and Scripting

Ssh to remote server loop is breaking

hi All, cat login.list server1 userid1 server2 userid2 server3 userid3 ---------------------------------------- #SSHSCRIPT.ksh FILE=login.list while read vah vah_id do ssh $vah -l $vah_id "pwd" done < "$FILE" ----------------------------------------- When i... (2 Replies)
Discussion started by: raghur77
2 Replies

3. Shell Programming and Scripting

breaking for loop

Dear Friends, Here I need your guidance once again. I have for loop which check all files in a folder for a particular string. If the string is found in a file it returns value other than 0 else returns 0 value in variable t2. At times the string which we are looking for is in first file... (1 Reply)
Discussion started by: anushree.a
1 Replies

4. Shell Programming and Scripting

Breaking out of loop

I have a main script with while loop having for loop inside. Again in for loop based on if condition few functions will be called. So when a function is called for certain condition it should come out from the main for loop and should continue with while loop. Let me explain with example here: I... (6 Replies)
Discussion started by: vpv0002
6 Replies

5. Shell Programming and Scripting

How to loop use while loop in csh script?

Hi all, i got 2 text file. file.txt value.txt i want use C shell script to write out while both of the file got different limit....how i going to write it in 1 while loop? (4 Replies)
Discussion started by: proghack
4 Replies

6. Shell Programming and Scripting

Stop a program without breaking the script?

Hello there! I am creating a script, that among other functions will run iperf (network stress tool) either on server or client mode. The thing is that it will run forever if the user does not stop it or break it. CTRL-C will break it. I need a method, however, that will break iperf and then... (2 Replies)
Discussion started by: ppucci
2 Replies

7. SCO

Restrict user from breaking out of login script

I have a login script similar to where the user's shell in /etc/passwd is set to csh: echo "In .login" setenv SHELL /bin/csh /bin/sh -c trap "" 1 2 3 5 6 7 8 15 /path/to/application logout --------------------- however, even with the trap command the user is still able to... (1 Reply)
Discussion started by: kuliksco
1 Replies

8. Shell Programming and Scripting

Breaking if-else loop and variety of comparisions

Hello Friends, Im trying to write a script to invoke nagios. In order to do this I grep some words that comes from output of some backup scripts. When there is "End-of-tape detected" in directed output logs it should give alarm. First I would like to know if there is any better way to write... (5 Replies)
Discussion started by: EAGL€
5 Replies

9. Linux

breaking out of while loop

Hi , I am running this script ( pasting only error code ) to generate some ddl definition for tables . But what I want is to break out of the db2look part when the base_table is not like DIM_$TN or FACT_$TN . After this it should come back to while loop to read the next TN . I read the other... (3 Replies)
Discussion started by: capri_drm
3 Replies

10. Shell Programming and Scripting

rsh breaking me out of loop

Hey all I have two scripts, one script containing the guts of my code. The other simply loops through a list, calling the other script on each iteration. Problem is when I add the line `/usr/bin/rsh -l root $HOSTNAME ""` to my main script, the loop never seems to exectute any more... (1 Reply)
Discussion started by: mark007
1 Replies

Featured Tech Videos