[: =: unary operator expected error

Why am I getting this error.... [: =: unary operator expected error
Thanks in advance.
giving me an error on the bolded lines

#!/bin/sh
# iOS-Ad-Remover
# Marshall Ford @ marshallbford@gmail.com
# This project is hosted @ http://ios-ad-
# remover.sourceforge.net
# Under the GNU GPL open source license


clear
echo
if [ `id -u` != 0 ]; then echo "You need to be root to run this script."; exit 0; # required by reboot command
echo
fi
set -xv
if [ -f "/var/mobile/iOS_Ad_Remover-`date +%d%m%y`.log" ]; then
rm iOS_Ad_Remover-`date +%d%m%y`.log
fi

logname=iOS_Ad_Remover-`date +%d%m%y`.log
echo "iOS-Ad-Remover Log File" >> $logname
datetime=`date +%c_%p`
echo "$datetime" >> $logname
deviceinfo="Device: "`deviceinfo -p`
echo "$deviceinfo" >> $logname

if [ -f "/var/lib/dpkg/info/com.ericasadun.utilities.list" ]; then # Erica Utilities check
:
else
echo "Erica Utilities not installed" >> $logname
echo "Error: You need to install Erica Utilities from Cydia"
sleep 5
exit
fi

if [ -d "/Applications/iFile.app" ]; then # iFile check
:
else
echo "Error: iFile not installed" >> $logname
echo "You need to install iFile from Cydia"
sleep 5
exit
fi

echo "Erica Utilities and iFile installed" >> $logname

if [ -f "/var/mobile/adremover.sh" ]; then
:
else
echo "This script is not called adremover.sh or is not located at /var/mobile/adremover.sh"
echo "Error: Script name or location not correct" >> $logname
fi
date # Start intro
echo
echo "Welcome to iOS-Ad-Remover Beta 1.0
"
sleep 3
echo "This script is similar to Adblock in that it will remove ads from most apps, some websites, and in Cydia.
"
sleep 5
echo "Click RETURN when you want to continue
"
read
echo "You will need to meet the following requirements...
"
read
echo "1. iFile and Erica Utilities installed and updated
"
read
echo "2. This script must be located @ /var/mobile
"
read
echo "3. This script will perform a reboot, you must have an untethered jailbreak or have access to a computer to power back on.
"
sleep 3
echo # end intro

echo "Have you completed the requirements and still want to continue?
"
echo "y/n?
"
read answer
if [ $answer = "y" ]; then
clear
# download host file
echo "Do you have the latest host file downloaded and located in /var/mobile/Media/Downloads?
"
echo "y/n?
"
read hosta
if [ $hosta = "y" ]; then
:
if [ -f "/var/mobile/Media/Downloads/fhosts" ]; then
:
else
echo "The host file does not exist"
echo "Error: The host file did not exist" >> $logname
sleep 3
openURL http://sourceforge.net/projects/ios-ad-remover/files/iOS-Ad-Remover/
exit
fi
fi
if [ $hosta = "n" ]; then
echo "User sent to download host file" >> $logname
openURL http://sourceforge.net/projects/ios-ad-remover/files/iOS-Ad-Remover/
exit
fi

echo # backup process
sleep 1
echo
echo "Backing-up"
sleep 3 
cd ..
cd ..
cd var/mobile/Media/Downloads
if [ -d "/var/mobile/Media/Downloads/Backup" ]; then
:
else
mkdir Backup
fi

cd ..
cd ..
cd ..
cd ..
cd etc
cp fhosts /var/mobile/Media/Downloads/Backup
cd /var/mobile/Media/Downloads/Backup
cp fhosts $datetime-backup-fhosts # rename process
rm fhosts # rename process
echo
echo "Done"
echo
echo "Old host file backed-up" >> $logname # backup done
sleep 1
echo "Changing the host file..."
sleep 3
cd ..
cd ..
cd ..
cd ..
cd ..
cd etc
rm fhosts
cd ..
cd /var/mobile/Media/Downloads
cp fhosts /etc
cd ..
cd /var/mobile/Media/Downloads
rm fhosts
echo
echo "You have successfully removed the majority of ads on iOS.
"
echo "You have successfully removed the majority of ads on iOS" >> $logname
sleep 3
echo "If you find an app that does not function"
echo "because of the ad-removal please contact me at marshallbford@gmail.com"
read
echo "Reboot performed" >> $logname
reboot # needed to insure host file install
fi

if [ $answer = "n" ]; then

clear
echo "iOS-Ad-Remover was stopped by the user" >> $logname
echo "iOS-Ad-Remover was stopped by the user."
sleep 3
echo
echo "Good Bye"
sleep 3
fi

Try to quote all occurrences of the following variable de-references:

 [ $answer = "y" ]

should be:

[ "$answer" = "y" ]

and:

[ $hosta = "y" ]

should be:

[ "$hosta" = "y" ]

Does not work, it just ignores the if then statements when i add the quotes.

Try by changing all occurences to

[[ $answer = "y" ]]

instead of

 
[ $answer = "y" ]

Nope this does not work, everything was working fine until I came across this error, basically the script ends at the line

echo "Have you completed the requirements
and still want to continue?"
echo
echo "y/n?"

It just stops.
I am running this on my iphone? Would that make a difference?
Thanks for all the feedback.

Ignoring the iPhone factor and viewing this just as a Shell problem.

You have places where an "echo" has become two lines when it should be one line.

echo "You will need to meet
the following requirements..."

echo "2. This script must be located
@ /var/mobile"

echo "Have you completed the requirements
and still want to continue?"

Each of the three examples above needs the linefeed removed from the end of the first line.

Check carefully against the original code in case you have more paste errors.

thanks for the pointer but I would like to fix the bug before I focus on non code breaking things.

Again, I would move this to the urgent forum, but I can't afford to. In my opinion it is urgent because It is hindering me from continuing to learn new things and then applying them to my script.
So if anyone could help me out, I would gladly give some bits ( the little a do have).
:slight_smile:

I see you (or someone else that uses the same nick) have a project on Sourceforge with that script.

First I would like to ask, are you sure this is legal? I mean removing ads from apps?

try:

[[ "$answer" == "y" ]]

@mbf123
Please re-read my post. It is what has broken your script. You need to fix those places first.

On the same theme I would change

if [ `id -u` != 0 ]

to

if [ "`id -u`" -ne 0 ]

just to make sure...
Another useful technique may be to add:
"set -xv" near the top somewhere, and re-run the script. May tell you which test is failing.

I am a little confused by what you mean. Could you give me an example fix? Also I updated the code, does it look ok?
Btw, the code worked until recently, so I know the if statements don't require quotes or anything.

---------- Post updated at 05:39 PM ---------- Previous update was at 05:37 PM ----------

Nope that project is NOT MINE. :cool::smiley:

But I did hear that it is legal since Source Forge checks the project out, before accepting the program into their website. They let him in!

Well, in this case I would first ask the project owner for support :slight_smile:

Stupid question. Did you type "y" or "n" (then <enter>) at this point?

it still gives an error when a add a "$" on either side of the read variable, my if statements have been working without them too, just now it gives me an error.

Sorry the $ signs were from my editor. I have removed them from the post.

What I was wondering was whether the script asked the question and whether you could reply with "y" or "n" .

currently, I get an error before I get a chance to say yes or no. The script stops and gives me an error at the line

echo "Have you completed the requirements and still want to continue?"
echo
echo "y/n?"

As soon as it dislplays' y/n, it stops and gives me the error

./ad2.sh: line 66: [: = unary operator expected
./ad2.sh: line 144: [: = unary operator expected 

Please post the current version of the script.
We assume that you have now sorted out the multi-line "echo" statements (which do not work in every Shell) and of course the quotes in:
if [ "$variable" = "something" ]
statements which stops the condition being erroneously treated as a numeric compare.

Btw. This will be a lot quicker if you post the exact script, what you typed, and exactly how the script replied verbatim.

I updated it awhile ago, check back on page 2, i told you i updated it. Secondly the echo issue does not need to be addressed because there was a point where the script worked with those statements.
Thirdly, i am not quoting the if statements becuase it was working before.
Also othr iphone scripts do not use the quotes.

There must be another issue.

---------- Post updated at 07:34 PM ---------- Previous update was at 12:35 AM ----------

Can anyone help me? I am getting kind of depressed...