3

I have a problem when run diff in shellscript, it's syntax error near unexpected token('` code:

i have a 2 file File A, File B, i want compare 2 files and using in script:

diff <( sort fileA ) <( sort FileB )

but when run its get error:

syntax error near unexpected token `('

please help me! Thanks all!

6
  • do you want to diff the sorted fileA and sorted fileB? Commented Apr 11, 2013 at 9:18
  • I have checked in local and it works fine in my computer. Uhms. Commented Apr 11, 2013 at 9:22
  • you tried when add in shellscript? Commented Apr 11, 2013 at 9:28
  • i add it in for loop: for i in $(cat log.txt); do diff <(sort fileA.${i}) <(sort fileB.${i}) >> fileC done Commented Apr 11, 2013 at 9:34
  • 2
    What is on the top line of your script? #!/bin/bash or #!/bin/sh? if the latter, change to the former. Good luck. Commented Apr 11, 2013 at 11:03

1 Answer 1

3

Credit goes to @shellter. The construct you are using is called process substitution, which is not defined by the POSIX standard so you cannot rely on all your shells implementing this feature.

Also, when you encounter problems like this, always make sure you are actually running your script through the shell that you intend to use and if you ask a question here regarding shell scripting, mention which shell it is that you are using or that you need your problem to be targeted in, as this can make quite a difference.

Here are some examples to demonstrate that this works in e.g. bash and ksh, but not in e.g. dash:

$ bash -c 'diff <( sort file1 ) <( sort file2 )'
2c2
< file1
---
> file2

$ ksh -c 'diff <( sort file1 ) <( sort file2 )'
2c2
< file1
---
> file2

$ dash -c 'diff <( sort file1 ) <( sort file2 )'
dash: 1: Syntax error: "(" unexpected

$ sh -c 'diff <( sort file1 ) <( sort file2 )'
sh: -c: line 0: syntax error near unexpected token `('
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.