0

I have following script to check disk usage

    #!/bin/bash

# set alert level 90% is default
ALERT=10

OIFS=$IFS
IFS=','

storage=$(df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }')



for output in $storage ;

do
  echo "---------------@@@@@@@@@ output started @@@@@@@@@@@@@@@@-----------"
  echo $output
  echo "---------------@@@@@@@@@ output end @@@@@@@@@@@@@@@@-----------"

  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  echo "---------------###### useo started ######-----------"
  echo $usep
  echo "---------------###### usep end ######-----------"

  if [ $usep -ge $ALERT ]; then

    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" 
  fi
done

But when i am running this code i am getting the integer expression expected error at if conditional statement, Here is the output of this script

  97% /dev/sda1
1% udev
0% none
2% none
---------------@@@@@@@@@ output end @@@@@@@@@@@@@@@@-----------
---------------###### useo started ######-----------
97
1
0
2
---------------###### usep end ######-----------
./fordiskfor.sh: line 24: [: 97
1
0
2: integer expression expected
  • $usep contains no integer. – Cyrus Nov 14 '14 at 6:12
  • what should i do – pushpendra chauhan Nov 14 '14 at 6:19
2

The problem is there:

if [ $usep -ge $ALERT ]; then
  ...
fi

$usep contains multiple lines of digits. To cycle trough all of them use somthing like this instead of that part:

for $space in $usep;
do
  if [ $space -ge $ALERT ]; then
    echo "Running out of space..."
  fi
done
0

The value stored in the variable $storage comprises of multiple lines. As a result, $output will also contain multiple lines, and so will $usep.

You can extract and compare all values stored in $usep, one by one, using another for loop as mentioned in this answer. Or you can use while statement as follows:

echo $storage | while read output
do      
    ...
    ...

    if [ $usep -ge $ALERT ]; then    
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)"
  fi
done
  • Is this anything other than a repost of the question? – jasonwryan Nov 14 '14 at 7:06
  • I used while, whereas question contains for loop. – Mandar Shinde Nov 14 '14 at 7:37
  • Well rather than reprinting the whole script, perhaps just the relevent line and an explanation of why yuo think that is a better approach... – jasonwryan Nov 14 '14 at 7:40
  • I am very poor at explaining things. You could edit my answer if you wish to. – Mandar Shinde Nov 14 '14 at 7:42
  • @jasonwryan - I've tried to make my answer acceptable to you. Hope this answer makes you happy. – Mandar Shinde Nov 14 '14 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.