1
\$\begingroup\$

I'm looking to improve it in brevity, readability, and simplicity. Basically, I'm just looking for a more elegant solution. What improvements can I make?

i=0
while true; do
 OUTPUT="$(docker inspect --format='{{json .State.Health.Status}}' su-apache)"
 if [ $OUTPUT = '"healthy"' ]; then
 echo
 break
 fi
 echo -en "\r033円[KWaiting $i seconds for su-apache to finish starting... Current health: $OUTPUT"
 let "i++"
 sleep 1
done
echo 'Environment started successfully.'
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Sep 13, 2016 at 18:29
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

If you don't mind using Bash, then it will be more elegant to replace the while loop with a counting for loop:

for ((i = 0; ; i++)); do
 # ...
done

I'm guessing that you're using Bash anyway from the let "i++" syntax, which will not work with simpler shells like Dash.

Note that the various flags of echo are not portable. For a more portable solution, replace it with printf.

Lastly, the indentation is inconsistent: some blocks are indented with 4 spaces, others with 2. It would be better to indent consistently (I like 4 spaces).

answered Sep 15, 2016 at 20:22
\$\endgroup\$
2
  • \$\begingroup\$ Thanks for this. It really helped a lot! How are things normally done here on CodeReview? How long do people usually wait before accepting an answer? Should I edit my revised code into my question? \$\endgroup\$ Commented Sep 15, 2016 at 21:17
  • \$\begingroup\$ Hi, and welcome! I don't recall a rule for this. A few days on average is usually about right, I guess. We don't edit code in the question, as that would invalidate answers. See this page in the help center for more details, and enjoy the site! \$\endgroup\$ Commented Sep 15, 2016 at 21:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.