Dani Gehtdichnixan Dani Gehtdichnixan - 1 year ago 89
Linux Question

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.


find . -atime 2 | piped_qiv

where piped_qiv would look something like this:

[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.

Answer Source

Use xargs:

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

-print0 will make find print each found file seperated by NUL (\0) 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)

And in your case:

qiv -fm -F <(my_command)

Or reading from stdin:

qiv -fm -F <(cat)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download