Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I really like the look of the output of bar

But that script is only used for Input/Output operations.

How can I use it as a replacement for sleep but with visual feedback?

sleep 10
share|improve this question

2 Answers 2

I couldn't see a way to do this using bar. However you might be interested in these other commands that do the same thing, that might suite your needs better.

Method #1 - Fake it

This method will simply overwrite what's been previously displayed to the screen with a larger progress bar. Simple but effective.

Example, ex.bash:

#!/bin/bash

echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
echo -ne '#######################   (100%)\r'
echo -ne '\n'

Method #2 - pipeview

The command pv provides progress bar functionality. You can see more elaborate examples in this article titled: A Unix Utility You Should Know About: Pipe Viewer.

$ pv access.log | gzip > access.log.gz
611MB 0:00:11 [58.3MB/s] [=>      ] 15% ETA 0:00:59

Method #3 - spinner

You can use the following sample code to construct your own "spinner" using a simple loop. Within the loop you could embed whatever commands you like. This code comes from this article, titled: Can I do a spinner in Bash?.

#!/bin/bash

sp='/-\|'
printf ' '
for i in $(seq 3); do
  printf '\b%.1s' "$sp"
  sp=${sp#?}${sp%???}
  sleep 1
done
echo ''

Method #4 - dialog

There's a command called dialog which does pretty much what you want. It makes use of graphical dialogs, but they're ncurses based so they'll work in most every terminal and/or script so long as there's support for ncurses. You can see all the dialog documentation on the project's website.

screenshot

ss of guage

References

share|improve this answer
    
About slm's the method #1, a more complete example: progressbar.sh. –  Alexandre Apr 19 at 13:37

It seems trivial to implement. The bash function below (bar) below behaves like your bar script (based on the short screencast). It also resizes dynamically with the terminal width (on the next call to bar).

#!/bin/bash

#Helper functions
terminal_width(){
  local width_height=`stty size`
  echo ${width_height/* /}
}
string_times_n(){
  local s=$1
  local n=$2
  for((i=0;i<n; i++)); do echo -n "$s"; done
}
##The actual function
bar(){
  local percentage=$1
  local padding=10
  local width=$(echo "scale=0; 0.5 * $(terminal_width)" | bc | cut -d. -f1)
  local equals_n=$(echo "$percentage * $width / 100" | bc | cut -d. -f1)
  local dots_n=$((width - equals_n))

  #ANSI escape sequence magic
  local Esc="\033["
  local up="$Esc""K""$Esc""1A""$Esc""K"

  #Clear the line
  string_times_n ' ' "$width"
  echo -ne "\r"

  #Print the current screen
  printf  "%3s%% [" "$percentage"
    string_times_n '=' "$equals_n"
    string_times_n '.' "$dots_n"
  echo -n "]"

  #Go up unless finished
  if [[ "$percentage" == 100 ]] 
  then
    echo
  else
    echo -e "$up"
  fi
}

Usage

. bar.sh #Assuming it's saved in bar.sh
bar $percentage

Regular progress example:

for i in {1..10}; do bar $((i*10)); sleep 0.1; done;

Random progress example:

for i in {1..10}; do bar $((i*10)); sleep `echo $RANDOM / 10000|bc`; done;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.