0

The code-snippet below checks whether the instance_status available or not. If it is available, wait for 15 seconds and if and echo the instance status.

I am getting the following error:

line 38: While: command not found

code:

While ["$INSTANCE_STATUS" -eq "available"] 
do
  wait 15
  if $INSTANCE_STATUS" -ne "available"
  then 
    echo "$SOURCE_INSTANCE_ID" is "$INSTANCE_STATUS"
  fi
done
MarcM
  • 2,173
  • 22
  • 32
farooq
  • 21
  • 4
  • 4
    You should have a look at http://shellcheck.net. – kvantour Mar 28 '19 at 15:10
  • 5
    try with `while` (lowercase) – nullPointer Mar 28 '19 at 15:11
  • 1
    ...though there are other bugs in here too (missing spaces around the `[`s, use of `-ne` for string rather than numeric comparisons, missing `test` operator for the comparison against "available", etc), that doesn't make the question fit within our guidelines -- to the contrary, having more than one bug per question makes it "too broad". – Charles Duffy Mar 28 '19 at 15:13
  • [Why should there be a space after `[` and before `]` in bash?](https://stackoverflow.com/questions/9581064/why-should-there-be-a-space-after-and-before-in-bash); [How to compare strings in bash?](https://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash) – Charles Duffy Mar 28 '19 at 15:14
  • 3
    Also note that `wait 15` doesn't wait 15 seconds, it waits for the process with ID 15 to exit; you probably want `sleep 15` instead. And even if the status of whatever program you're actually monitoring *does* change in that 15 seconds, you're still using the old value of `INSTANCE_STATUS`, not checking whether there's been an update in this code; thus, the loop is always endless, no matter whether the instance status changes or not. – Charles Duffy Mar 28 '19 at 15:15

1 Answers1

0

Many simple mistakes in your original code: While not while, missing spaces around [, missing double-quotes, comparision operators -eq and -ne are not for comparing strings...

Beware using wait, which is a valid shell command. But probably you meant sleep instead. Here details about the differences.

Try this:

while [ "$INSTANCE_STATUS" = "available" ] 
do
  sleep 15 # 'waits' 15 seconds.

  # ATTENTION: here you might need to 'refresh' $INSTANCE_STATUS value to avoid an endless loop.
  # Solution suggested by @Charles Duffy:
  # INSTANCE_STATUS=$(systemctl status "$SOURCE_INSTANCE_ID")

  if [ "$INSTANCE_STATUS" != "available" ]
  then 
    echo "$SOURCE_INSTANCE_ID is $INSTANCE_STATUS"
  fi
done

Edited: many fixes to original answer suggested ...forced ;) by @Charles Duffy. Thanks Charles, now I realize my shell script skills are getting pretty rusty.

MarcM
  • 2,173
  • 22
  • 32