bash + progress dialog bar in linux machine

2019-07-29 11:45发布

The script run_tasks.bash read the text file

The script need to read line by line the text file and execute the scripts in the text file

One important remark about file.txt

Lines numbers in the txt file could be different

For example on the first running lines in txt file could be 12

On the second running lines in txt file could be for example 213 , and so on .....

   ./run_tasks.bash   /tmp/file.txt

Example of text file - file.txt

 1 /tmp/run.sh
 2 /var/tmp/load_run.pl
 3 /etc/RT/LM.pl
 .
 .

What I want to do is a process dialog progress that illustrate the number of the tasks in the txt file

For example if number of tasks ( lines ) in the txt file is 34 Then the dialog process will start from 0% to 100% according to the 34 tasks

Or

If the number of tasks (lines) in the txt are 321 then the dialog process will start from 0% to 100% reference to the 321 tasks

Another thing the dialog need to view each of the ruining script in the dialog

Please advice how to build the dialog code according to my requirements

Mean while I have the following dialog code but I not understand how to fit this code to the different tasks ( lines ) in the txt file

 #!/bin/sh
 #A gauge Box example with dialog
 (
  c=10
 while [ $c -ne 110 ]
   do
     echo $c
     echo "###"
     echo "$c %"
     echo "###"
     ((c+=10))
      sleep 1
done
  ) |
 dialog --title "A Test Gauge With dialog" --gauge "Please wait ...." 10 60 0

1条回答
ゆ 、 Hurt°
2楼-- · 2019-07-29 12:18

It's a matter of math ;) In items I've put the total number of items you'll be processing. You probably want to have something like items=$(wc -l file.txt) there. To convert the number of processed lines into a percentage, I do $(( $processed * 100 / $items)). Note the order, since we only have integers the usual processed/items*100 won't work.

#!/bin/bash

(
    items=123
    processed=0
    while [ $processed -le $items ]; do
        pct=$(( $processed * 100 / $items ))
        echo "XXX"
        echo "Processing item $processed"
        echo "XXX"
        echo "$pct"
        processed=$((processed+1))
        sleep 0.1
    done
) | dialog --title "Gauge" --gauge "Wait please..." 10 60 0
查看更多
登录 后发表回答