Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 20 of 20
OK, that is a difference! But neither a human nor some (sensible) software would normally set the string to "02" instead to "2". But sure, with "-ne" and "-eq" you ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25

    OK, that is a difference! But neither a human nor some (sensible) software would normally set the string to "02" instead to "2". But sure, with "-ne" and "-eq" you are OK in all cases!

  2. #12
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    Quote Originally Posted by drl View Post
    -- from your reference with title "the Bash Variables Are Untyped"
    Is this the official BASH reference? Looking at the code and the comments provided in this guide I do not think this is a masterpiece of clarity!

    First of all:

    • A string is a sequence of ASCII characters (8 bits pro character)
    • An integer is a sequence of (generally) 32 bits representing an integer in binary format


    Please do not call a string consisting of the ASCII characters 0,1,2,...,9 an integer as appearently is done in the code-comments of this guide! Call it something else like a string with characters representing an integer in decimal notation(or something shorter)! A clear statement would be

    • in BASH there are only variables of type string
    • for a character string representing an integer in decimal notation BASH provides the comparisons -ne and -eq etc and also arithmetic functions (addition, subtraction)


    But is this precise and sensible statement true? I would like to read this from an official guide!

  3. #13
    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,305
    Hi, skitgubbe.

    Thank you for the time and effort you are spending to make descriptions clear and precise.

    I think the ABS is clear (for me, anyway) about variables in bash, especially where examples are provided. Seeing myself as pragmatic, where I find those examples not sufficient, I can create my own examples, and that is an additional aid in learning how things work.

    As you are quite passionate about this, perhaps contributing to The Linux Documentation Project, particularly in the area of the ABS would provide you with satisfaction as well as help those who, like you, desire that clarity and precision.

    Of course, you could write your own explanations separately, say on a blog if it were not convenient to invest your time in working on TLDP.

    Best wishes ... 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 )

  4. $spacer_open
    $spacer_close
  5. #14
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    Quote Originally Posted by drl View Post
    When comparing a value such as "2" against "02", you may see the utility of comparison operators "-eq", "-ne", as opposed to the string comparison operators.
    Best wishes ... cheers, drl
    Sounds convincing! But what about this test:

    Script:

    Code:
    a=3
    b=03
    echo '$a = ' $a
    echo '$b = ' $b
    #
    echo '$a == $b'
    test $a == $b
    echo '$? = ' $?
    #
    echo '$a -eq -$b'
    test $a -eq -$b
    echo '$? = ' $?
    #
    echo '$a -ge -$b'
    test $a -ge -$b
    echo '$? = ' $?
    #
    echo '$a -gt -$b'
    test $a -gt -$b
    echo '$? = ' $?
    #
    echo '$a -le -$b'
    test $a -le -$b
    echo '$? = ' $?
    #
    echo '$a -lt -$b'
    test $a -le -$b
    echo '$? = ' $?
    Result:

    Code:
    $a =  3
    $b =  03
    $a == $b
    $? =  1
    $a -eq -$b
    $? =  1
    $a -ge -$b
    $? =  0
    $a -gt -$b
    $? =  0
    $a -le -$b
    $? =  1
    $a -lt -$b
    $? =  1
    Also with the supposedly numerical comparisons "-eq" "-ne" "-gt" "-ge" "-le" "-lt" BASH is of the opinion that "3" is larger than "03"!

  6. #15
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    Another (more?) interesting test:

    Script:

    Code:
    a=23
    b=3
    echo '$a = ' $a
    echo '$b = ' $b
    #
    echo '$a > $b'
    test $a > $b
    echo '$? = ' $?
    #
    echo '$a -gt $b'
    test $a -gt $b
    echo '$? = ' $?
    Result:

    Code:
    $a =  23
    $b =  3
    $a > $b
    $? =  0
    $a -gt $b
    $? =  0
    Also with ">" the value "3" is considered smaller than "23"! But this is not lexicographic sorting as "2" is less than "3".

    Using the "sort" command one gets:

    Code:
    linux-7bll:~/bin> sort
    3
    23
    23
    3
    "sort" sorts by default lexicographically and puts "23" first and "3" behind as expected!
    Last edited by skitgubbe; 09-25-2013 at 10:21 AM. Reason: Spelling mistake

  7. #16
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    "<" and ">" must be escaped! Redo the test above!

    Code:
    a=3
    b=24
    echo '$a = ' $a
    echo '$b = ' $b
    #
    echo '$a \> $b'
    test $a \> $b
    echo '$? = ' $?
    #
    echo '$a -gt $b'
    test $a -gt $b
    echo '$? = ' $?
    #
    echo '$a \< $b'
    test $a \< $b
    echo '$? = ' $?
    #
    echo '$a -lt $b'
    test $a -lt $b
    echo '$? = ' $?
    Result

    Code:
    $a =  3
    $b =  24
    $a \> $b
    $? =  0
    $a -gt $b
    $? =  1
    $a \< $b
    $? =  1
    $a -lt $b
    $? =  0
    Then one does indeed get the lexicographical order! Sorry for my mistake but this with the escaping of special characters is a really tricky thing with the shell that really needs to be pointed out!

  8. #17
    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,305
    Hi.
    Quote Originally Posted by skitgubbe
    Then one does indeed get the lexicographical order! Sorry for my mistake but this with the escaping of special characters is a really tricky thing with the shell that really needs to be pointed out!
    Thank you for your apology! This is pointed out! See the ABS!
    Code:
    <
    
        is less than, in ASCII alphabetical order
    
        if [[ "$a" < "$b" ]]
    
        if [ "$a" \< "$b" ]
    
        Note that the "<" needs to be escaped within a [ ] construct.
    -- Other Comparison Operators

    Use the man pages! Use the supporting documentation! Use Google! Use The Force! Use normal punctuation!

    The basic syntax and semantics of the constructs of the Bourne shell and descendants have not changed much since 1977. There certainly have been additions (e.g. (( ... )), [[ ... ]], declare, etc.), but the meanings of the returns from test (i.e. 0 is true, 1 is false), the redirection operator >, and other basics remain the same as far as I am aware. As with any complex software product, there are probably some errors, but with millions of lines of code using the shells bash, ksh, zsh, etc., egregious errors seem very unlikely to me. In other words, if one thinks there is an error in the shell, it is most often with one's understanding or construction, or both.

    And, speaking of unlikely, I am unlikely to continue replying in this thread.

    Best wishes .. 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 )

  9. #18
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    I just detected that for my first post there was an undetected "-" character

    Code:
    test $a -eq -$b
    Removing this character one gets as should be:

    Code:
    $a =  3
    $b =  03
    $a == $b
    $? =  1
    $a -eq $b
    $? =  0
    $a -ge $b
    $? =  0
    $a -gt $b
    $? =  1
    $a -le $b
    $? =  0
    $a -lt $b
    $? =  1
    Everything OK! And I think it was a rather useful discussion, not only for me. All variables in BASH are strings, some can be interpreted as integer values in decimal notation and for these the "numeric comparisons" are available.
    Last edited by skitgubbe; 09-25-2013 at 08:07 PM. Reason: Typing error in script

  10. #19
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    Quote Originally Posted by bandeg View Post
    Hello:

    Here are some easy but not so useful examples of my questions.

    1. What is the proper way to have a conditional using the $? result? Is this always a string? or is it ever an integer?

    i.e.,

    test -e passwd
    if [ "$?" = "0" ]
    then
    echo "Got it"
    fi

    or

    if [ "$?" -eq 0 ]

    2. Does grep -c return an integer or a string.
    val=$(ls -al | grep -c passwd)

    if [ "$val" = "1" ]

    .. OR ..

    if [ "$val" -eq 1 ]

    Thanks.

    B
    This was the start of the thread! The answer is:

    The tests "-eq" and "==" (or "=") are absolutely equivalent as long as the string with a numerical value (in decimal representation!) does not have a leading zero (like for example "0071"). The string "$?" is either "0" or "1", i.e. it has no leading zeros!

    There is also no reason to type "1" instead of 1 or "$val" instead of $val. In BASH the apostrophes are not used to signify strings (as nothing else exists) but is used to protect special characters (and blanks) from the shell
    Last edited by skitgubbe; 09-26-2013 at 08:11 AM.

  11. #20
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    Just a final? remark about the numeric comparisons and the "lexicographical" comparisons! The difference is the same as for the "sort" command with or without option "-n"

    Code:
    mats@linux-7bll:~> test -3 -lt -2
    mats@linux-7bll:~> echo $?
    0
    mats@linux-7bll:~> test -3 \< -2
    mats@linux-7bll:~> echo $?
    1
    mats@linux-7bll:~>
    The second test results in "false" (1) because 3 is larger than 2

    Similar comparing for example 3 and 24. 24 is larger than 3 although 2 is less than 3
    Last edited by skitgubbe; 10-02-2013 at 02:07 PM.

Page 2 of 2 FirstFirst 1 2

Posting Permissions

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