which loop in bash script

153 Views Asked by At

I am quite new in bash, but I need to create a simple script which will do below steps:

Wait 1 minute

A) bash script will use CM to generate result file

B) check row 8 in result file (to know if Administrator is running any jobs or not)

if NO jobs:

C) bash script will use CM to start cube refresh

D) wait 1 minute

D1) Remove result file

E) generate result file

E1) Read row 8

no jobs: F) remove result file G) EXIT

yes:

I) Go to D)

YES:

E) Wait 1 minute

F) Remove result file

Go to A)

As bash doesn't have goto (or should not be use), I tried few loops, but I not sure which I should choose.

I know how to: - start cube(step C) - generate result file (step A & E): - check line 8: sed '8!d' /abc_uat/cmlogs/adm_jobs_u1.log condition for loops will be probably similar to this: !='Owner = Administrator'

but how to avoid goto ?

I tried with while do loop, but I am not sure what should I add in case of false condition, I added else, but not sure of it:

sleep 60
Generate result file with admin jobs (which admin runs inside of 3rd party tool)

while [ sed '8!d' admin_jobs_result_file.log !="Owner = Administrator" ];
 do
--NO Admin jobs
    START CUBE REFRESH (it will start admin job)
    sleep 60
    REMOVE RESULT FILE (OLD)
    GENERATE RESULT FILE
    while [ sed '8!d' admin_jobs_result_file.log = "Owner = Administrator" ];
 --Admin is still running cube refresh
    do 
    sleep 60    
    REMOVE RESULT FILE (OLD)
    GENERATE RESULT FILE
-- it should continue checking every 1 minute if admin is still running cube refresh job, so I hope it will go back to while condition
    else
    done
else
-- Admin is running something
sleep 60    
REMOVE RESULT FILE (OLD)
GENERATE RESULT FILE
-it should check result file again but I think it will finish loop
done 
3

There are 3 best solutions below

0
On

You can replace goto with a loop. while loop, for example.

Syntax

while <condition>
do
    action
done
2
On

Check out cron jobs. Delegate, if possible, "waiting for a minute" task to cron. Cron should worry about running your script on a timely fashion.

You may consider writing two scripts instead of one.

Do you really need to create a result file? Do you know piping ? (no offense, just mentioning it because you said you were fairly new to bash)

4
On

Hopefully this is self explanatory.

result_file=admin_jobs_result_file.log

function generate {
    logmsg sleeping
    sleep 60
    rm -f "$result_file"
    logmsg generating
    # use CM to generate result file 
}

function owner_is_administrator {
    # if line 8 contains "Owner = Administrator", exit success
    # else exit failure
    sed -n '8 {/Owner = Administrator/ q 0; q 1}' "$result_file"
}

function logmsg { date "+%Y-%m-%d %T -- $*"; }

##############
generate

while owner_is_administrator; do
    generate
done

# at this point, line 8 does NOT contain "Owner = Administrator"

logmsg start cube refresh
# use CM to start cube refresh

generate

while owner_is_administrator; do
    generate
done

logmsg Done

Looks like AIX's sed can't exit with a specified status. Try this instead:

function owner_is_administrator {
    # if line 8 contains "Owner = Administrator", exit success
    # else exit failure
    awk 'NR == 8 {if (/Owner = Administrator/) {exit 0} else {exit 1}}' "$result_file"
}