LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-10-2009, 12:38 AM   #1
jhyland87
LQ Newbie
 
Registered: Dec 2008
Posts: 25

Rep: Reputation: 15
Noob as bash - Getting an error


hey guys, im trying to make a bash script, here is what I have so far

PHP Code:
for PROCESS in $PS ; do
        
#This AWK command outputs a short bash "program" which can be called w/ eval to set the right variables 
        
eval `echo $PROCESS | awk -F\| '{printf ("USR=%s;CPU=%s;MEM=%s;CMD=%s;PID=%s", $1, $2, $3, $4, $5)}'`
    
        
#Now put the variables into arrays
        
USRS[${COUNT}]=$USR
        CPUS
[${COUNT}]=$CPU
        MEMS
[${COUNT}]=$MEM
        CMDS
[${COUNT}]=$CMD
        PIDS
[${COUNT}]=$PID
        
if [ $CPU -gt "0" ];
        
then
            
echo "$CPU TOO HIGH!"
        
else
            echo 
"$CPU"
        
fi
        let COUNT
=COUNT+1
done 
However, when I run it, I get this error;
Quote:
./process: line 22: [: 0.0: integer expression expected
0.0
./process: line 22: [: 0.0: integer expression expected
0.0
./process: line 22: [: 0.7: integer expression expected
0.7
./process: line 22: [: 0.0: integer expression expected
0.0
./process: line 22: [: 0.0: integer expression expected
0.0
./process: line 22: [: 0.0: integer expression expected
0.0
./process: line 22: [: 0.0: integer expression expected
0.0
Any ideas? Its driving me nuts
 
Old 01-10-2009, 02:24 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
Could you indicate which is line 22?

One thing you could do is "cat -n process" to add line numbers to your script.
You have obscured the problem by not posting the assignment of the variables.
For example, compare:
Code:
CPU='a'
if [ $CPU -gt 0 ]; then echo greater; fi

CPU='1'
if [ $CPU -gt 0 ]; then echo greater; fi
The expression with -gt compares integer values. The first example gives the same error you listed.
Also check how count is initialized.
From the info bash manual:
Quote:
The SUBSCRIPT is treated as an arithmetic expression that must evaluate
to a number greater than or equal to zero.
Bash arrays aren't hashes. The index is an integer. You can have sparse arrays however, so in that sense they are like hashes. You can have an array with two elements var[1] and var[50].

Last edited by jschiwal; 01-10-2009 at 02:32 AM.
 
Old 01-10-2009, 02:25 AM   #3
jhyland87
LQ Newbie
 
Registered: Dec 2008
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
Could you indicate which is line 22?

One thing you could do is "cat -n process" to add line numbers to your script.
You have obscured the problem by not posting the assignment of the variables.
For example, compare:
Code:
CPU='a'
if [ $CPU -gt 0 ]; then echo greater; fi

CPU='1'
if [ $CPU -gt 0 ]; then echo greater; fi
The -gt expression expects to integer values, and you will get a very similar error in the first example. The error may be in the type of the arguments you are using and not in the code itself.

Also check how count is initialized.
line 22 is
if [ $CPU -gt "0" ];
 
Old 01-10-2009, 02:30 PM   #4
jhyland87
LQ Newbie
 
Registered: Dec 2008
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
Could you indicate which is line 22?

One thing you could do is "cat -n process" to add line numbers to your script.
You have obscured the problem by not posting the assignment of the variables.
For example, compare:
Code:
CPU='a'
if [ $CPU -gt 0 ]; then echo greater; fi

CPU='1'
if [ $CPU -gt 0 ]; then echo greater; fi
The expression with -gt compares integer values. The first example gives the same error you listed.
Also check how count is initialized.
From the info bash manual:


Bash arrays aren't hashes. The index is an integer. You can have sparse arrays however, so in that sense they are like hashes. You can have an array with two elements var[1] and var[50].
Ive pretty much got most of that figured out, I just dont get why im getting this stupid error
 
Old 01-10-2009, 05:16 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Bash does not manage floating point numbers. The CPU variable stores a floating point (the cpu percentage from a ps command, I guess) hence the error:
Code:
./process: line 22: [: 0.0: integer expression expected
where ./process is the name of the script,
line 22 is the relevant line
[ is the command originating the error (yes, in linux [ is a command)
0.0 is the wrong item
the rest is the error message.
As you can see the numbers 0.0 and 0.7 in your example trigger the error.

At this point you can:
1) format the output from the awk statement, forcing the percentage of the CPU to be an integer. It will be rounded anyway, so you will lost most of its meaning, especially when it is a value between 0 and 1.
2) multiply the number by 10 in the awk statement and again print it as integer using the %d modifier. Then the test CPU -gt 0 is still valid. If you want to change the test, don't forget to multiply the threshold by the same factor 10. For example if you want to test a CPU usage greater than 0.8, you will do CPU -gt 8.

Last edited by colucix; 01-10-2009 at 05:17 PM.
 
  


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
bash and filesize for noob lorenww Linux - Newbie 9 02-27-2008 06:24 PM
bash noob question sir-lancealot Programming 8 01-03-2008 01:56 PM
BSD noob [adding bash] simplebob *BSD 5 04-03-2006 02:27 AM
noob bash script question babag Programming 9 04-30-2005 02:25 AM
stuck again (noob bash math question) babag Programming 6 04-25-2005 01:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:35 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration