Find the answer to your Linux question:
Results 1 to 8 of 8
Well. I've an error when running this: Code: #!/bin/bash MYPASS="leonardo" # This is my password echo "Type your password: " read YPASS if [ "$MYPASS" -eq "$YPASS" ]; then # ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie X.Cyclop's Avatar
    Join Date
    May 2006
    Location
    Israel
    Posts
    143

    What's wrong with this script? -eq


    Well.

    I've an error when running this:
    Code:
    #!/bin/bash
    
    MYPASS="leonardo" # This is my password
    
    echo "Type your password: "
    read YPASS
    
    if [ "$MYPASS" -eq "$YPASS" ]; then # is valid? 
    	echo "Login succesful!" # Welcome
    else
    	echo "Wrong password" # Goodbye
    fi
    ./IfElse: line 12: [: leonardo: integer expression expected

    But, it works fine:
    Code:
    #!/bin/bash
    
    MYPASS="leonardo" # This is my password
    
    echo "Type your password: "
    read YPASS
    
    if [ "$MYPASS" == "$YPASS" ]; then # is valid? 
    	echo "Login succesful!" # Welcome
    else
    	echo "Wrong password" # Goodbye
    fi
    Isn't -eq the same as ==?
    "Don't think about the work, think about the benefit"

    Leonardo Juszkiewicz

  2. #2
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,790
    -eq is an integer expression, so passing strings to it wont work. Not sure to be honest how you go about comparing strings in bash, but from recollection it wont be -eq
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  3. #3
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,744
    Hi !!

    -eq, -lt, -le, -gt, -ge are for integers/numbers..... for strings its '=='








    .
    Last edited by devils casper; 06-09-2007 at 12:46 PM.
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie X.Cyclop's Avatar
    Join Date
    May 2006
    Location
    Israel
    Posts
    143
    Dam*it. It doesn't seem to be in the manual.

    Thanks!
    "Don't think about the work, think about the benefit"

    Leonardo Juszkiewicz

  6. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,311
    Hi.
    Quote Originally Posted by X.Cyclop
    Dam*it. It doesn't seem to be in the manual.
    Code:
           string1 == string2
                  True if the strings are equal.  = may be used in place of == for
                  strict POSIX compliance.
           string1 != string2
                  True if the strings are not equal.
           string1 < string2
                  True if string1 sorts before string2  lexicographically  in  the
                  current locale.
           string1 > string2
                  True  if  string1  sorts  after string2 lexicographically in the
                  current locale.
                  -- excerpt from man bash, CONDITIONAL EXPRESSIONS
    See also http://www.tldp.org/LDP/abs/html/comparison-ops.html ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  7. #6
    Linux Newbie X.Cyclop's Avatar
    Join Date
    May 2006
    Location
    Israel
    Posts
    143
    That happends to me for getting many tutorials/e-books.
    "Don't think about the work, think about the benefit"

    Leonardo Juszkiewicz

  8. #7
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    That's the shell for you. Sometimes you use different syntax to do similar things. I prefer to keep my sanity.

    Code:
    #!/usr/bin/python
    MYPASS="leonardo"
    YPASS = raw_input("Type your password, 0 to quit: ")
    if MYPASS == YPASS :
    	print "Login succesful!"
    elif int(YPASS) == 0: 
    	print "Exiting" 
            .....
    "==" is used for comparison, both strings and numbers. No confusion here.

  9. #8
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    I would like to expand drl's post - just a little bit -- from `man bash`:

    Code:
           string1 == string2
                  True if the strings are equal.  = may be used in place of == for strict POSIX compliance.
           string1 != string2
                  True if the strings are not equal.
           string1 < string2
                  True if string1 sorts before string2 lexicographically in the current locale.
           string1 > string2
                  True if string1 sorts after string2 lexicographically in the current locale.
           arg1 OP arg2
                   OP is one of -eq, -ne, -lt, -le, -gt, or -ge.  These arithmetic binary operators return true if arg1 is equal to, not equal to, less than,
                  less  than or equal to, greater than, or greater than or equal to arg2, respectively.  Arg1 and arg2 may be positive or negative integers.
    (bold and italic emphasis mine)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •