Linux Bash脚本:使用转义字符比较if中的字符串

时间:2015-06-25 19:46:59

标签: linux bash

代码:

if [["$MP" -ne "$vl2"]]; then
    echo -e "Comparing Apples to Oranges !!\n" >> ${LOGFILE}
    subject="$0 : Warning : Please check Source Directory"
    SendEmail I "${subject}" "" "${LOGFILE}" "${vel}"
    exit  99
fi

以下是输出:请检查最后一行的错误。谢谢 !

+ PG=./cleanupADir.sh
+ MP=/u01/oracle
+ TH=90
+ MT=3
+ TY=log
+ SD=/u01/oradata/logs
++ expr length /u01/oracle
+ vl1=11
++ expr substr /u01/oradata/logs 1 11
+ vl2=/u01/oradat
+ echo /u01/oradat /u01/oracle
/u01/oradat /u01/oracle
+ '[[/u01/oracle' -ne '/u01/oradat]]'
./cleanupADir.sh: line 73: [[/u01/oracle: No such file or directory

1 个答案:

答案 0 :(得分:3)

[[之后和]]之前添加一些空格,并为字符串更改ne˛至!=

if [[ "$MP" != "$vl2" ]]; then

类似的会议:

$ set -x
$ export x=apple
+ export x=apple
+ x=apple
$ export y=orange
+ export y=orange
+ y=orange
$ if [["$x" -ne "$y"]];then echo "XXX";fi
+ '[[apple' -ne 'orange]]'
bash: [[apple: command not found
if [[ "$x" -ne "$y" ]];then echo "XXX";fi
+ [[ apple -ne orange ]]
if [[ "$x" != "$y" ]];then echo "XXX";fi
+ [[ apple != \o\r\a\n\g\e ]]
+ echo XXX
XXX