2015-12-16 126 views
-3

我有一个bash脚本:错误:从bash脚本 “[参数太多”

#!/bin/bash 
pathToTestCasesFile=$1 

while IFS=';' read -r col1 col2 
do 

    if [[ $col1 == V[0-9]:* ]]; then 
     echo "decrypt" 
     if [[ "$(decrypt "$col1")" == "$col2" ]] ; then 
     echo "$col1 is equal to $col2" 
     else 
     echo "$col1 is not equal to $col2" 
     fi 
    else 
     echo "encrypt" 
     if [[ "$(encrypt $col1)" == "$col2" ]] ; then 
     echo "$col1 is equal to $col2" 
     else 
     echo "$col1 is not equal to $col2" 
     fi 
    fi 

done < $pathToTestCasesFile 

exit 0 

下面是测试文件:

alex;V1:IVjd9qcAbUrR954gyPDbKw== 
V1:IVjd9qcAbUrR954gyPDbKw==;alex 

输出看起来是这样的:

encrypt 
alex is not equal to V1:IVjd9qcAbUrR954gyPDbKw== 
decrypt 
V1:IVjd9qcAbUrR954gyPDbKw== is not equal to alex 
decrypt 

但输出应该说一切都是平等的。

我确信在解密后的命令等于另一个命令的值。我单独测试了它。

也许有比较的问题..

非常感谢您的帮助。

+0

该命令对我说第23行:$ pathToTestCasesFile:模棱两可的重定向。也许是$? –

+0

好兄弟,你必须在pathToTestCasesFile加双引号变量 “$ pathToTestCasesFile:” –

+0

从'/ automatedRegressionTest'文件内容 –

回答

1

这应该工作:

#!/bin/bash 

while IFS=, read -r col1 col2 
do 
    echo "I got|$col1|$col2|" 

    if [[ "$col1" =~ V.:.* ]]; then 
     echo "decrypt" 
     if [[ "$(decrypt "$col1")" == "$col2" ]] ; then 
      echo "[$col1] is equal to [$col2]" 
     else 
      echo "[$col1] is not equal to [$col2]" 
     fi 
    else 
     echo "encrypt" 
     if [[ "$(encrypt "$col1")" == "$col2" ]] ; then 
      echo "[$col1] is equal to [$col2]" 
     else 
      echo "[$col1] is not equal to [$col2]" 
     fi 
    fi 
done < "$pathToTestCasesFile" 

为字段分隔符使用逗号, - 在你的代码。在示例数据中,您有;。如果您需要;IFS=,更改为IFS=';'

+0

是的,它是一个cmd。但你的回答并不适合我.. – Mansouritta

+4

“没有为我工作”没有帮助。你在期待什么,而发生了什么? – chepner

+0

我更新了帖子,以便您现在可以看到测试数据。当我使用您的解决方案时,输出结果如下: |得到:输入; Erwarteter输出 不等于输出 |得到:Alex; V1:sdfsfsfsff 不等于f |得到:V1:sdfsfsfsff; Alex 不等于x |得到:Moage; V1:sdfgjhhkdf 不等于df 所以它看起来像脚本将一个字符串分割成许多字符串..但它不应该。 – Mansouritta