Linux - 进度条

Linux - 进度条

bash脚本

脚本一

#!/bin/bash -f
# Filename: process_bar_with_printf.sh
# Date: 24/08/2021

i=0
bar=
index=0
arr=( "|" "/" "-" "\\" )

while [ $i -le 100 ]
do
	let index=index%4
	printf "[%-100s][%d%%][\e[43;46;1m%c\e[0m]\r" "$bar" "$i" "${arr[$index]}"
	let i++
	let index++
	sleep 0.1
	bar+="#"
done
printf "\n"

脚本二

#!/bin/bash -f
# Filename: process_bar_with_whiptail.sh
# Date: 24/08/2021

for i in $(seq 1 100)
do
	sleep 0.1
	echo $i
done | whiptail --title 'Process Bar' --gauge 'Running...' 6 60 0

脚本三

#!/bin/bash -f
# Filename: process_bar_with_pv.sh
# Date: 24/08/2021

head -c 1G < /dev/urandom | pv -s 1G > /dev/null

脚本四

#!/bin/bash -f
# Filename: process_bar_with_zenity.sh
# Date: 24/08/2021

(echo 10; sleep 2; echo 20; sleep 2; echo 50; sleep 2) | zenity --progress

脚本五

thesre@HP-Z420-Workstation:~$ cat process_bar_percent.sh 
#!/bin/bash

# Show a progress bar at step number $1 (from 0 to 100)


function is_int() { test "$@" -eq "$@" 2> /dev/null; } 

# Parameter 1 must be integer
if ! is_int "$1" ; then
   echo "Not an integer: ${1}"
   exit 1
fi

# Parameter 1 must be >= 0 and <= 100
if [ "$1" -ge 0 ] && [ "$1" -le 100 ]  2>/dev/null
then
    :
else
    echo bad volume: ${1}
    exit 1
fi

# Main function designed for quickly copying to another program 
Main () {

    Bar=""                      # Progress Bar / Volume level
    Len=25                      # Length of Progress Bar / Volume level
    Div=4                       # Divisor into Volume for # of blocks
    Fill="▒"                    # Fill up to $Len
    Arr=( "▉" "▎" "▌" "▊" )     # UTF-8 left blocks: 7/8, 1/4, 1/2, 3/4

    FullBlock=$((${1} / Div))   # Number of full blocks
    PartBlock=$((${1} % Div))   # Size of partial block (array index)

    while [[ $FullBlock -gt 0 ]]; do
        Bar="$Bar${Arr[0]}"     # Add 1 full block into Progress Bar
        (( FullBlock-- ))       # Decrement full blocks counter
    done

    # If remainder zero no partial block, else append character from array
    if [[ $PartBlock -gt 0 ]]; then
        Bar="$Bar${Arr[$PartBlock]}"
    fi

    while [[ "${#Bar}" -lt "$Len" ]]; do
        Bar="$Bar$Fill"         # Pad Progress Bar with fill character
    done

    echo Volume: "$1 $Bar"
    exit 0                      # Remove this line when copying into program
} # Main

Main "$@"

thesre@HP-Z420-Workstation:~$ cat test_process_bar.sh 
#!/bin/bash

# test_progress_bar3

Main () {

    tput civis                              # Turn off cursor
    for ((i=0; i<=100; i++)); do
        CurrLevel=$(./process_bar_percent.sh "$i")   # Generate progress bar 0 to 100
        echo -ne "$CurrLevel"\\r            # Reprint overtop same line
        sleep .04
    done
    echo -e \\n                             # Advance line to keep last progress
    echo "$0 Done"
    tput cnorm                              # Turn cursor back on
} # Main

Main "$@"

参考资料

https://askubuntu.com/questions/747143/create-a-progress-bar-in-bash

你可能感兴趣的:(Unix,Like,linux)