0

I am new to bash scripting. My code is :

MK=M
SM=1.4

if [[ $MK == "M" ]]
 then
        if [ $SM -gt 1.3 ]
            then
            echo "Greater than 1.3M"
            else
            echo "Less than 1.3M"
            fi
 else
 echo "Not yet M...."
fi

Reply:

/tmp/tmp.sh: line 6: [: 1.4: integer expression expected
Less than 1.3M

What am i doing wrong ?

0

2 Answers 2

3

It's ultimately because bash is not terribly patient when it comes to floating point numbers. In very simple terms here, I would suggest you do one of two things:

  1. It looks like you are trying to determine if something is larger than 1.3 Mb or not, is this correct? If this is the case, then leave everything the way you have it, and just use Kb for $sm and the compare

    like this:

    #/bin/bash
    
    mk="p"  
    km="p"  
    sm="1400"  
    ms="1300"  
    
    if [[ $mk == $km ]]  
    then  
    if [ $sm > $ms ]  
    then  
    echo "Greater than 1.3M"  
    else  
    echo "Less than 1.3M"  
    fi  
    else  
    echo "Not yet M...."  
    fi
    

    or

  2. Use bc for the calculation of the floating point numbers...

    # /bin/bash
    
    mk="p"
    km="p"
    sm="1.4"
    ms="1.3"
    
    if [ $(echo "$mk == $km" | bc) ]
    then   
    if [ $(echo "$sm > $ms" | bc) ]
    then
    echo "Greater than 1.3M"
    else
    echo "Less than 1.3M"
    fi
    else
    echo "Not yet M...."
    fi
    

One more thing to note here, is that, as you can see from my code, I have primed new variables with the data, rather than using raw letters and numbers in boolean and compare operations, which can have some genuinely unexpected results. Also, while they may work in some conditions, temporarily, bash prefers all variable names to be in lower-case. Let me know if you have any questions.. However, I have tested both code chunks, and they both work fine.

Sign up to request clarification or add additional context in comments.

8 Comments

Oh... Right ... I can do that. Thanks. But Bash is not float friendly ? :(
Bash can absolutely handle floating points. You just need more code, in order to manipulate, compare and do arithmetic. If you need to use the floats, then as soon as I get into the office - which will be in an hour or so - I'll rewrite the code, using bc, which adds a couple of extra lines - and a few additional steps and processes to get your compares done. Let me know.
I would love to learn that, though using kilobytes fulfilled the need. Please if you have free time then do give me the lines about floating numbers in bash... :)
sometime in the next couple of hours I'll get it done for you.. Also, I forgot to mention (and I am sure you have already figured it out) that I didn't actually bother calculating Kb from 1.3Mb and 1.4Mb, I just averaged to 1300 and 1400 Kb respectively, if you really want to be accurate with your compares, you might want to do the maths :)
Bash itself does not handle floats; by contrast, external utilities you can call from Bash may (e.g. bc). > in the context of [ ... ] and [[ ... ]] performs lexical comparison, not numerical. Bash does not care about the case of variables, but it's preferable to stay away from all-uppercase variables to avoid clashes with environment variables and Bash's built-in variables.
|
3

Here's what man bash says:

arg1 OP arg2 ... Arg1 and arg2 may be positive or negative integers.

You seem to be trying to compare floating point numbers.

2 Comments

Can you help me write exact line ?
I would consider using another scripting language; e.g. Python. Floating point arithmetic is beyond the capability of a bash script.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.