Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question

1 Answer 1

up vote 8 down vote accepted

Both bash and zsh have a >(pipeline) feature:

psql ... | tee >(wc -l) | bzip2

Note that the > here is not a normal redirection, but a necessary part of the syntax. You would need a second > if you wanted to combine it with an actual redirection (with a space in between so it wouldn't be read as >> redirect-for-append).

share|improve this answer
1  
what is >(wc -l) called so I can search it? :D –  Majid Azimi Apr 8 '12 at 9:29
1  
@MajidAzimi It's called Process Substitution. –  jw013 Apr 8 '12 at 9:30
2  
You probably want >(wc -l >&2) unless you want the wc output to get bzipped as well. –  jw013 Apr 8 '12 at 9:34
1  
That is an... interesting... bash behavior; I would expect it to be independent of the rest of the pipeline (and in zsh it is). –  geekosaur Apr 8 '12 at 9:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.