Binary operator expected

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Binary operator expected
# 1  
Old 04-04-2017
Binary operator expected

Hi Team,

I just started to learn shell scripting and i got this script from an online book and tried to run in my terminal. But it throws error message.

Code:
echo $0
-bash

echo $UID
501

cat check_rootuser.sh
#!/bin/bash
# Run as root, of course.
LOG_DIR=/var/log
ROOT_UID=0 # Only users with $UID 0 have root privileges.
LINES=20 # Default number of lines saved.
E_XCD=66 # Can't change directory?
E_NOTROOT=67 # Non−root exit error.

if [ "$UID" −ne "$ROOT_UID" ]
then
echo "Must be root to run this script"
exit $E_NOTROOT
fi


Code:
./check_rootuser.sh
./check_rootuser.sh: line 10: [: −ne: binary operator expected

please help me to solve this error.


Thanks
Selva


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-04-2017 at 07:49 AM.. Reason: Added CODE tags.
# 2  
Old 04-04-2017
Please share your bash version.

While the construct works well with GNU bash, version 4.1.11(1)-release (amd64-portbld-freebsd9.0):
Code:
if [ "$UID" -ne "$ROOTUID" ]; then echo "Must be root to run this script"; fi
Must be root to run this script

, it doesn't with GNU bash, version 4.3.46(1)-release (x86_64-pc-linux-gnu):
Code:
if [ "$UID" −ne "$ROOTUID" ]; then echo "Must be root to run this script"; fi
bash: [: −ne: binary operator expected

Looks like a flaw in that bash version...

Last edited by RudiC; 04-04-2017 at 08:29 AM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-04-2017
Hi,

One thing you could try is using != instead of -ne. As RudiC has said, different versions of Bash can behave differently in this type of situation, depending on what they think they're comparing and what the situation precisely is.
This User Gave Thanks to drysdalk For This Post:
# 4  
Old 04-04-2017
bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

it looks like, it won't work in GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
# 5  
Old 04-04-2017
Hi.

When I looked at your script with cat -A, the if showed up as:
Code:
if [ "$UID" ?ne "$ROOT_UID" ]$

which suggests that the minus sign is not the ASCII code 45 2D -

Try changing that to a minus sign (on my keyboards, the key to the right of 0 [zero] ).

Did you edit this on Windows or some other non-Linux place?

Best wishes ... cheers, drl
These 2 Users Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional binary operator expected Error

Hi, Below is my code where i m trying to grep for name>$servername in manages*.tmp files servername="serv1" set manages*.tmp if ; then However, i get the below error at the if condition: Can you please suggest how can i fix the problem. (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Binary operator expected Error

Here is line number 59 of my script 59 if ; thenI get the following error at this line. Can you please explain why ? (1 Reply)
Discussion started by: mohtashims
1 Replies

3. Shell Programming and Scripting

Help - binary operator expected error

Hello Unix forum. I'm encountering the following error "binary operator expected error" and I cannot seem to solve the issue. I have the following source files to process: CPA_LOOKUP_dat.lst PROFILE_TXN__dat.lst TRANSACTION_CODE_dat.lst PROFILE_TXN_OUT_OF_BALANCE_dat.lst ... (2 Replies)
Discussion started by: pchang
2 Replies

4. Shell Programming and Scripting

Binary Operator expected while executing the below shell script.

Hi Experts, Iam bit poor in shell scripting, Here my requirement is for generating an alert where the oracle database db_recovery_file_dest_size usage. If it reaches beyond 80% should recieve an alert through an email. Want to schedule this alert in cron. #!/bin/bash .... (9 Replies)
Discussion started by: Jagadish m
9 Replies

5. Shell Programming and Scripting

bash script error with binary operator expected.

Hello, I am not sure, where I am missing in the scirpt, I am trying to grep few users from /etc/passwd file and if exists, I added line to echo as user exist, if not create it. #!/bin/bash for vid in v707 z307 z496 z163 z292 ; do if then echo " $vid User exists " else ... (2 Replies)
Discussion started by: bobby320
2 Replies

6. Linux

Binary operator expected

hi i'm trying to do program that counts the total no of words from files from a directory and all it's subdirectories.ang i get the binary operator expected error at line 7 and line 12.can you please help me with this as quick as possible? if test -d $1 then sum=0 for name in $1/* do... (2 Replies)
Discussion started by: marian
2 Replies

7. UNIX for Dummies Questions & Answers

[: =: unary operator expected

HI, while running a script, I got the below mentioned error. /bin/sh: line10 : supportedMozillaVersion() { case "$*" in *rv:1.*) return 0;; *rv:.*) return 0;; *rv:*) return 1;; Mozilla\ 1.*) return 0;; Mozilla\ .*) return 0;; *) return 1;; esac } supportedFirefoxVersion() { case... (7 Replies)
Discussion started by: bhag281
7 Replies

8. Shell Programming and Scripting

unary operator expected

hi i am trying to compare a value with value 50. but i am getting " I am using if then echo "------------" fi please help thanks in advance Satya (2 Replies)
Discussion started by: Satyak
2 Replies

9. Shell Programming and Scripting

Binary operator expected

Within my script, there is this following if, then statement. It basically looks for files in a directory that match a certain naming convention (bingofile*.DAT) and are non empty files and moves these files to a diff. directory. The script works okay if there is only one file matching the search... (4 Replies)
Discussion started by: basisvasis
4 Replies

10. Shell Programming and Scripting

binary operator expected error

It is erroring for : binary operator expected on the if line. Any suggestions? Thanks in advence. (7 Replies)
Discussion started by: apps_user
7 Replies
Login or Register to Ask a Question