使用BASH显示进度(工作)指标【JAVA教程】

!
也想出现在这里? 联系我们
信息

使用BASH显示进度(工作)指标,第1张

概述使用BASH显示进度(工作)指标

使用bash脚本,你怎么提供一个bash进度指示器?

所以我可以运行一个命令表单bash,并且当这个命令执行的时候让用户知道一些事情还在发生。

无法在windows API中实现仪表

保持进度条在上面。 linux的

在这个使用SCP的例子中,我演示了如何获取进程ID(pID),然后在进程运行时做一些事情。

这显示一个简单的spinnng图标。

/usr/bin/scp me@website.com:file somewhere 2>/dev/null & pID=$! # Process ID of the prevIoUs running command spin[0]=\”-\” spin[1]=\”\\\” spin[2]=\”|\” spin[3]=\”/\” echo -n \”[copying] ${spin[0]}\” while [ kill -0 $pID ] do for i in \”${spin[@]}\” do echo -ne \”b$i\” sleep 0.1 done done

威廉Pursell的解决方案

/usr/bin/scp me@website.com:file somewhere 2>/dev/null & pID=$! # Process ID of the prevIoUs running command spin=\’-|/\’ i=0 while kill -0 $pID 2>/dev/null do i=$(( (i+1) %4 )) printf \”r${spin:$i:1}\” sleep .1 done

如果您有一种方法可以估算已完成的百分比,例如当前处理的文件数量和总数,则可以制作一个简单的线性进度表,其中包含一些数学运算和关于屏幕宽度的假设。

count=0 total=34 pstr=\”[=======================================================================]\” while [ $count -lt $total ]; do sleep 0.5 # this is work count=$(( $count + 1 )) pd=$(( $count * 73 / $total )) printf \”r%3d.%1d%% %.${pd}s\” $(( $count * 100 / $total )) $(( ($count * 1000 / $total) % 10 )) $pstr done

或者不用线性电表,你可以估计剩下的时间。 这与其他类似的事情一样准确。

count=0 total=34 start=`date +%s` while [ $count -lt $total ]; do sleep 0.5 # this is work cur=`date +%s` count=$(( $count + 1 )) pd=$(( $count * 73 / $total )) runtime=$(( $cur-$start )) estremain=$(( ($runtime * $total / $count)-$runtime )) printf \”r%d.%d%% complete ($count of $total) – est %d:%0.2d remaininge[K\” $(( $count*100/$total )) $(( ($count*1000/$total)%10)) $(( $estremain/60 )) $(( $estremain%60 )) done printf \”ndonen\”

这里介绍的是一个不错的微调功能(稍作修改),也会帮助你的光标停留在原来的位置。

spinner() { local pID=$! local delay=0.75 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 \”bbbbbb\” done printf \” bbbb\” }

用法:

(a_long_running_task) & spinner

这是一个相当简单的技术:

@H_403_39@

(只要用你想要指示的任何命令替换sleep 20 )

#!/bin/bash sleep 20 & PID=$! #simulate a long process echo \”THIS MAY TAKE A WHILE,PLEASE BE PATIENT WHILE ______ IS RUNNING…\” printf \”[\” # While process is running… while kill -0 $PID 2> /dev/null; do printf \”▓\” sleep 1 done printf \”] done!\”

输出如下所示:

> THIS MAY TAKE A WHILE,PLEASE BE PATIENT WHILE ______ IS RUNNING… > [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] done!

它每秒钟增加一个high(高密度点),直到过程完成。

这里有一个简单的在线人员,我使用:

while true; do for X in \’-\’ \’/\’ \’|\’ \’\’; do echo -en \”b$X\”; sleep 0.1; done; done

这是我的尝试。 我是新的bash脚本,所以这些代码可能是可怕的:)

输出示例:

代码:

progressbarWIDth=20 # Function to draw progress bar progressbar () { # Calculate number of fill/empty slots in the bar progress=$(echo \”$progressbarWIDth/$taskCount*$tasksDone\” | bc -l) fill=$(printf \”%.0fn\” $progress) if [ $fill -gt $progressbarWIDth ]; then fill=$progressbarWIDth fi empty=$(($fill-$progressbarWIDth)) # Percentage Calculation percent=$(echo \”100/$taskCount*$tasksDone\” | bc -l) percent=$(printf \”%0.2fn\” $percent) if [ $(echo \”$percent>100\” | bc) -gt 0 ]; then percent=\”100.00\” fi # Output to screen printf \”r[\” printf \”%${fill}s\” \’\’ | tr \’ \’ ▉ printf \”%${empty}s\” \’\’ | tr \’ \’ ░ printf \”] $percent%% – $text \” } ## Collect task count taskCount=33 tasksDone=0 while [ $tasksDone -le $taskCount ]; do # Do your task (( tasksDone += 1 )) # Add some frIEndly output text=$(echo \”somefile-$tasksDone.dat\”) # Draw the progress bar progressbar $taskCount $taskDone $text sleep 0.01 done echo

你可以在这里看到源代码: https : //gist.github.com/F1LT3R/fa7f102b08a514f2c535

除了古典的微调,你可以使用这个进度条

它通过使用半块字符来实现子字符精度

代码包含在链接中。

@ DavIDD对Pez Cuckows的评论回答,这是一个如何在脚本中捕获进度条输出的例子,并且仍然可以看到屏幕上的微调:

#!/usr/bin/env bash ############################################################################# ########################################################################### ### ### ModifIEd/Rewritten by AMDanischewski (c) 2015 v1.1 ### Issues: If you find any issues emai1 me at my <first name> dot ### <my last name> at gmail dot com. ### ### Based on scripts posted by Pez Cuckow,William Pursell at: ### http://stackoverflow.com/questions/12498304/using-bash-to-display- ### a-progress-working-indicator ### ### This program runs a program passed in and outputs a timing of the ### command and it exec\’s a new fd for stdout so you can assign a ### variable the output of what was being run. ### ### This is a very new rough draft but Could be expanded. ### ### This program is free software: you can redistribute it and/or modify ### it under the terms of the GNU General Public license as published by ### the Free Software Foundation,either version 3 of the license,or ### (at your option) any later version. ### ### This program is distributed in the hope that it will be useful,### but WITHOUT ANY WARRANTY; without even the implIEd warranty of ### MERCHANTABIliTY or fitness FOR A PARTIculaR PURPOSE. See the ### GNU General Public license for more details. ### ### You should have received a copy of the GNU General Public license ### along with this program. If not,see <http://www.gnu.org/licenses/>. ########################################################################### ############################################################################# declare CMD=\”${1}\” shift ## Clip the first value of the $@,the rest are the options. declare CMD_OPTIONS=\”$@\” declare CMD_OUTPUT=\”\” declare TMP_OUTPUT=\”/tmp/_${0##*/}_$$_$(date +%Y%m%d%H%M%s%N)\” declare -r SPIN_DELAY=\”0.1\” declare -i PID= function usage() { cat <<EOF Description: ${0##*/} This program runs a program passed in and outputs a timing of the command and it exec\’s a new fd for stdout so you can assign a variable the output of what was being run. Usage: ${0##*/} <command> [command options] Eg >$ ${0##*/} sleep 5 && echo \”hello\” | figlet Running: sleep 5 && echo hello | figlet,PID 2587:/ real 0m5.003s user 0m0.000s sys 0m0.002s _ _ _ | |__ ___| | | ___ | \’_ / _ | |/ _ | | | | __/ | | (_) | |_| |_|___|_|_|___/ Done.. >$ var=$(${0##*/} sleep 5 && echo hi) Running: sleep 5 && echo hi,PID 32229:- real 0m5.003s user 0m0.000s sys 0m0.001s Done.. >$ echo $var hi EOF } function spin_wait() { local -a spin spin[0]=\”-\” spin[1]=\”\\\” spin[2]=\”|\” spin[3]=\”/\” echo -en \”Running: ${CMD} ${CMD_OPTIONS},PID ${PID}: \” >&3 while kill -0 ${PID} 2>/dev/random; do for i in \”${spin[@]}\”; do echo -ne \”b$i\” >&3 sleep ${SPIN_DELAY} done done } function run_cmd() { exec 3>$(tty) eval \”time ${CMD} ${CMD_OPTIONS}\” 2>>\”${TMP_OUTPUT}\” | tee \”${TMP_OUTPUT}\” & PID=$! # Set global PID to process ID of the command we just ran. spin_wait echo -en \”n$(< \”${TMP_OUTPUT}\”)n\” >&3 echo -en \”Done..n\” >&3 rm \”${TMP_OUTPUT}\” exec 3>&- } if [[ -z \”${CMD}\” || \”${CMD}\” =~ ^-. ]]; then usage | more && exit 0 else run_cmd fi exit 0

迷幻进度条bash脚本。 通过命令行调用“./progressbar xy”,其中“x”是以秒为单位的时间,“y”是要显示的消息。 内部函数progressbar()也是独立工作的,它以\’x\’为百分数,\’y\’为消息。

#!/bin/bash if [ \”$#\” -eq 0 ]; then echo \”x is \”time in seconds\” and z is \”message\”\”; echo \”Usage: progressbar xz\”; exit; fi progressbar() { local loca=$1; local loca2=$2; declare -a bgcolors; declare -a fgcolors; for i in {40..46} {100..106}; do bgcolors+=(\”$i\”) done for i in {30..36} {90..96}; do fgcolors+=(\”$i\”) done local u=$(( 50 – loca )); local y; local t; local z; z=$(printf \’%*s\’ \”$u\”); local w=$(( loca * 2 )); local bouncer=\”.oO°Oo.\”; for ((i=0;i<loca;i++)); do t=\”${bouncer:((i%${#bouncer})):1}\” bgcolor=\”\\E[${bgcolors[RANDOM % 14]}m \\033[m\” y+=\”$bgcolor\”; done fgcolor=\”\\E[${fgcolors[RANDOM % 14]}m\” echo -ne \” $fgcolor$t$y$z$fgcolor$t \\E[96m(\\E[36m$w%\\E[96m)\\E[92m $fgcolor$loca2\\033[mr\” }; timeprogress() { local loca=\”$1\”; local loca2=\”$2\”; loca=$(bc -l <<< scale=2;\”$loca/50\”) for i in {1..50}; do progressbar \”$i\” \”$loca2\”; sleep \”$loca\”; done echo -e \”n\” }; timeprogress \”$1\” \”$2\”

下面是一个“活动指标”的例子,用于通过linux speedtest-cli命令进行互联网连接速度测试:

printf \’ntInternet speed test: \’ # http://stackoverflow.com/questions/12498304/using-bash-to-display-a-progress-working-indicator spin[0]=\”-\” spin[1]=\”\\\” spin[2]=\”|\” spin[3]=\”/\” # http://stackoverflow.com/questions/20165057/executing-bash-loop-while-command-is-running speedtest > .st.txt & ## & : continue running script pID=$! ## PID of last command # If this script is killed,kill \’speedtest\’: trap \”kill $pID 2> /dev/null\” EXIT # While \’speedtest\’ is running: while kill -0 $pID 2> /dev/null; do for i in \”${spin[@]}\” do echo -ne \”b$i\” sleep 0.1 done done # disable the trap on a normal exit: trap – EXIT printf \”nt \” grep Download: .st.txt printf \”t \” grep Upload: .st.txt echo \’\’ rm -f st.txt

总结

以上是内存溢出为你收集整理的使用BASH显示进度(工作)指标全部内容,希望文章能够帮你解决使用BASH显示进度(工作)指标所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

© 版权声明
THE END
喜欢就支持一下吧
点赞122 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容