2 edited tags
| link
    Tweeted twitter.com/#!/StackUnix/status/189044889403666433
1
source | link

command like wc but with tee behaviour

I want to backup a database using psql and COPY command. here is my script:

psql "user=${USERNAME} host=${HOSTNAME} dbname=${DBNAME} password=${PASSWORD}" -c \
"COPY (SELECT * FROM tbl) ORDER BY id ASC) TO STDOUT WITH CSV HEADER;" | \
bzip2 -z -f --best -c > /home/${DBNAME}-${FILENAME}.csv.bz2

Also I want to know how many lines are copied from database but psql does not have such feature. so I need external command. I need something like this:

psql ... | wc -l | bzip2

Is there any solution? The only solution I have found is to use fifo:

mkfifo /home/backup.fifo
psql ... | tee /home/backup.fifo | bzip2

Now from another terminal window I can use wc:

wc -l /home/backup.fifo