bashcomm

Bash script using COMM and SORT issues syntax error near unexpected token


Linux, CentOS - to compare 2 files I use command

comm --check-order -1 --output-delimiter=----- <sort(file1.txt) <sort (file3.txt) > result.txt ;

and it works on shell, but when I try create a bash file - I have

syntax error near unexpected token `('

The script is simplest

#!/bin/bash
cd /var/www/html/compare/ ;
comm --check-order -1 --output-delimiter=----- <sort(file1.txt) <sort (file3.txt) > result.txt ;
exit ;
    sh

I already tried variations with escaping of round brackets like

sort\(file1.txt\)

or

sort'(file1.txt)'

but this case shell says

sort(file1.txt)...: No such file or directory

I tried with absolute path like

<sort\(var/www/html/compare/file1.txt\)

same result "No such file"

and I already tried run the script with variations like

sh /a/compare.sh
bash /a/compare.sh
chmod +x /a/compare.sh; ./a/compare.sh

Still same problem.

So I have OR "No such file..." with escaping of brackets - OR "unexpected token"in other cases.

Thanks in advance for any ideas to try, may be should be a right "mix" of syntax ?


Solution

  • After many combinations I found the solution where we need to force BASH with a little specific escaping. This script works

    #!/bin/bash
    cd /var/www/html/compare/ ;
    `bash -c "comm --check-order -1 --output-delimiter=----- <(sort file1.txt) <(sort file2.txt) > result.txt" ` ;
    exit ;
        sh
    

    Hopefully will help somebody to save the time.