Skip to main content

顯示進度 progress

範例:簡易版
echo -ne '>>>                       [20%]\r'
# some task
sleep 2
echo -ne '>>>>>>>                   [40%]\r'
# some task
sleep 2
echo -ne '>>>>>>>>>>>>>>            [60%]\r'
# some task
sleep 2
echo -ne '>>>>>>>>>>>>>>>>>>>>>>>   [80%]\r'
# some task
sleep 2
echo -ne '>>>>>>>>>>>>>>>>>>>>>>>>>>[100%]\r'
echo -ne '\n'
範例:顯示進度條
#!/bin/bash
#
# inprogress.sh
# Description: this script may show some simple motion in foreground
# when main program in progress.
# Author: A-Lang (alang[dot]hsu[at]gmail[dot]com)

trap "kill $BG_PID;echo;exit" 1 2 3 15

function waiting
{
    INTERVAL=0.5
    TT='->——
    –>—–
    —>—-
    —->—
    —–>–
    ——>-
    ——->
    ——<-
    —–<–
    —-<—
    —<—-
    –<—–
    -<——
    <——-';
    echo -ne "Please waiting… \n"
    while true
    do
        for j in $TT
        do
            echo -ne "Please be patient, $j\r"
            sleep $INTERVAL
        done

    done
}

# Start the waiting, where place your main program.
waiting &
BG_PID=$!

sleep 10

# End of the waiting
kill $BG_PID
echo
範例:旋轉符號
#!/usr/bin/env bash

spinner()
{
    local pid=$!
    local delay=delay=${1:1}
    local spinstr='|/-\'
    while [ "$(ps a | awk '{print $1}' | grep $pid)" ]
    do
        local temp=${spinstr#?}
        printf " [%c] " "$spinstr"
        local spinstr=$temp${spinstr%"$temp"}
        sleep $delay
        printf "\b\b\b\b\b"
    done
    printf "    \b\b\b\b"
}

# Run your program here
#(a_long_running_task) &
sleep 10 &
spinner 0.75
範例:整合 LED 的顯示
#!/bin/bash

echo "Starting to format the disk...."

# Start to blink the LED
/root/bin/led.sh b
/root/bin/led.sh blink &
led_pid=$!

# Main codes
echo "formating hdisk2..."
sleep 10

# When the main codes is ending up
# Stop to blink the LED
kill -9 $led_pid
wait $led_pid 2>/dev/null
sleep 2

# Making sure the LED is ON with Blue
/root/bin/led.sh b
echo "done"

led.sh 用來控制 LED 的閃燈

wait .... 這行用來隱藏 killed ... 的文字訊息