LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-22-2014, 07:40 PM   #1
mrjoli021
LQ Newbie
 
Registered: Jan 2012
Posts: 11

Rep: Reputation: Disabled
script error


I am running the script below and I keep getting
"line 18: [: eq: binary operator expected"

Quote:
#!/bin/bash

host="4.2.2.2"
file="/tmp/ping.txt"

packetsTransmitted=$(head -1 $file | tail -1 | cut -f 1 -d ' ' | tr -d ' ')
packetsReceived=$(head -1 $file | tail -1 | cut -f 4 -d ' ' | tr -d ' ')
packetLoss=$(head -1 $file | tail -1 | cut -f 6 -d ' ' | tr -d ' ')
pingMax=$(head -2 $file | tail -1 | cut -f6 -d'/')
pingAvg=$(head -2 $file | tail -1 | cut -f5 -d'/')


#ping -c 10 $host | tail -2 > $file

echo $packetsReceived
echo $packetsTransmitted

if [ $packetsReceived eq $packetsTransmitted ]; then
echo "packets match"
else
echo "packets are wrong"
echo "packets transmitted is "$packetsTransmitted "and packets recieved is " $packetsRecived
fi
I have checked the variables and when I do an echo on them they print as 10 both of them.
 
Old 04-22-2014, 07:50 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by mrjoli021 View Post
I am running the script below and I keep getting
"line 18: [: eq: binary operator expected"



I have checked the variables and when I do an echo on them they print as 10 both of them.
Try putting a hyphen before the eq.

Also it's good practice to put double quotes around the variables, e.g.

Code:
if [ "$packetsReceived" -eq "$packetsTransmitted" ]; then
For debugging, put a set -x before the offending line, and a set +x after it. This way, the shell will give you some detail about what it's doing.
 
Old 04-22-2014, 09:00 PM   #3
mrjoli021
LQ Newbie
 
Registered: Jan 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks putting the "-" worked.
 
Old 04-23-2014, 01:28 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
When testing numbers in bash you can also use (()) and your normal maths operators:
Code:
(( packetsReceived == packetsTransmitted ))
Please mark as SOLVED once you have a solution
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script.pl with sed shell calls: sh error syntax error near unexpected token `(' MMaddoxx Programming 7 11-24-2011 08:00 AM
bash script read error and awk ouptut error whited Programming 4 10-16-2007 07:05 PM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM
error when tying to run python script(bash error?) shanenin Programming 5 01-10-2006 10:01 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 04:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration