The shell scripts a program installation with a progress bar

need

Write a software installation script using the shell, with a progress bar

example

#!/bin/bash

# 模拟软件安装的步骤列表
steps=("解压文件" "安装依赖" "配置设置" "复制文件" "")

# 计算总步骤数
total_steps=${
    
    #steps[@]}

# 安装进度的初始值
progress=0

# 打印安装进度函数
print_progress() {
    
    
    local current_step=$1
    local percentage=$2
    local step=$3

    # 清除当前行
    printf "\r\033[K"

    # 构建进度条字符串
    local progress_bar=$(printf "[%-${total_steps}s] %d%%" "$(yes "#" | head -n $current_step | tr -d '\n')" "$percentage")


    # 打印安装进度
    printf "安装进度: %s %s" "$progress_bar" "$step"
}

# 循环执行每个步骤
for ((i=0; i<total_steps; i++)); do
    step=${steps[$i]}

    # 模拟每个步骤的安装操作
    sleep 1

    # 更新进度
    ((progress = (i+1) * 100 / total_steps))

    # 打印安装进度
    print_progress "$((i+1))" "$progress" "$step"
done

# 打印安装完成消息
printf "\n软件安装完成!\n"

In this example, ANSI escape sequences are used to overwrite the original printed information and keep the progress bar displayed on the same line. The overwrite effect can be achieved by using \r for a carriage return, followed by \033[K to clear the contents of the current line.

In the print_progress function, firstly clear the content of the current line, then build the progress bar string, and use \r to realize the effect of carriage return to the beginning of the line. The progress bar string contains the fill and percentage of the current step.

Finally, the progress of the installation is printed by calling the print_progress function, and an installation complete message is printed when the installation is complete.

detail

local progress_bar=$(printf "[%-${total_steps}s] %d%%" "$(yes "#" | head -n $current_step | tr -d '\n')" "$percentage")
How to understand this line?

This line of code is used to build the progress bar string progress_bar.

Explain step by step what this line of code means:

  1. yes "#" | head -n $current_step: This part of the code uses yesthe command to generate an infinitely repeating #string, and uses head -n $current_stepthe command to intercept the previous $current_stepline. This generates a #string of characters with a length of $current_step.

  2. tr -d '\n': This part of the code uses trthe command to remove newline characters in the string \n, that is, to combine multi-line strings into a single line.

  3. $percentage: This part of the code is the numerical value of the progress percentage.

  4. $(printf "[%-${total_steps}s] %d%%" ...): This part of the code uses printfthe function of the formatted string to insert the $current_stepstring #and the progress percentage of the row into the formatted string.

The specific explanation is as follows:

  • [%-${total_steps}s]: This is a formatted string representing a fixed-length string of $total_stepssquare brackets, where -%represents left alignment and ${total_steps}srepresents a string placeholder for padding #characters.

  • %d%%: This is a format string representing an integer placeholder used to fill in the value of the progress percentage, and the following %%means to print a percent sign character %.

Ultimately, progress_barthe content of the string is something like [##### ] 50%, where #the number and progress percentage change as the installation progresses.

Effect

insert image description here

Guess you like

Origin blog.csdn.net/m0_47406832/article/details/132482385