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

时光总嘲笑我的痴心妄想 提交于 2019-12-12 03:32:26

问题


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 ?


回答1:


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.




回答2:


!/usr/bin/env bash

to solve it, instead of label the command, label the file




回答3:


Process substitution puts the entire command in parens.

... <(sort ...) ...


来源:https://stackoverflow.com/questions/36391675/bash-script-using-comm-and-sort-issues-syntax-error-near-unexpected-token

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!