[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 $?

 

 

 

 

 

 

 

 

Advertisements

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s