test with "$integer"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coaxfiber
    New Member
    • Mar 2007
    • 60

    test with "$integer"

    why is it that when i used the value with zero,comparing two integers,it seems that i got the wrong ouput?

    Please consider the ff:

    #!/bin/ksh
    echo "6391800000 9" > k
    cat k
    read x < k
    (( aj=63918000009 ))
    if
    [ ! -d "$x" = "$aj" ]
    then
    echo "$aj"
    elif
    [ "$k" = "$aa" ]
    then
    echo "$aa"
    else
    echo "done"
    fi
    exit 99
    --------------
    OUTPUT:
    63918000009
    -506509431

    ********but if:***** consider the 000009 that i removed from the first script.

    #!/bin/ksh
    echo "63918" > k
    cat k
    read x < k
    (( aj=63918 ))
    (( aa=12346 ))
    if
    [ ! -d "$x" = "$aj" ]
    then
    echo "$aj"
    elif
    [ "$k" = "$aa" ]
    then
    echo "$aa"
    else
    echo "done"
    fi
    exit 99
    -----------
    OUPUT:
    63918
    63918

    Thanks..
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    In the first $k and $aa are both unitialized. In the second, only $k is.

    Comment

    • coaxfiber
      New Member
      • Mar 2007
      • 60

      #3
      Originally posted by Motoma
      In the first $k and $aa are both unitialized. In the second, only $k is.

      Hi Sir,


      Don't Know exactly what you mean.but somehow i just figured it out. i'l give it a try.thanks..

      Comment

      • coaxfiber
        New Member
        • Mar 2007
        • 60

        #4
        This his how my question is about:

        #!/bin/ksh
        echo "1234567890 " > k
        cat k
        read x < k
        (( aj=1234567890 ))

        if
        [ ! -d "$x" = "$aj" ]
        then
        echo "$aj"

        else
        echo "done"
        fi
        exit 99
        -----------
        OUPUT:
        1234567890


        but when i added any number to my input "1234567890 1" which becomes 11 numbers it seems that the output doesn't print for any input of more than 10 numbers.

        ---------------
        OUTPUT:
        done

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          I am not entirely familiar with ksh scripting, but when I try and load up the last script you posted, I got an error on the line that reads:

          [ ! -d "$x" = "$aj" ]

          What are you trying to test for with this line?

          Comment

          • coaxfiber
            New Member
            • Mar 2007
            • 60

            #6
            Originally posted by Motoma
            I am not entirely familiar with ksh scripting, but when I try and load up the last script you posted, I got an error on the line that reads:

            [ ! -d "$x" = "$aj" ]

            What are you trying to test for with this line?

            Maybe you can remove the "! -d" from the syntax.

            [ "$x" = "$aj" ]

            --------------------------------------------------
            I guess I'm having problem with zero (0).

            sometimes when I have to compare it with the value 43918706 ,it doesn't give me true value. what i need to do is to cut 439187 and then cut 6 and then paste the result so i have the value 4391876, which doesn't have any zero.

            Please consider the following. let say you have to input your value.


            echo "input: \c" ----> my input is 4391876
            read a
            let x=4391876

            if
            [ "$a" = "$x" ]
            then
            echo "$x"
            else
            echo "wrong"
            fi

            OUTPUT
            -------------
            4391876

            CASE #2:

            echo "input: \c" ----> my input is 43918706
            read a
            let x=43918706

            if
            [ "$a" = "$x" ]
            then
            echo "$x"
            else
            echo "wrong"
            fi

            OUTPUT
            -------------
            wrong

            Comment

            • Motoma
              Recognized Expert Specialist
              • Jan 2007
              • 3236

              #7
              if you are sure that your input is going to be numbers, just use the -eq comparison.

              [ $x -eq $aj ]

              Comment

              • coaxfiber
                New Member
                • Mar 2007
                • 60

                #8
                Originally posted by Motoma
                if you are sure that your input is going to be numbers, just use the -eq comparison.

                [ $x -eq $aj ]
                #!/bin/ksh
                echo "input: \c"
                read a
                let x=6391800000706

                if
                [ "$a" -eq "$x" ]
                then
                echo "$x"
                else
                echo "wrong"
                fi

                *Try to use input ==>639180000070 6
                OUTPUT:
                ----------------
                888664258

                Comment

                • Motoma
                  Recognized Expert Specialist
                  • Jan 2007
                  • 3236

                  #9
                  Load up this script I wrote. Hopefully you will see where the error is occurring.

                  Code:
                  #!/bin/ksh
                  echo "input: \c"
                  read a
                  let x="6391800000706"
                  
                  echo "a = $a; x = $x"
                  
                  if
                  [ $a -eq $x ]
                  then
                  echo "Shell script comparison passed."
                  else
                  echo "Shell script comparison failed."
                  fi
                  
                  if
                  [ $(echo "$a == $x" | bc ) -eq 1 ]
                  then
                  echo "bc variable comparison passed."
                  else
                  echo "bc variable comparison failed."
                  fi
                  
                  if
                  [ $(echo "$a == 6391800000706" | bc ) -eq 1 ]
                  then
                  echo "bc numerical comparison passed."
                  else
                  echo "bc numerical comparison failed."
                  fi

                  Comment

                  • coaxfiber
                    New Member
                    • Mar 2007
                    • 60

                    #10
                    Originally posted by Motoma
                    Load up this script I wrote. Hopefully you will see where the error is occurring.

                    Code:
                    #!/bin/ksh
                    echo "input: \c"
                    read a
                    let x="6391800000706"
                    
                    echo "a = $a; x = $x"
                    
                    if
                    [ $a -eq $x ]
                    then
                    echo "Shell script comparison passed."
                    else
                    echo "Shell script comparison failed."
                    fi
                    
                    if
                    [ $(echo "$a == $x" | bc ) -eq 1 ]
                    then
                    echo "bc variable comparison passed."
                    else
                    echo "bc variable comparison failed."
                    fi
                    
                    if
                    [ $(echo "$a == 6391800000706" | bc ) -eq 1 ]
                    then
                    echo "bc numerical comparison passed."
                    else
                    echo "bc numerical comparison failed."
                    fi
                    ---------------------------------------
                    Zero "0" is very sensitive in unix, so I thought about this while doing my script.:


                    echo "input: \c"
                    read a

                    if
                    [ $a = ' 6391800000706' ]
                    then
                    echo "6391800000 706"
                    else
                    echo "wrong"
                    fi

                    Comment

                    Working...