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.

Normally you would write:

diff file1 file2

But I would like to diff a file and output from the command (here I make command a trivial one):

diff file1 <(cat file2 | sort)

Ok, this work when I enter this manually at shell prompt, but when I put exactly the same line in shell script, and then run the script, I get error.

So, the question is -- how to do this correctly?

Of course I would like avoid writing the output to a temporary file.

share|improve this question

2 Answers 2

up vote 15 down vote accepted

I suspect your script and your shell are different. Perhaps you have #!/bin/sh at the top of your script as the interpreter but you are using bash as your personal shell. You can find out what shell you run in a terminal by running echo $SHELL.

An easier way to do this which should work across most shells would be to use a pipe redirect instead of the file read operator you give. The symbol '-' is a standard nomenclature for reading STDIN and can frequently be used as a replacement for a file name in an argument list:

cat file2 | sort | diff file1 -

Or to avoid a useless use of cat:

sort < file2 | diff file1 -
share|improve this answer
    
Ah, you are good, indeed, I didn't check if sh is bash or not. Thank you very much for the solution. –  greenoldman Apr 21 '11 at 10:04

The scope of the standard input it's the script itself, so just put your code inside a blocku like this:

{
  diff file1 <(sort file2)
}

In this way the scope of the STDIN it's inside the block..

I've tried and it works.

share|improve this answer
    
That's not a function, it's a block, and it doesn't make any difference. –  Gilles Apr 20 '11 at 23:05
    
@gilles Thx for the correction. BTW I've tried myself and it works. Did you? –  tmow Apr 21 '11 at 6:09
2  
The block doesn't have any effect on the validity of the <(…) construct. Your script will work or not work depending on whether you run it in a shell that supports <(…) or not (ksh93/bash/zsh vs Bourne/ash/kshk88). Caleb's answer nails it all. –  Gilles Apr 21 '11 at 6:51
    
@gilles ok, thx for the explanation. –  tmow Apr 26 '11 at 12:38

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.