3

I have a strange behavior in my script bash. when the while condition is true the script behaves correctly but if it's false the commands after the loop aren't executed at all and the script stops. There is no break in my commands after the loop. I cannot see where is the problem! Any help is welcome :) Thanks in advance.

while  [ expression1 ] || [ expression2 ]  
do
            echo in the loop
            if  [ expression3 ] && [ expression4 ] ;
            then
                    commands..   
                    break;
            fi
            commands..
done
commands..
echo out from the loop

Real code:

start_t=`grep Start_t $job_template | awk -F= '{print $2}'`
current_date=`date +%s`
progress_t=`expr $current_date - $start_t`
exec_t=`grep Exec_t $job_template | awk -F= '{print $2}'`

running_state="r"
req_state $job_id # get the state 
xml_state=` grep "job_id=$job_id" $list_job_file | awk '{print $4}'`
while  [ $state = $running_state ] || [ $xml_state = "stoped" ]  
    do
            echo in the loop
            if  [ "$xml_state" = "running" ] && [ $progress_t  -gt $exec_t ] ;
            then
                    kill_job $job_id
                    update_status $job_template "killed"
                    echo The job is killed    
                    break;
            fi

            sleep $sleeping_t

            $req_state  $job_id # to update the state
            echo state $state
            xml_state=` grep "job_id=$job_id" $list_job_file | awk '{print $4}' `
            echo xml_state $xml_state
            start_t=`grep Start_t $job_template | awk -F= '{print $2}'`
            current_date=`date +%s`
            progress_t=`expr $current_date - $start_t`
    done
echo out from the loop
commands..
9
  • If you do echo "hi" immediately after the done gets printed? Commented Oct 10, 2012 at 9:34
  • 1
    You should show us your real script, not a pseudocode. There may be something important missing here. Commented Oct 10, 2012 at 9:37
  • check you script once more. script, which repeats your pseudocode definitely works as expected paste.ubuntu.com/1270794 Commented Oct 10, 2012 at 9:40
  • @ KingsIndian: no it isn't printed. Commented Oct 10, 2012 at 9:43
  • 4
    I suspect that bash encounters an error and terminates, probably with an error message. Run your script like this bash -x <scriptname> to trace it. I suspect that xml_state in the loop is eventually being set to an empty string or only spaces, and so the [ $xml_state = "stoped" ] expression becomes invalid. Better to put quotes around $xml_state. Commented Oct 10, 2012 at 10:26

1 Answer 1

2

There are many mistakes in this script :

  • state is not initialised
  • as test are done with single right bracket, the variables should be double quoted to avoid shell expansion
  • seems req_state is a function or a command, so there must not be preceded by a $
  • useless use of grep with awk : grep Start_t $job_template | awk -F= '{print $2}' and awk -F= '/Start_t/{print $2}' $job_template will do the same thing.
Sign up to request clarification or add additional context in comments.

1 Comment

"there must be a semicolon ; before in-line comments " -- really?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.