Skip to content
Advertisement

redirecting stdin to tempfile in script

I want to write a bash script that receives a list of files through a pipe, writes a tempfile and then starts a program (qiv – an image viewer) with this tempfile.

Example:

find . -atime 2 | piped_qiv

where piped_qiv would look something like this:

#!/bin/bash
[receive file list through pipe] > /tmp/qiv_temp
qiv -fm -F /tmp/qiv_temp
rm /tmp/qiv_temp

I’m probably missing something very basic about bash scripting but I was not able to find a proper solution to this problem on this side or google.

Sidenote: I know I could also use qiv $(find . …) but using pipes would feel more natural to me.

Advertisement

Answer

Use xargs:

find . -atime 2 -print0 | xargs -0 my_command

-print0 will make find print each found file seperated by NUL () and -0 will make xargs read each line separated by the same.

Alternative if you want to pass a file you can use process substitution:

% echo <(ls)
/dev/fd/63

And in your case:

qiv -fm -F <(my_command)

Or reading from stdin:

qiv -fm -F <(cat)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement