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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 -
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0