Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2006
    Posts
    27

    BASH conditionals: Testing $?


    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

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    hi,

    the proper syntax is
    Code:
    if <command>
    then
       :do stuff
    else
       :do other stuff
    fi
    where <command> may be test, or any other command.

    so
    Code:
    if test -e fileOrDirectory
    then
       echo "fileOrDirectory exists"
    else
       echo "fileOrDirectory doesn't exist"
    fi
    = is lexical comparision,
    -eq is arithmetic comparision.

  3. #3
    Just Joined!
    Join Date
    Oct 2006
    Posts
    27
    Thanks,
    Allow me to clarify my question a bit. I have some longer conditionals involving a few commands, and the result is going to either $? or a variable.
    So I wanted to understand if $? is always a char type, so I need to use bash's lexical compare, or an integer so I need to do an arithmetic compare.
    I guess the source of my confusion is that a program exit(int) takes an integer argument. Then is $? an integer then or is it always a char?
    Similarly, does grep -c return a char (string) or a integer?
    Regards,
    B

  4. #4
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    return code is always an integer 0<=$?<256

  5. #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,283
    Hi.

    The exit status is a character string that is composed of characters representing integers.

    For example, we can create an error situation and look at the exit status:
    Code:
    $ xxxx 
    bash: xxxx: command not found
    $ echo $?
    127
    Now we'll look at the exit status by printing the exit status in octal (octal dump):
    Code:
    $ xxxx
    bash: xxxx: command not found
    $ echo $? | od -bc
    0000000 061 062 067 012
              1   2   7  \n
    The 61, etc. are the ASCII codes for 1 (one), etc.:
    Code:
    $ ascii -t 1 2 7
    3/1   49   0x31   0o61   00110001
    3/2   50   0x32   0o62   00110010
    3/7   55   0x37   0o67   00110111
    So shells emit character strings, and it is up to other programs to convert these to internal form (binary) in order to compare values arithmetically, if that is desired. As watael wrote, there are different operators for string comparison and arithmetic comparison.

    See man pages for details.

    Best wishes ... cheers, drl
    Last edited by drl; 06-26-2013 at 03:43 PM. Reason: Correct 127 ascii.
    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 )

  6. #6
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    Sorry "dri" but his does not resolve the matter. Sure that

    echo $a

    results in ASCII output but this does not necessary have anything to do if $a internally is an integer or a string. BASH is unfortunately a "weak typed language" that internally indeed uses "strings" and "integers". See "Advanced Bash-Scripting Guide" There is a bash statement

    Code:
    declare -i number
    forcing the variable "$number" to be an integer

    Code:
    declare -i number
    number=5
    echo $number | od -bc
    number=a
    echo $number | od -bc
    echo $number results in an ASCII string although it is declare as an integer variable.

    The question is:

    is
    test $a -eq $b
    completely synonymous with

    test $a ==$b ?

    Try

    Code:
    a=3
    b=4
    test $a -eq $b
    catch=$?
    echo '$a -eq $b'
    echo $catch
    test $a == $b
    catch=$?
    echo '$a == $b'
    echo $catch

  7. #7
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    In fact, my test indicates that

    $a==$b

    always can be used while BASH protests when

    $a -eq $b

    is used for $a $b being neither integers nor strings being the ASCII representation of integer numbers

    -eq (and -ne)

    are then really superfluous!

    As opposed to
    -gt etc

  8. #8
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    Another test:

    Code:
    declare -i i1
    declare -i i2
    i1=5
    i2=5
    test $i1 != $i2
    echo $?
    test $i1 -ne $i2
    echo $?
    As "!=" works fine also in this case when with the variables explicitly declared integers it must be considered proven that it never is necessary to use "-ne"

  9. #9
    Just Joined!
    Join Date
    Sep 2013
    Posts
    25
    The following more complete test shows:

    Code:
    declare -i i1
    declare -i i2
    declare -i i3
    i1=16
    i2=5
    echo 'test if $i1 == $i2 works for integer'
    test $i1 == $i2
    echo $?
    echo 'test if $i1 -eq $i2 works for integer'
    test $i1 -eq $i2
    echo $?
    echo 'add two integers'
    i3=$i1+$i2
    echo $i3
    echo 'add two undeclared variables'
    ii1=16
    ii2=5
    ii3=$ii1+$ii2
    echo $ii3
    Output:
    Code:
    mats@linux-7bll:~/bin> int_test
    test if $i1 == $i2 works for integer
    1
    test if $i1 -eq $i2 works for integer
    1
    add two integers
    21
    add two undeclared variables
    16+5
    1
    "==" and "!=" always work, no need for "-eq" and "-ne" (this we already knew!)
    2
    If variables are explicitly declared as integers this affects the result of arithmetic expressions

    I guess:

    If you don't use "declare -i" statements the user can assume that all variables are strings. Possible internal uses of integers are then transparent and of no concern for the users! From the point of view of the user it is then not a "weakly typed language"

  10. #10
    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,283
    Hi.
    Quote Originally Posted by skitgubbe
    BASH is unfortunately a "weak typed language" that internally indeed uses "strings" and "integers". See "Advanced Bash-Scripting Guide"
    Quote Originally Posted by Advanced Bash-Scripting Guide
    Unlike many other programming languages, Bash does not segregate its variables by "type." Essentially, Bash variables are character strings, but, depending on context, Bash permits arithmetic operations and comparisons on variables. The determining factor is whether the value of a variable contains only digits.
    -- from the ABS at Bash Variables Are Untyped

    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
    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 )

Page 1 of 2 1 2 LastLast

Posting Permissions

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