help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Use stdin without tempfile


From: Matthew Cengia
Subject: Re: [Help-bash] Use stdin without tempfile
Date: Mon, 2 Dec 2013 10:51:30 +1100
User-agent: Mutt/1.5.21 (2010-09-15)

On 2013-12-01 16:59, Peng Yu wrote:
> Hi,
> 
> I want to process stdin in the way that the first 10 lines are
> processed by prog1, and the remaining lines are processed by prog2.
> 
> The output of prog1 is printed to stdout first, then the output of
> prog2 is printed to stdout. No tempfiles should be created, but fifo
> can be created. Is it possible?
> 
> When the input is not stdin, a script like the following can be easily
> created. But when the input is a pipe, it is not clear to me what to
> do without creating a tempfile.

bash$ printf "%s\n" {1..30} > file.txt
bash$ cat main.sh
#!/bin/bash

prog1(){
  local line
  while read -r line
  do
    printf "PROG1: %s\n" "$line"
  done
}
prog2(){
  local line
  while read -r line
  do
    printf "PROG2: %s\n" "$line"
  done
}

{
  for i in {1..10}
  do read -r line
    echo "$line"
  done | prog1

  prog2
} < "$1"
bash$ ./main.sh file.txt
PROG1: 1
PROG1: 2
PROG1: 3
PROG1: 4
PROG1: 5
PROG1: 6
PROG1: 7
PROG1: 8
PROG1: 9
PROG1: 10
PROG2: 11
PROG2: 12
PROG2: 13
PROG2: 14
PROG2: 15
PROG2: 16
PROG2: 17
PROG2: 18
PROG2: 19
PROG2: 20
PROG2: 21
PROG2: 22
PROG2: 23
PROG2: 24
PROG2: 25
PROG2: 26
PROG2: 27
PROG2: 28
PROG2: 29
PROG2: 30

-- 
Regards,
Matthew Cengia

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]