Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

I am new in scripting, can someone figure out why am i getting this error.

share|improve this question
    
$usep contains no integer. –  Cyrus Nov 14 '14 at 6:12
    
what should i do –  pushpendra Nov 14 '14 at 6:19

2 Answers 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
share|improve this answer

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
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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