0

I have below code

i=0
for s in / - \\ \| 
do 
  printf "\rWaiting for application start to finish $i $s"
  sleep 1
  ((i++))
  if [[ $i -gt 30 ]]
  then 
    break 
  fi
done

The loop always ends after 3 iterations. Any reason as why?

4
  • can you show the full output when you run it ? Commented Dec 8, 2021 at 13:17
  • 1
    It loops up to 3 (4 iterations) because the arguments in the for loop are 4: '/', '-', '\' and '|', isn't it? Commented Dec 8, 2021 at 13:19
  • Waiting for application start to finish 3 | Commented Dec 8, 2021 at 13:20
  • Yeah, I think that' it. I've added more arguments to the array and it loops more times. Not sure how to solve it though Commented Dec 8, 2021 at 13:20

2 Answers 2

1

This might be what you are looking for:

#!/bin/bash

a=('/' '-' '\' '|')
for ((i = 0; i < 30; ++i)); do
    printf '\rWaiting for application start to finish %d %s' \
            "$i" "${a[i%4]}"
    sleep 1
done
echo
Sign up to request clarification or add additional context in comments.

Comments

0

My mistake. The for loop which has three arguments is exiting instead of the if condition failing.

3 Comments

Why this has been posted as an answer?
@User123 Because it's the answer to the question?
The behavior was right and should not have been a question at all. Therefore the answer.

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.