Rajeev
Rajeev

Reputation: 46899

shell script comparison of values

In shell script how can i compare these kind of variable

  echo $du   //170G
  echo $expected_du // 40G

   if [ $expected_du -le $du ]
   then
        echo "$du exceeded";
   fi  

Upvotes: 0

Views: 574

Answers (1)

larsks
larsks

Reputation: 311268

Use the -k flag to du, which returns the size in kilobytes without any units. For example:

$ du -ks /tmp
1068    /tmp

Now you have a number that you can compare using -le.

You could also multiple things out in your script:

case $du in
  *K)   du_k=${du%K};;
  *M)   du_k=$(( ${du%M} * 1024 ));;
  *G)   du_k=$(( ${du%G} * 1024 *1024 ));;
  *T)   du_k=$(( ${du%T} * 1024 *1024 * 1024 ));;

  *[0-9])   du_k=$du;;

  *)    echo "What?"
      exit 1
      ;;
esac

echo $du_k

Upvotes: 2

Related Questions