我正在尝试找到一种方法,一个进度需要多长时间,所以我不会硬编码设定的时间长度。我试图在不添加任何新库的情况下做到这一点。pv 命令似乎很流行,但我的系统由于某种原因不支持它。我假设有一种方法 SIGINFO 可以获取此信息。我尝试做 awhile [ ! -z pgrep command ] do \\logic
让它在任务存在时运行。这是针对以下代码的 load%:
if [ "$selected_choice" == "Go Back" ]
then gsettings set org.gnome.desktop.peripherals.keyboard repeat false && echo -en "\e" #This is the escape sequence in the first menu.
else cd && cd $search_dir && jar cf $file $fileclass & bash ~/Bash_Files/Bash_Animations/load3.sh
fi
这是load3.sh的代码
#!/bin/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 "%.0f\n" $progress)
if [ $fill -gt $progressBarWidth ]; then
fill=$progressBarWidth
fi
empty=$(($fill-$progressBarWidth))
# Percentage Calculation
percent=$(echo "100/$taskCount*$tasksDone" | bc -l)
percent=$(printf "%0.2f\n" $percent)
if [ $(echo "$percent>100" | bc) -gt 0 ]; then
percent="100.00"
fi
# Output to screen
## https://unix.stackexchange.com/questions/664055/how-to-print-utf-8-symbols this is why %s and %b are used
## ORGINAL
##printf "\r["
##printf "%${fill}s" '' | tr ' ' ▉
##printf "%${empty}s" '' | tr ' ' ░
##printf "] $percent%% - $text "
#This is for GNU users as UNICODE charaters do not work on their terminal
printf "\r["
printf "%${fill}s" '' | sed 's/ /\o342\o226\o210/g'
printf "%${empty}s" '' | sed 's/ /\o342\o226\o221/g'
printf "] $percent%% - $text "
}
## Collect task count
taskCount=33
tasksDone=0
while [ $tasksDone -le $taskCount ]; do
# Do your task
(( tasksDone += 1 ))
# Can add some friendly output
# text=$(echo "somefile-$tasksDone.dat")
# Draw the progress bar
progressBar $taskCount $taskDone $text
sleep 0.01
done
echo
代码工作的逻辑是我只是尝试动态分配值taskCount
或taskDone
找到不同的方法来计算任务开始到完成。