Skip to content

Instantly share code, notes, and snippets.

@lsongdev
Last active April 29, 2016 03:19
Show Gist options
  • Save lsongdev/332b80f24e7f203987eff11fc6765bd8 to your computer and use it in GitHub Desktop.
Save lsongdev/332b80f24e7f203987eff11fc6765bd8 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# example:
#
# curl -s https://gist.githubusercontent.com/song940/332b80f24e7f203987eff11fc6765bd8/raw/progressbar.sh | sh
#
function ProgressBar(){
let max=$1
let done=$2
let left=$max-$done
let current=($done*100/$max*100)/100
local fill=$(printf "%${done}s")
local empty=$(printf "%${left}s")
printf "\r [${fill// /#}${empty// /-}] ${current}%%"
}
for i in {1..70}; do
ProgressBar 70 $i
sleep 0.1
done
printf "\nFinished!\n"
@wenshin
Copy link

wenshin commented Apr 28, 2016

始终在一行输出是什么原理?

@lsongdev
Copy link
Author

@wenshin 如果不输出换行符(\n)就是会在当前行输出,\r 可以回到当前行的开头。

@netwjx
Copy link

netwjx commented Apr 28, 2016

\r 回车符 可以回到行开头输出

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment