使用 Shell 脚本实现安装进度指示器

一、安装过程中使用旋转线来表示进度

本文原文链接: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

二、使用原点来表示进度

本文原文链接:https://blog.csdn.net/xzk9381/article/details/111314715

dots函数每隔一段时间打印一个圆点,这个时间值可以通过第一个参数传入,否则默认为5秒。在后台启动dots函数之后,通过"$!"获取dots的pid,然后开始执行耗时的工作,在工作执行完毕之后kill掉后台执行的dots。trap命令是为了防止用户Ctrl_C中断脚本执行的时候dots仍然在后台执行。

#!/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

猜你喜欢

转载自blog.csdn.net/xzk9381/article/details/111314715