Find the answer to your Linux question:
Results 1 to 6 of 6
i can not understand why the command after else never executed even if the size is less than num #! /bin/bash num="63%" size=$(df -h |grep home|awk -F " " '{print ...
  1. #1
    Just Joined!
    Join Date
    Sep 2009
    Posts
    4

    script problem

    i can not understand why the command after else never executed even if the size is less than num

    #! /bin/bash

    num="63%"
    size=$(df -h |grep home|awk -F " " '{print $5}')

    if [ "$size">="$num" ];then
    echo $size
    echo "toto"
    else
    echo "sorry"
    echo $num
    fi;


    please help me

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    $size and $num are string values and are not friendly to arithmetics. You must strip the '%' sign from them.

    Use ' -gt ' instead instead of '>=' for integer comparison.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Just Joined!
    Join Date
    Sep 2009
    Posts
    4
    but you must know that the resuld of size is in percentage

    size=$(df -h |grep home|awk -F " " '{print $5}')
    (this command gives the result in %)

    so how can i compare those two percentage values?

  4. #4
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    You must strip the '%' sign
    Code:
    ...
    num=62
    ...
    size=${size%'%'} #HERE YOU KEEP EVERYTHING EXCEPT THE LAST CHARACTER
    ...
    if [ $size -gt $num ]; then
    ...
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  5. #5
    Just Joined!
    Join Date
    Sep 2009
    Posts
    4
    thanks a lot ,every thing is ok. now can i have your mail in order to keep in touch

  6. #6
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Loooooooooool !
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

Posting Permissions

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