Home > Back-end >  loop in bash does not perform integer comparison properly
loop in bash does not perform integer comparison properly

Time:12-09

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?

CodePudding user response:

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

CodePudding user response:

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
  •  Tags:  
  • bash
  • Related