0

I don't know why I am getting this error --> : integer expression expected: [: 82 value 82 comes from CURRENT. I am trying to grab the disk space usage and assign to variable called CURRENT and then email out. I don't know where I am going wrong? Any help is appreciated.

#!/bin/bash
[email protected]
THRESOLD=80
CURRENT=$(df -H | grep '/dev/mapper/cvs-cvs' | awk '{ print $5}' | cut -d'%' -f1)
if  [ $CURRENT -ge $THRESOLD ]; then 
    echo "My CVS disk space usage is $CURRENT %" | mailx -s "Disk Space Usage" $ADMIN
fi
2
  • 2
    Run bash -x script.sh. Commented Aug 4, 2014 at 22:18
  • 1
    set -vx is the shell debugger/verbose mode. Add that before you suspect line of code, then see the values that are substituted from CURRENT and THRESHOLD. Good luck. Commented Aug 4, 2014 at 22:19

3 Answers 3

3

Probably your script is in DOS format that the assignment includes \r The extra value appended to the number causes the error. Try converting it first with either of the following commands:

sed -i 's|\r||' script
dos2unix script
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like it to me. The error message : integer expression expected: [: 82 is a giveaway -- the : integer expression expected part should be at the end of the line, not the beginning, but since the erroneous string is 82\r, it winds up at the beginning instead.
1

It looks like your CURRENT value is not being assigned to an integer from the command substitution output.

I'd recommend running df on the directory you want, then parse the output.

#!/bin/bash
[email protected]
THRESOLD=80
read -ra DF_OUT < <(df -H /dev/mapper/cvs-cvs 2>&1|tail -n 1)
if [[ ${DF_OUT[@]} != "df:"* ]]; then
    CURRENT=${DF_OUT[4]/\%/}
    if [ $CURRENT -ge $THRESOLD ]; then 
        echo "My CVS disk space usage is $CURRENT %" | mailx -s "Disk Space Usage" $ADMIN 
    fi
else
    echo -e "df encountered the following error:\n$DF_OUT"
fi

This first checks for an error on the df output before proceeding to parse the results for disk usage.

As others suggested, using -x with the script helps diagnose things.

2 Comments

What was the error you got? Did you remove the \r characters as @konsolebox suggested in his solution? If your script works as is you can just use that, my solution was just a design difference to verify there weren't any errors from df output.
your script is good but it did not work for me. konsolebox solution work for me. After you write the script, u have to execute sed -i 's|\r||' script.sh and it remove \r and execute fine. thanks for help. I upvoted your answer.
0

try

#!/bin/bash
[email protected]
let "THRESOLD=80"
declare -i CURRENT
let CURRENT=$(df -H | grep '/dev/sda1' | awk '{ print $5}' | cut -d'%' -f1)
if (($CURRENT >= $THRESOLD )) ; then
    echo "My CVS disk space usage is $CURRENT %" | mailx -s "Disk Space Usage" $ADMIN
fi

4 Comments

this is error i got --> sh Disk_Space_Emailer.sh ")syntax error: invalid arithmetic operator (error token is " ': not a valid identifierne 4: declare: `CURRENT ")syntax error: invalid arithmetic operator (error token is " Disk_Space_Emailer.sh: line 6: ((: >= : syntax error: operand expected (error token is ">= ")
what editor are you using to edit your scripts? What OS
sublimetext, which editor do you recommend? Windows 7
? so, if editing on win7 where are you running the bash script? cygwin? Many windows editors use \n\r to terminate lines if you run a bash script with \r you will get un-expected errors and can spend many hours trying to debug. As mentioned above you want to either change settings on the editor to use linux \n line termination and not \n\r. sublimetext.com/docs/file-type-preferences search for defaultline you want 'unix' for bash scripts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.