0

I have a function within a bash script.

  closing )
  $CMD_POS open0stat
  status=$(cat /opt/cpu/hold/strstat)
  if [[ "${status}" = "C " ]]; then
    log "Precheck Closing:\tALLOWED\tStore Status '${status}'"
    return
  else
    err "Precheck Closing:\tDENIED\tStore Status '${status}'"
  fi
  ;;

There are some variables here but they are pretty self explanatory. Let me know if you need me to define anything.

Currently, if my script comes across an 'err' it exits, and cleans. I'm wanting to change that, so it continuously checks for that status to = C, until it does, and then move on.

How can i loop function, until the condition is met?

T_Wrong
  • 59
  • 5
  • 1
    You can use an `until` loop. [See this question and first answer](https://unix.stackexchange.com/questions/168385/can-you-put-an-if-statement-within-an-until-loop) – JNevill Jan 14 '20 at 18:26
  • So reading that, using my function above, can i just change the "if" to an "until", and remove the else? That should loop "until $status = C", and once it is C, logs my tALLOWED, and moves on? – T_Wrong Jan 14 '20 at 18:34
  • 1
    Pretty much that's the idea. You just want it to loop until that condition is TRUE. – JNevill Jan 14 '20 at 18:36
  • Got it thanks. I haven't used 'until'. Do i still need to end it with 'fi', or just stop at 'return'? – T_Wrong Jan 14 '20 at 18:37
  • 1
    Instead of `if;then;fi` you'll use `until;do;done`. You can see in that answer how they've changed that block from `if` to `until`. – JNevill Jan 14 '20 at 18:41
  • 1
    Be sure to reload the file each time through the loop. If you set `status` before the loop and just keep checking it, it'll never change and the loop will run forever. It's probably simplest to skip the variable and just use `until [[ "$(cat /opt/cpu/hold/strstat)" = "C " ]]; do ...` – Gordon Davisson Jan 14 '20 at 19:51
  • Thank you so much! I was just running into that haha – T_Wrong Jan 14 '20 at 20:38

0 Answers0