4 Replies - 1288 Views - Last Post: 04 July 2011 - 08:04 AM Rate Topic: -----

#1 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

if statement broken bash

Posted 03 July 2011 - 03:01 PM

I have the statement

if [ "$hasDir" == "y" ]


and when i run the script it tells me

[: 165: n: unexpected operator

PS the "n" its referring to is user input

im looking at other code on the internet and everyone else is doing it the same way i am...
Is This A Good Question/Topic? 0
  • +

Replies To: if statement broken bash

#2 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: if statement broken bash

Posted 03 July 2011 - 03:45 PM

sorry guys i fixed it

i was running the BASH script with the SH command

when i ran it usieng the bash command it started working

tnx guys
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: if statement broken bash

Posted 03 July 2011 - 06:07 PM

Note that if you use = instead of ==, it will work in all shells (including dash, which I assume your sh points to).

Allowing == instead of = in conditional expressions is a non-standard extension allowed by bash (among others), but not dash (or the original bourne shell, I suppose).
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5226
  • View blog
  • Posts: 27,000
  • Joined: 10-May 07

Re: if statement broken bash

Posted 03 July 2011 - 06:24 PM

The equals sign is for string conditionals, & the dash is for digit conditional comparison.
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: if statement broken bash

Posted 04 July 2011 - 08:04 AM

View Postno2pencil, on 04 July 2011 - 03:24 AM, said:

The equals sign is for string conditionals, & the dash is for digit conditional comparison.

I think you misunderstood me.

Using bash, the bourne again shell, (and some other shells) you can use == instead of = without problem (though it gains you nothing), while using dash, the Debian Almquist shell, which /bin/sh points to on Debian and Ubuntu systems (as well as other debian-derived systems, I imagine), you can only use = and using == will cause the error which the OP encountered.

Numerical comparisons should be made using -eq etc. instead of = or == and don't figure into this at all.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1