shell pipe process repeat

2019-09-13 01:04发布

I want to do something like this:

~ cat dump.sh
command 1 | command 2 | command 1 | command 2 | ...(ten times) | command 1 | command2

~ ./dump.sh < demo.log

So how to modify dump.sh while I can specify exactly n times of command 1 and command 2 in pair to process demo.log?

1条回答
forever°为你锁心
2楼-- · 2019-09-13 01:33

You can write a simple recursive helper function, something like this:

loop () {
    case $1 in
      0) cat;;
      *) command 1 | command 2 | loop $(($1 - 1)) ;;
    esac
}

Invoke it like

loop 3 <demo.log
查看更多
登录 后发表回答