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
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.
One way is to use the exit status of the sub script.
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}
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.
I think the sub-script.sh runs independently where as master_script.sh has dependency on the exit status of sub_script.sh. If this the case then you can not check the exit status of an independent script from the master script where as you can create a temp_file whener sub_script has return status 1and from master script you can chen if the file has been created then execute the ii.sh and remove the temp file. Keep this thing in the while loop. I ma not sure how it help full to you.