2015-09-08 22 views
0

我有需要在兩個變量進行比較的內容 - RES1和RES2回聲重定向字符串區分操作

compRes=`diff -W999 --side-by-side <(echo ${res1}) <(echo ${res2})` 

拋出一些錯誤

command substitution: line 435: syntax error near unexpected token `(' 
command substitution: line 435: `diff -W999 --side-by-side <(echo ${res1}) <(echo ${res2})' 

嘗試 - 雙引號,以及

compRes=`diff -W999 --side-by-side <(echo "$res1") <(echo "$res2")` 

同一個錯誤:

command substitution: line 435: syntax error near unexpected token `(' 
command substitution: line 435: `diff -W999 --side-by-side <(echo "$res1") <(echo "$res2")' 

可能是什麼問題?

一些額外的信息(怪):

File: test.sh 

    file1="Simple.csv" 
    file2="SimpleWithAddedLine.csv" 

    res1=$(cut -d, -f1-2 $file1) 
    res2=$(cut -d, -f1-2 $file2) 
    compRes=`diff -W999 --side-by-side <(echo "$res1") <(echo "$res2") | sed '/^[^\t]*\t\s*|\t\(.*\)/{s//\1 CMPUPDATED/;b};/^\([^\t]*\)\t*\s*<$/{s//\1 CMPDELETED/;b};/^.*>\t\(.*\)/{s//\1 CMPNEW/;b};d'` 
    added=$(echo "$compRes" | grep "CMPNEW" | wc -l) 
    deleted=$(echo "$compRes" | grep "CMPDELETED" | wc -l) 
    updated=$(echo "$compRes" | grep "CMPUPDATED" | wc -l) 
    let "ttlUpdates = $added + $deleted + $updated" 
    echo -e "\nAdded: $added - Deleted: $deleted - Updated: $updated" 
    echo -e "Total Changes: $ttlUpdates (Maximum allowed: $maxAllowed)\n" 

而且

File: callscript.sh 

#!/bin/bash 

CORE_SCRIPT_FILE="test.sh" 
sh ${CORE_SCRIPT_FILE} | tee "logfile.log" 

OK,我有兩個文件。 當我打電話給test.sh時 - 一切都很順利。

[batch]$ ./test.sh 

Added: 4 - Deleted: 1 - Updated: 1 
Total Changes: 6 (Maximum allowed:) 

與此同時,當我打電話 - callscript.sh文件:

[batch]$ ./callscript.sh 
/abc/test.sh: command substitution: line 6: syntax error near unexpected token `(' 
/abc/test.sh: command substitution: line 6: `diff -W999 --side-by-side <(echo "$res1") <(echo "$res2") | sed '/^[^\t]*\t\s*|\t\(.*\)/{s//\1 CMPUPDATED/;b};/^\([^\t]*\)\t*\s*<$/{s//\1 CMPDELETED/;b};/^.*>\t\(.*\)/{s//\1 CMPNEW/;b};d'' 
+0

試試雙引號'$ res1'和'$ res2'? – pynexj

+0

@whjm - 也試過雙引號。同樣的錯誤 –

+0

什麼是你使用的外殼,我沒有看到在Bash shell中的任何問題。 –

回答

2

callscript.sh當你運行你正在使用shtest.sh<(...)過程替換語法只是在bash。將最後一行更改爲

bash ${CORE_SCRIPT_FILE} | tee "logfile.log" 

它應該可以正常工作。

+0

非常感謝它的工作。希望 。它不會爲「test.sh」中存在的其他活動造成任何其他麻煩。 –