Use Shell script to implement installation progress indicator

1. Use a rotating line to indicate progress during the installation process

Link to the original text of this article: https://blog.csdn.net/xzk9381/article/details/111314715

#!/bin/bash

function KILLPROC(){
    
    
	echo $1 | xargs kill -9 &> /dev/null
}

function PROC_NAME(){
    
    
    printf "%-45s" ${1}
    tput sc
    while true
    do
        for ROATE in '-' "\\" '|' '/'
        do
            tput rc && tput ed
            printf "\033[1;36m%-s\033[0m" ${ROATE}
            sleep 0.5
        done
    done
}

function CHECK_STATUS(){
    
    
    if [ $? == 0 ];then
        KILLPROC ${1} &> /dev/null
        tput rc && tput ed
        printf "\033[1;36m%-7s\033[0m\n" 'SUCCESS'
    else
        KILLPROC ${1} &> /dev/null
        tput rc && tput ed
        printf "\033[1;31m%-7s\033[0m\n" 'FAILED'
    fi
}

function NGINX_INSTALL(){
    
    
    PROC_NAME Nginx_Service &
    PROC_PID=$!

    apt-get install nginx -y &> /dev/null
    CHECK_STATUS ${PROC_PID}
}

NGINX_INSTALL

Second, use the origin to indicate progress

Link to the original text of this article: https://blog.csdn.net/xzk9381/article/details/111314715

The dots function prints a dot at regular intervals. This time value can be passed in through the first parameter, otherwise the default is 5 seconds. After starting the dots function in the background, get the pid of dots through "$!", and then start to perform the time-consuming work, and kill the dots executed in the background after the work is completed. The trap command is to prevent dots from being executed in the background when the user Ctrl_C interrupts the script execution.

#!/bin/bash
function dots(){
    
    
    seconds=${1:-5} # print a dot every 5 seconds by default
    while true
    do
        sleep $seconds
        echo -n '.'
    done
}
 
dots 10 &
BG_PID=$!
trap "kill -9 $BG_PID" INT
 
# Do the real job here
sleep 150
kill $BG_PID
echo

Guess you like

Origin blog.csdn.net/xzk9381/article/details/111314715