简体   繁体   English

在脚本中运行命令时的Bash进度栏

[英]Bash progress bar when running commands from within a script

I'm writing a bash script which performs an installation of stuff. 我正在编写一个bash脚本,用于执行东西的安装。

I don't want the user to see the commands and their output, so I'll redirect them using 2>&1 . 我不希望用户看到命令及其输出,因此我将使用2>&1重定向它们。

I do wish to print a progress bar for each section which finished installing, and a success/failed message according if there were errors, for example: 我确实希望为每个完成安装的部分打印一个进度条,并根据是否存在错误来显示成功/失败消息,例如:

Installing OpenCv ..................... [Success]
Installing Qt     ..................... [Failed]

Here's my code: 这是我的代码:

#!/bin/bash

installOf="Installing OpenCv  "

function printProgressBar() 
{
    local progressBar="."
    printf "%s" "${progressBar}"
}

function InstEssent
{
    sudo apt-get -y install build-essential
    sleep 5
    echo "Done"
}

printf "%s" "${installOf}"

InstEssent  &

while [ "${InstEssent}" != "Done" ]
do
    printProgressBar 
    sleep 1
done

installStatus="Success"
printf " [%s]\n" "${installStatus}"

Well, as such there are no native tools in bash or other shells I can know of, but you can use this below printf & this custom function to achieve what you need. 好吧,因此我不知道bash或其他shell中没有本机工具,但是您可以在printf和此自定义函数下使用此工具来实现所需的功能。 This small snippet will print the installation progress bar which you can print by just a normal function call printProgressBar at various places in the script where you would like to show it. 这个小片段将打印安装进度条,您可以通过普通函数调用printProgressBar来打印安装进度条,该脚本可以在脚本中要显示它的各个位置显示。

function printProgressBar() {
    local progressBar="."
    printf "%s" "${progressBar}"
}

Assuming you have n steps in your function call, insert this function call at places between. 假设函数调用中有n步骤,请将此函数调用插入之间的位置。 For the actual printing of error message, fill the installation header in variable installOf which assuming from your example could take either "Installing OpenCv " (or) "Installing Qt " , use it in this variable before the steps as 对于错误消息的实际打印,将安装标头填充到变量installOf ,假设您的示例可能采用"Installing OpenCv " (或) "Installing Qt " ,请在执行以下步骤之前在此变量中使用它

installOf="Installing OpenCv  "
printf "%s" "${installOf}"

and for the final status, since you didn't let us know how you get the overall status of the installation, assuming you find it depending upon success or failure, update it in another variable 对于最终状态,由于您没有让我们知道如何获得安装的总体状态(假设您根据成功或失败找到了安装状态),请在另一个变量中进行更新

installStatus="Success"
printf " [%s]\n" "${installStatus}"

So putting it all together, I have this simple while loop which runs the function for 20 calls, you can use a similar way to adopt your function call at various positions in your script. 因此,综上所述,我有一个简单的while循环,该循环可运行20个函数,您可以使用类似的方式在脚本的各个位置采用函数调用。

installOf="Installing OpenCv  "

function printProgressBar() {
    local progressBar="."
    printf "%s" "${progressBar}"
}

printf "%s" "${installOf}"

while (( cnt < 20))
do
    ((cnt++))
    printProgressBar 
    sleep 1
done

# You can determine the status of your installation as your script demands

installStatus="Success"
printf " [%s]\n" "${installStatus}"

Running the script produces a result something similar to your requirement, 运行脚本会产生类似于您要求的结果,

$ bash script.sh
Installing OpenCv  .................... [Success]

Observe that, each . 观察每个. represents each instance of a function call. 表示函数调用的每个实例。

Update:- 更新: -

Looking at your code logic, you are missing a point on how background jobs work. 查看您的代码逻辑,您缺少关于后台作业如何工作的要点。 Your background function InstEssent in installing a certain module. 您的后台功能InstEssent安装特定模块。 To use the progress bar effectively, you need to constantly to poll the background job to see it is still running using the kill -0 "$pid" command and if it is running, print the install bar as indicated in the code below. 要有效使用进度条,您需要使用kill -0 "$pid"命令不断地轮询后台作业以查看其是否仍在运行,如果正在运行,请按照以下代码所示打印安装栏。

function InstEssent()
{
    sudo apt-get -y install build-essential
    sleep 5
}

printf "%s" "${installOf}"

InstEssent  &
pid_InstEssent="$!"

while kill -0 "$pid" 2> /dev/null
do
    printProgressBar 
    sleep 1
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM