0

I want to refer with this question to the following post where I got the Skript: Post with Script

I'm using Bash 4.1.2(1) and when I try to execute the script, I get following error messages:

 ./check_disk_by_size.sh: Zeile 9: [: ==: Einstelliger (unärer) Operator erwartet. (standard_in) 1: syntax error ./check_disk_by_size.sh: Zeile 13: [: -eq: Einstelliger (unärer) Operator erwartet. (standard_in) 1: syntax error (standard_in) 1: syntax error ./check_disk_by_size.sh: Zeile 16: [: Zu viele Argumente. (standard_in) 1: syntax error ./check_disk_by_size.sh: Zeile 19: [: -eq: Einstelliger (unärer) Operator erwartet. 

I have basic Bash-scripting experiences but whenever I change something, I get even more errors. I'm not able to bring it to work.

EDIT 1: Here's the script

#!/bin/bash FREESPACE=`/usr/local/nagios/libexec/check_nt -H $2 -p 12489 -s PASSWORD \ -v USEDDISKSPACE -l $4 | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $1 }'` SIZE=`echo $FREESPACE | awk '{ print $2 }'` UNIT=`echo $FREESPACE | awk '{ print $3 }'` if [ $UNIT == "Gb" ]; then SIZE=`echo $SIZE \* 1024 | bc` fi if [ `echo "$SIZE >= $6" | bc` -eq 1 ]; then echo "$4:\_Drive_Space OK - $FREESPACE" exit 0 elif [ `echo "$SIZE < $6" | bc` -eq 1 -a `echo "$SIZE > $8" | bc` -eq 1 ]; then echo "$4:\_Drive_Space WARNING - $FREESPACE" exit 1 elif [ `echo "$SIZE <= $8" | bc` -eq 1 ]; then echo "$4:\_Drive_Space CRITICAL - $FREESPACE" exit 2 fi 

I run the script like this:

./check_disk_by_size -H [IP_Adress] -l [DRIVE] -w [INTEGER] -c [INTEGER] -p [PORT] -s [PASSWORD] 

Example:

./check_disk_by_size -H 192.168.1.110 -l c -w 10240 -c 8192 -p 12489 -s PASSWORD 

UPDATE

Thanks for your help! It works now :) The script looks like this now:

#!/bin/bash # FREESPACE=`/usr/local/nagios/libexec/check_nt -H $2 -p 12489 -s Nag4AlphA \ -v USEDDISKSPACE -l $4 | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $1 }'` SIZE=`echo $FREESPACE | awk '{ print $2 }'` UNIT=`echo $FREESPACE | awk '{ print $3 }'` if [ "$UNIT" == "Gb" ]; then SIZE="$(echo "$(($SIZE * 1024))" | bc)" fi echo $SIZE echo " " echo $6 echo " " echo $8 if [ $SIZE -ge $6 ]; then echo "$4:\_Drive_Space OK - $FREESPACE" exit 0 elif [ $SIZE -lt $6 -a $SIZE -gt $8 ]; then echo "$4:\_Drive_Space WARNING - $FREESPACE" exit 1 elif [ $SIZE -lt $8 ]; then echo "$4:\_Drive_Space CRITICAL - $FREESPACE" exit 2 fi 
6
  • Are you using #!/bin/bash or #!/bin/sh in your script? Commented Apr 20, 2015 at 12:00
  • Seems somewhere in your script you are trying to compare values with different types (e.g String vs Intgeger). Hard to say while we cannot see your script and without knowing what it is supposed to do... Commented Apr 20, 2015 at 12:04
  • Thanks for the replies! I edited the post with the script. Commented Apr 20, 2015 at 12:33
  • What's in your FREESPACE variable? Execute it manually or echo it before you get the SIZE and UNIT out of it. There is also another version of the script in the thread you posted. Commented Apr 20, 2015 at 12:51
  • Try to run your script with bash -x check_disk_by_size arguments so you can have a small debug on how it is performing. Commented Apr 20, 2015 at 13:12

1 Answer 1

4

To me it looks like the left operand in the "if test" is empty.

About why there might be an empty value, I suggest you to run the Nagios command from the shell and see what it does. What's its output? Run all the steps to the output:

  1. /usr/local/nagios/libexec/check_nt -H $2 -p 12489 -s PASSWORD -v USEDDISKSPACE -l $4

  2. /usr/local/nagios/libexec/check_nt -H $2 -p 12489 -s PASSWORD -v USEDDISKSPACE -l $4 | awk -F"- " '{ print $4 }'

  3. /usr/local/nagios/libexec/check_nt -H $2 -p 12489 -s PASSWORD -v USEDDISKSPACE -l $4 | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $1 }'

About the script itself I "kindly suggest" to:

  • ALWAYS enclose variables in double quotes, EVERYWHERE
  • ALWAYS prefer $() instead of the backticks for shell expansion, EVERYWHERE
  • ALWAYS enclose shell expansion in double quotes, EVERYWHERE

Of course seldom exceptions do apply, like when you test numeric values.

I don't mean to be rude, just make sure you understand how important this is :)

Orignal:

if [ $UNIT == "Gb" ]; then SIZE=`echo $SIZE \* 1024 | bc` fi 

Hardened:

if [ "$UNIT" == "Gb" ]; then SIZE="$(echo "$SIZE * 1024" | bc)" fi 

I never ever had any problem after I started to consistently follow these rules.

Here is a nice article about Bash scripting best practices: http://www.davidpashley.com/articles/writing-robust-shell-scripts/ and another one about shell expansion http://mywiki.wooledge.org/BashFAQ/082

UDPATE: corrected few typos

2
  • Interesting articles for reading (and learning) :) Commented Apr 20, 2015 at 13:41
  • Thanks a lot! It works now and I also learned something :) Commented Apr 20, 2015 at 14:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.