8

Possible Duplicate:
Shell programming, avoiding tempfiles

Say I have the file data.txt, and the command cmd.

cmd takes one argument, a file. Or, you could use stdin.

Now, say data.txt is uppercase, but cmd only works if all data is lowercase.

Sure, you could do this

tr '[:upper:]' '[:lower:]' < data.txt > lowercase_data.txt
cmd lowercase_data.txt
rm lowercase_data.txt

But, is there a way to integrate this?

Like, a wrapper around the original file, that applies a filter, then passes a reference to the temporary file; the command is executed; last, the temporary file is deleted?

I use zsh.

Emanuel Berg
  • 6,903
  • 8
  • 44
  • 65

2 Answers2

9

zsh supports process substitution, which should do what you're asking:

A command of the form =(...) is replaced with the name of a file containing its output.

So for your example, to avoid manually creating a temporary file to pass the output of tr into cmd, you could say

cmd =(tr '[:upper:]' '[:lower:]' < data.txt)

For other shells, the equivalent would be:

  • bash: cmd <(tr '[:upper:]' '[:lower:]' < data.txt)
  • ksh: cmd <(tr '[:upper:]' '[:lower:]' < data.txt)
  • rc: cmd <{tr '[:upper:]' '[:lower:]' < data.txt}

Note that bash, ksh and rc implement process substitution using named pipes, rather than temporary files as zsh uses, and require the /dev/fd/ filesystem to be mounted

  • 7
    zsh also supports the same <(...) form used by bash and ksh. The =(...) is made available for situations where the command will try to do something (like seek backwards) to the argument that isn't possible with a pipe. – chepner Feb 24 '13 at 21:30
2

There's nothing wrong with the way you do it. Except for the way you create your temp files (what if the script you're running is executed twice concurrently?).

You should use mktemp instead.

rahmu
  • 20,023
  • So there is no tool or well-known method to do this? You should use temporary files explicitly, only with mktemp to make it secure? – Emanuel Berg Feb 04 '13 at 00:28
  • I don't get it. mktemp is the tool you're looking for. Your usage of temporary files is totally justified. I used to ask myself the same question. Take a look at the question I link to in the comments of your question. – rahmu Feb 04 '13 at 00:29
  • The point is not how to avoid using temporary files but to integrate that dance: create file - run command - remove file -- so that the temporary files would not have to be bothered with explicitly. – Emanuel Berg Feb 04 '13 at 00:36