bash 尽管代码看起来不错,但字符串比较不起作用
bash string comparison not working although code appears fine
下面是我的简单bash脚本
!/usr/bin/sh
cmd_output=$(ctmcontb -ADD )
echo below is the hex output of "CONDITION: DATE: added"
echo "CONDITION: DATE: added" | od -xc
echo below is the hex output for the variable cmd_ouput
echo "$cmd_output" | od -xc
echo raw input passed to cmdlet with user arguments inputted and a space added infront
echo " CONDITION: DATE: added" | od -xc
if [ "$cmd_ouput" = " CONDITION: DATE: added" ]; then
echo successfull ran util
exit 0
else
echo error occurred running util
exit 1
fi
下面是输出
ctmtest1-tctmsv80 [49] job_late.sh ctmtest1 u350932-14 0910
below is the hex output of CONDITION:u350932-14 DATE:0910 added
0000000 434f 4e44 4954 494f 4e3a 7533 3530 3933
C O N D I T I O N : u 3 5 0 9 3
0000020 322d 3134 2044 4154 453a 3039 3130 2061
2 - 1 4 D A T E : 0 9 1 0 a
0000040 6464 6564 0a00
d d e d \n
0000045
below is the hex output for the variable cmd_ouput
0000000 2043 4f4e 4449 5449 4f4e 3a75 3335 3039
C O N D I T I O N : u 3 5 0 9
0000020 3332 2d31 3420 4441 5445 3a30 3931 3020
3 2 - 1 4 D A T E : 0 9 1 0
0000040 6164 6465 640a
a d d e d \n
0000046
raw input passed to cmdlet with user arguments in putted and a space added in front
0000000 2043 4f4e 4449 5449 4f4e 3a75 3335 3039
C O N D I T I O N : u 3 5 0 9
0000020 3332 2d31 3420 4441 5445 3a30 3931 3020
3 2 - 1 4 D A T E : 0 9 1 0
0000040 6164 6465 640a
a d d e d \n
0000046
error occurred running util
所以你可以看到字符串比较行
if [ "$cmd_ouput" = " CONDITION: DATE: added" ]; then
似乎没有成功比较字符串,即使它们来自我所看到的相同。
我注意到在原始输入通过 cmdlet 运行 后添加了一个 space。所以为了解决这个问题,我基本上在字符串比较中手动添加了一个 space (我不知道最好的方法,但我 运行 没有想法)
基本上我不确定为什么当字符串相同并且我的 bash 代码看起来正确时字符串比较不起作用?
这个版本似乎可以工作?
这是我的代码。
ctmcontb:
#!/bin/bash
echo " CONDITION: DATE: added"
test2.sh:
#!/bin/bash
cmd_output=$(./ctmcontb -ADD "" "")
expected=" CONDITION: DATE: added"
echo below is the hex output of "CONDITION: DATE: added"
echo "CONDITION: DATE:" | od -xc
echo below is the hex output for the variable cmd_ouput
echo "$cmd_output" | od -xc
echo raw input passed to cmdlet with user arguments inputted and a space added infront
echo "${expected}" | od -xc
if [ "${cmd_ouput}"="${expected}" ]
then
echo successfull ran util
exit 0
else
echo error occurred running util
exit 1
fi
或者,类似这样的东西,基于 return 代码而不是确切的 returned 字符串,看起来更简单并且与 unix 的其余部分更一致,即使它需要更改ctmcontb程序一点:
ctmcontb
#!/bin/bash
if [[ "" -eq 1 ]]; then
exit 0
else
exit 1
fi
test.sh
#!/bin/bash
./ctmcontb ""
result="$?"
if [[ result -eq 0 ]]; then
echo "Success"
exit 0
else
echo "Fail"
exit 1
fi
$ ./test3.sh 2
Fail
$ ./test3.sh 1
Success
$ ./test3.sh 0
Fail
下面是我的简单bash脚本
!/usr/bin/sh
cmd_output=$(ctmcontb -ADD )
echo below is the hex output of "CONDITION: DATE: added"
echo "CONDITION: DATE: added" | od -xc
echo below is the hex output for the variable cmd_ouput
echo "$cmd_output" | od -xc
echo raw input passed to cmdlet with user arguments inputted and a space added infront
echo " CONDITION: DATE: added" | od -xc
if [ "$cmd_ouput" = " CONDITION: DATE: added" ]; then
echo successfull ran util
exit 0
else
echo error occurred running util
exit 1
fi
下面是输出
ctmtest1-tctmsv80 [49] job_late.sh ctmtest1 u350932-14 0910
below is the hex output of CONDITION:u350932-14 DATE:0910 added
0000000 434f 4e44 4954 494f 4e3a 7533 3530 3933
C O N D I T I O N : u 3 5 0 9 3
0000020 322d 3134 2044 4154 453a 3039 3130 2061
2 - 1 4 D A T E : 0 9 1 0 a
0000040 6464 6564 0a00
d d e d \n
0000045
below is the hex output for the variable cmd_ouput
0000000 2043 4f4e 4449 5449 4f4e 3a75 3335 3039
C O N D I T I O N : u 3 5 0 9
0000020 3332 2d31 3420 4441 5445 3a30 3931 3020
3 2 - 1 4 D A T E : 0 9 1 0
0000040 6164 6465 640a
a d d e d \n
0000046
raw input passed to cmdlet with user arguments in putted and a space added in front
0000000 2043 4f4e 4449 5449 4f4e 3a75 3335 3039
C O N D I T I O N : u 3 5 0 9
0000020 3332 2d31 3420 4441 5445 3a30 3931 3020
3 2 - 1 4 D A T E : 0 9 1 0
0000040 6164 6465 640a
a d d e d \n
0000046
error occurred running util
所以你可以看到字符串比较行
if [ "$cmd_ouput" = " CONDITION: DATE: added" ]; then
似乎没有成功比较字符串,即使它们来自我所看到的相同。
我注意到在原始输入通过 cmdlet 运行 后添加了一个 space。所以为了解决这个问题,我基本上在字符串比较中手动添加了一个 space (我不知道最好的方法,但我 运行 没有想法)
基本上我不确定为什么当字符串相同并且我的 bash 代码看起来正确时字符串比较不起作用?
这个版本似乎可以工作?
这是我的代码。
ctmcontb:
#!/bin/bash
echo " CONDITION: DATE: added"
test2.sh:
#!/bin/bash
cmd_output=$(./ctmcontb -ADD "" "")
expected=" CONDITION: DATE: added"
echo below is the hex output of "CONDITION: DATE: added"
echo "CONDITION: DATE:" | od -xc
echo below is the hex output for the variable cmd_ouput
echo "$cmd_output" | od -xc
echo raw input passed to cmdlet with user arguments inputted and a space added infront
echo "${expected}" | od -xc
if [ "${cmd_ouput}"="${expected}" ]
then
echo successfull ran util
exit 0
else
echo error occurred running util
exit 1
fi
或者,类似这样的东西,基于 return 代码而不是确切的 returned 字符串,看起来更简单并且与 unix 的其余部分更一致,即使它需要更改ctmcontb程序一点:
ctmcontb
#!/bin/bash
if [[ "" -eq 1 ]]; then
exit 0
else
exit 1
fi
test.sh
#!/bin/bash
./ctmcontb ""
result="$?"
if [[ result -eq 0 ]]; then
echo "Success"
exit 0
else
echo "Fail"
exit 1
fi
$ ./test3.sh 2
Fail
$ ./test3.sh 1
Success
$ ./test3.sh 0
Fail