LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2010, 12:49 PM   #1
michael.wegemer
LQ Newbie
 
Registered: Oct 2007
Posts: 14

Rep: Reputation: 1
Strange behaviour in bash comparison when variable contains arithmetic operator


I have the following if statement in a bash script

if [ "$PARM2" = "all" ] || [ "$PARM2" eq "$2" ];then
DUMMY="0"
else
return
fi

This works perfectly until $2 contains a value of / (slash). It then fails with "line 61: [: eq: binary operator expected". I really do want to check $2 for a value of slash and I have no idea how to do this.

Can anyone help?
 
Old 10-18-2010, 01:11 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Shouldn't that be -eq instead of eq.......

You also seem to mix ways of testing. Have a look here: ABSG: 7.1. Test Constructs

Hope this helps.
 
Old 10-18-2010, 01:21 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
The -eq test is used to compare integer values. Use (as you did for your first comparison) the = operator to compare string values. Note that bash arguments are strings, so , even if $2 is numeric, the = test should work. Note also that /, by itself, is a string, not an integer.

<edit>
Note also that this question would have been more appropriate tin the "programming" section.
</edit>

Last edited by PTrenholme; 10-18-2010 at 01:23 PM.
 
Old 10-19-2010, 05:22 AM   #4
michael.wegemer
LQ Newbie
 
Registered: Oct 2007
Posts: 14

Original Poster
Rep: Reputation: 1
Smile

Thank you very much for your assistance. I did not even notice that I had mixed = with eq. Changing the eq to = makes all the difference in the world. It works just the way I intended now.

I did not know that the programming section existed. I will look for it next time.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash file comparison operator question gizmola Programming 5 12-23-2009 08:49 AM
strange behaviour when using fprintf within an if comparison itchy8me Programming 20 05-04-2009 06:55 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Strange behaviour in bash - while loop Guttorm Programming 4 06-08-2007 02:11 PM
strange behaviour when bash starts Y0jiMb0 Linux - General 8 07-09-2004 06:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:01 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