LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-15-2012, 05:13 PM   #1
daisychick
Member
 
Registered: Nov 2006
Location: Texas
Distribution: ubuntu 12.04 LTS
Posts: 154

Rep: Reputation: 0
-ge: unary operator expected


I'm trying this script out but, I don't know enough about how it works to figure out what's causing the problem. I'm not having a lot of luck googling. OS is ubuntu 11.10.
SCRIPT:
Code:
#!/bin/bash
HDDS="/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde"
HDT=/usr/sbin/hddtemp
LOG=/usr/bin/logger
DOWN=/sbin/shutdown
ALERT_LEVEL=55
for disk in $HDDS
do
  if [ -b $disk ]; then
        HDTEMP=$($HDT $disk | awk '{ print $4}' | awk -F '°' '{ print $1}')
        if [ $HDTEMP -ge $ALERT_LEVEL ]; then
           $LOG "System going down as hard disk : $disk temperature $HDTEMP°C crossed its limit"
           sync;sync
           $DOWN -h 0
        fi
  fi
done
ERROR:
Code:
daisy@mediaserver:~$ sudo ./tempcheck.sh
./tempcheck.sh: line 15: [: -ge: unary operator expected
./tempcheck.sh: line 15: [: -ge: unary operator expected
./tempcheck.sh: line 15: [: -ge: unary operator expected
./tempcheck.sh: line 15: [: -ge: unary operator expected
daisy@mediaserver:~$
 
Old 09-15-2012, 07:41 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I think you might need to quote the variables inside the test ( [..] ) construct---See chapter 7 in the infamous "ABS":
http://www.tldp.org/LDP/abs/abs-guide.pdf

But wait----"-ge" is nowhere near line 15----

This could be the problem:
Code:
for disk in $HDDS
do
  if [ -b $disk ]; then
Maybe try this instead?
Code:
for disk in $HDDS; do
  if [ -b $disk ]; then
 
Old 09-16-2012, 04:03 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
The script is fine, the only issue is that $HDTEMP is not defined when the if statement is hit.

Here is an alternative way to get the temperature:

Code:
smartctl -A /dev/sda | grep Temperature_Celsius | awk '{ print $10 }'
OR
smartctl -A /dev/sda | grep Airflow_Temperature_Cel | awk '{ print $10 }'
Also, the HDD temperature is not too important. I mean, I don't see any reason for the HDD to overheat, not any reason to shutdown if it does. Just make sure there is room around the HDD inside the case and it should be fine.

Last edited by H_TeXMeX_H; 09-16-2012 at 04:05 AM.
 
Old 09-16-2012, 04:30 AM   #4
daisychick
Member
 
Registered: Nov 2006
Location: Texas
Distribution: ubuntu 12.04 LTS
Posts: 154

Original Poster
Rep: Reputation: 0
That's kinda of the problem. It's an older dell tower pc with really crappy ventilation. Two of the drives are constantly ranging from 50°-60° and I'm not so cool with that. It seriously decreases the lifespan of the drive and it gets annoying to have to RMA one every 6 months. Since it's a media server, I prefer it to be constantly at the ready but, since I'm not home 75% of the day, I'd rather it turn off if it gets too hot than turn it off, turn it on, turn it off, turn it on several times a day.
 
Old 09-16-2012, 05:12 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
add fan(s)?

back to the code: It seems to me that HDTEMP is defined--right before the if statement that tests it.....but yes, if it's not defined, that causes the error.

But--BUT--maybe it's getting set to a string and not to a number?
 
Old 09-16-2012, 05:30 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,631

Rep: Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265
try to use set -xv, so you will get much more info about what's happening (set -xv should be inserted as the second line of the script). In general you need to use " with variables, so better to write: if [ -b "$disk" ]; then and if [ "$HDTEMP" -ge "$ALERT_LEVEL" ]; then
You may need to try a better approach to find out the temperature, so you can try this:
HDTEMP=$($HDT $disk | awk '{ match($4, "[0-9]+", a); print a[0]}')
Also using the idea from H_TeXMeX_H you can try:
HDTEMP=$(smartctl -A $disk | awk '/Temperature_Celsius/ { print $10 }')

(not tested)
 
Old 09-16-2012, 08:34 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
First of all, you should use an array for your drive list instead of a scalar variable. Or just a globbing pattern.

And don't try to put commands in variables, use functions instead.

Since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them.

When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://wiki.bash-hackers.org/syntax/arith_expr


Here's how I would do it:

Code:
#!/bin/bash

alert_level=55
hdds=( /dev/sd{a,b,c,d,e} )

hdt() {
	/usr/sbin/hddtemp "$1" | awk '{ print int($NF) }'
}

log() {
	/usr/bin/logger "System going down as hard disk : $1 temperature $2°C crossed its limit"
}

down() {
	/sbin/shutdown -h 0
}

for disk in "${hdds[@]}"; do

	if [[ ! -b $disk ]]; then
		echo "Unknown device type: $disk.  Skipping"
		continue
	fi

	hdtemp=$( hdt "$disk" )

	if (( hdtemp >= alert_level )); then

		log "$disk" "$hdtemp"
		sync
		sync
		down

        fi

done
And as mentioned, you could just skip the array completely and use this:

Code:
for disk in /dev/sd[a-e]; do
This would also remove the need for the "-b" test, since you'll only be getting devices that actually exist at the moment.

Last edited by David the H.; 09-16-2012 at 08:43 AM. Reason: small code corrections
 
Old 09-16-2012, 03:12 PM   #8
daisychick
Member
 
Registered: Nov 2006
Location: Texas
Distribution: ubuntu 12.04 LTS
Posts: 154

Original Poster
Rep: Reputation: 0
thanks for all the replies! unfortunately, most of it was over my head but I'll try to pick it up. I went with David the H's idea and it seems to work. Is there any way to output what it does to a log so I can verify it's working? @pixellany: there is no more room/no more juice from the power supply for any more fans. I've added tons. I really need a new machine but I'm being cheap.
 
Old 09-17-2012, 12:56 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,631

Rep: Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265Reputation: 7265
Quote:
Originally Posted by daisychick View Post
Is there any way to output what it does to a log so I can verify it's working?
That is set -xv what I suggested you before
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unary operator expected error greenpool Linux - Newbie 1 01-31-2012 07:36 PM
[SOLVED] -gt: unary operator expected hd_pulse Programming 10 05-28-2011 07:57 AM
[SOLVED] unary operator expected hd_pulse Programming 8 05-26-2011 11:01 PM
error: unary operator expected ?? Lynda_M Programming 3 11-29-2008 08:03 PM
unary operator expected MONKEYJUDO Linux - Newbie 1 07-13-2008 06:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration