[Shell] – How to create a spinner with shell script? How to create a spinner which will be finished and do not will output kill termination?

Following an example to do it.

function spinner() {
  case $1 in
    start)
      let column=$(tput cols)-${#2}-8
      echo -ne ${2}
      printf "%${column}s"
      local i sp n
      sp='/-\|'
      n=${#sp}
      while sleep 0.1; do
        printf "%s\b" "${sp:i++%n:1}"
      done
      ;;
    stop)
      if [[ -z ${3} ]]; then
        exit 1
      fi
      echo -e "\n"
      kill $3 > /dev/null 2>&1
    esac
}

function spinnerStart {
  spinner "start" "${1}" &
  pid=$!
  disown
}

function spinnerStop {
  spinner "stop" $1 $pid
  unset pid
}

Following an example about how to use the above spinner.

spinnerStart 'Running my command ...'
command <mySlowCommand> &>/dev/null
spinnerStop $?

 

 

 

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s