为diff操作回显重定向字符串



我有内容需要在两个变量中进行比较- 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")'

有什么问题吗?

一些附加信息(Strange):

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]*ts*|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"

好的,我有两个文件。当我调用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]*ts*|t(.*)/{s//1 CMPUPDATED/;b};/^([^t]*)t*s*<$/{s//1 CMPDELETED/;b};/^.*>t(.*)/{s//1 CMPNEW/;b};d''

callscript.sh中,当您运行test.sh时,您正在使用sh,但<(...)进程替换语法仅在bash中。将最后一行改为

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

最新更新