bash - 从流中获取数字

时间:2017-01-13 12:31:38

标签: bash shell wget cut whiptail

从命令 chk 文件中记录的典型输出:

wget -O - http://website/file > /dev/null 2>chk &

类似于:

   0K .......... .......... .......... .......... ..........  0%  143K 62s

  50K .......... .......... .......... .......... ..........  1%  433K 41s

 100K .......... .......... .......... .......... ..........  1% 1.20M 30s

 150K .......... .......... .......... .......... ..........  2%  259K 31s

 200K .......... .......... .......... .......... ..........  2% 83.2M 24s

...

 8800K .......... .......... .......... .......... .......... 98%  260K 1s

 8850K .......... .......... .......... .......... .......... 98%  329K 0s

 8900K .......... .......... .......... .......... .......... 99%  433K 0s

 8950K .......... .......... .......... .......... ......... 100%  331K=31s

2017-01-13 13:16:59 (288 KB/s) - written to stdout [9215609/9215609]

在整个下载过程中,文件会逐行更新。 好吧,我只需得到百分比:0,1,2 ...... 99,仅此而已。

以下脚本完成这项工作,即使不完美:

tail -n 5 chk | tail -n 1 | colrm 1 63 | cut -d '%' -f 1

当我需要对bash脚本执行相同操作时会出现问题,如下所示:

#!/bin/bash
# Test script for getting the percentage number from 'wget' output
i=0
wget -O - http://website/file > /dev/null 2>chk &
sleep 1

while (( $i < 90 ))
do
i=`tail -n 5 chk | tail -n 1 | colrm 1 63 | cut -d '%' -f 1`
echo $i
done

脚本开始获取所需文件,它写出chk文件,但停止并显示错误消息:

line 9: ((: < 90 : syntax error: operand expected (error token is "< 90 ")

我尝试使用[[]],引用...但不起作用。

这里有什么想做的更好吗?

1 个答案:

答案 0 :(得分:1)

wget,whiptail和GNU sed的进度条:

var list = [];
var mkeys = ['-Ka2D86jpdjKpTf2-Nmf', '-KaCPtlIoPX34ltMPqs3'];

   function Getumanes(msgkeys,callback) {
    msgkeys.forEach(function (key) {
        var msgstref = firebase.database().ref("Msgtemplate/" + key + '/Actionusers');
        msgstref.orderByChild('status').equalTo(true).limitToFirst(2).once('value').then(function (snap) {
            snap.forEach(function (csnap) {
                list.push(csnap.key);
            })
            console.log(list);
        })
    })
    callback(list)
}

Getumanes(mkeys, function (finallist) {
    console.log("finallist: " + finallist);
});