user1918074
user1918074

Reputation: 29

Linux Bash Script : Comparison of strings in if with escape characters

Code :

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

Here is the output : Please check the error on last line. thanks !

+ 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

Upvotes: 0

Views: 2002

Answers (1)

Lajos Veres
Lajos Veres

Reputation: 13725

Add a few spaces after the [[ and before the ]], and for strings change the ne˛to !=:

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

A similar session:

$ 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

Upvotes: 3

Related Questions