I'm having real trouble getting my script to work, it's about 3 pages long and I'm having trouble with one of my if...then constructs. The code for the sub is as follows:

check_a_b () {
	CHECKA="634274"
	CHECKB="512274"
	if [ CHECKA=CHECKB ]; then
		echo "Match"	
		echo "$CHECKA" >> "$LOGFILE"	
		echo "$CHECKB" >> "$LOGFILE"	
	else
		echo "No match"
		echo "$CHECKA" >> "$LOGFILE"	
		echo "$CHECKB" >> "$LOGFILE"	
	fi
}

I'm running this script under MacOS X Darwin. The script above, when executed, prints "Match" ... although I'm fairly certain that CHECKA and CHECKB are different. If I remove the last space in the IF brackets, ie:

if [ CHECKA=CHECKB ]; then

becomes:

if [ CHECKA=CHECKB]; then

Then it prints "No match", and cannot be coaxed into printing "Match", even when I use:

if [ CHECKA=CHECKA]; then

I'm really confused! :cry:
Thanks in advance ...

Recommended Answers

All 3 Replies

inside the if, you should be comparing $CHECKA and $CHECKB, not CHECKA and CHECKBKeep the space there. Also, try removing the ; and move 'then' to the next line. (not sure if this matters)

if [ CHECKA = CHECKB ]; then

Try this it will work

I would agree to a little bit of both

if [ "$CHECKA" == "CHECKB" ]; then

I'm not sure if the Mac OS underlying *nix requires you to use the $ prefix to expand your variables values.

Best of luck :)

, Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.