LinuxQuestions.org
Register a domain and help support LQ
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-24-2003, 12:21 AM   #1
dolvmin
Member
 
Registered: Jul 2003
Location: Florida
Distribution: Red Hat 7.2/8/9, Fedora Core 1/2/3, Smoothwall, Mandrake 7.0/10, Vecter 4, Arch 0.6, EnGuarde
Posts: 289

Rep: Reputation: 30
How do I use X =< Y w/ IF statements?


Still learning, sorry for the dumb question.

I just need to know how to use the greater then simble w/ an IF statement. I keep on getting my system to try an Append function. heh

Last edited by dolvmin; 09-24-2003 at 12:26 AM.
 
Old 09-24-2003, 02:04 AM   #2
Saraev
Member
 
Registered: Apr 2002
Location: Los Gatos, CA
Distribution: boring redhat 9
Posts: 163

Rep: Reputation: 30
are we talkin about a bash script? If so,
if [ "$1" = "-h" ]
then
do something
# elif means "else if"
elif [ "$1" = "-a" ]
then
do something else
fi
 
Old 09-24-2003, 02:08 AM   #3
dolvmin
Member
 
Registered: Jul 2003
Location: Florida
Distribution: Red Hat 7.2/8/9, Fedora Core 1/2/3, Smoothwall, Mandrake 7.0/10, Vecter 4, Arch 0.6, EnGuarde
Posts: 289

Original Poster
Rep: Reputation: 30
Sorry, yes bash.

Do I need to use else if? (elif)?

This is what I need.

if [ $(ps aux | grep "./nwserver -modul" | head -1 | awk '{print $3}') = 28.0 ];then

I want to setup a line where if the number is any number greater then 28.0, then (*)

If I use elif, I'll have to setup 7200 elifs. Ouch man. LOL

Then again, I could use a case, and just right out 7200 possible actions under 1 case. Save myself some typing, eh? <laughs>

Last edited by dolvmin; 09-24-2003 at 02:10 AM.
 
Old 09-24-2003, 02:14 AM   #4
dolvmin
Member
 
Registered: Jul 2003
Location: Florida
Distribution: Red Hat 7.2/8/9, Fedora Core 1/2/3, Smoothwall, Mandrake 7.0/10, Vecter 4, Arch 0.6, EnGuarde
Posts: 289

Original Poster
Rep: Reputation: 30
I got it, but developed a new problem. I found some java programing information, used it and didn't get an error related to it (asuming it works)

My line is now:
if [ $(ps aux | grep "./nwserver -modul" | head -1 | awk '{print $3}') >= 28.0 ];then

Problem is, it does not know how to identify 28.0 as a number of 1.0-100.0. Any way to fix this?

Last edited by dolvmin; 09-24-2003 at 02:15 AM.
 
Old 09-24-2003, 02:17 AM   #5
Saraev
Member
 
Registered: Apr 2002
Location: Los Gatos, CA
Distribution: boring redhat 9
Posts: 163

Rep: Reputation: 30
I would separate the logic a bit, like so.

testvariable = `ps aux | grep "./nwserver -modul" | head -1 | awk '{print $3}'`

if [ $testvariable -ge 28.0 ]
then
do your stuff
fi


You don't need the elif, I just threw that in there.

http://www.tldp.org/LDP/abs/html/comparison-ops.html
This page shows the different comparison operators. Looks like if you want to use >= you need to use parentheses.

edit:
I think if you compare integers you must use either -ge or put it in parentheses

Last edited by Saraev; 09-24-2003 at 02:19 AM.
 
Old 09-24-2003, 02:50 AM   #6
dolvmin
Member
 
Registered: Jul 2003
Location: Florida
Distribution: Red Hat 7.2/8/9, Fedora Core 1/2/3, Smoothwall, Mandrake 7.0/10, Vecter 4, Arch 0.6, EnGuarde
Posts: 289

Original Poster
Rep: Reputation: 30
Okay, I'm doing something wrong. Here's the information.

ps aux | grep "./nwserver -modul" | head -1 | awk '{print $3}'

Result is: 26.6

Yet, this line:

if [ "$(ps aux | grep "./nwserver -modul" | head -1 | awk '{print $3}') -ge 40.0" ]

Is asuming a # greater then 100.0

When I try using -el, I get the same result. The problem is the 40.0. I do not think it is in a readable format. Is there a way I can resolve this?

Last edited by dolvmin; 09-24-2003 at 04:16 AM.
 
Old 09-24-2003, 03:15 AM   #7
dolvmin
Member
 
Registered: Jul 2003
Location: Florida
Distribution: Red Hat 7.2/8/9, Fedora Core 1/2/3, Smoothwall, Mandrake 7.0/10, Vecter 4, Arch 0.6, EnGuarde
Posts: 289

Original Poster
Rep: Reputation: 30
I think I got it!!! But I didn't use -ge. Let me test it thoroughly!

Last edited by dolvmin; 09-24-2003 at 04:16 AM.
 
Old 09-24-2003, 03:21 AM   #8
dolvmin
Member
 
Registered: Jul 2003
Location: Florida
Distribution: Red Hat 7.2/8/9, Fedora Core 1/2/3, Smoothwall, Mandrake 7.0/10, Vecter 4, Arch 0.6, EnGuarde
Posts: 289

Original Poster
Rep: Reputation: 30
Damn, no. I didn't get it. I replaced -ge with = and replaced the number 40.0 with "[4-0][9-0].[9-0]" All it did was move on to the else statement.

I verified this when I changed the string to "[0-9][0-9].[0-9]"
It continued on the else statement.

Last edited by dolvmin; 09-24-2003 at 04:16 AM.
 
Old 09-24-2003, 03:43 AM   #9
dolvmin
Member
 
Registered: Jul 2003
Location: Florida
Distribution: Red Hat 7.2/8/9, Fedora Core 1/2/3, Smoothwall, Mandrake 7.0/10, Vecter 4, Arch 0.6, EnGuarde
Posts: 289

Original Poster
Rep: Reputation: 30
Okay, I changed the line a little bit to clean up the output, but it still does not work. I'm very lost.. Here's the change:

if [ "$(ps aux | grep "./nwserver -modul" | head -1 | sed 's/[.][0-9]//' | awk '{print $3}') -ge 40" ];then

I also tried this.

CPUCHECK='ps aux | grep "./nwserver -modul" | head -1 | sed 's/[.][0-9]//' | awk '{print $3}''

if test $CPUCHECK -ge 80;then
-failed.


if expr [ $CPUCHECK -ge 80];then
-failed:I get a syntex error.

At least it's different lol.

Last edited by dolvmin; 09-24-2003 at 04:17 AM.
 
Old 09-24-2003, 04:29 AM   #10
dolvmin
Member
 
Registered: Jul 2003
Location: Florida
Distribution: Red Hat 7.2/8/9, Fedora Core 1/2/3, Smoothwall, Mandrake 7.0/10, Vecter 4, Arch 0.6, EnGuarde
Posts: 289

Original Poster
Rep: Reputation: 30
Okay, I think I figured out what's going on. The output of $CPUTEST is visually a number, but is not classified in computation as a number. So the test -ge function does not handle it correctly like the = sign would, because test is (if I am correct) only for numeric and not for alpha-numeric much like the = sign would be.

This means, I need another way to do this other then -ge, or change the output of $CPUTEST to a numeric form of which, test can handle. Well, I have been working on this for 6 hours. I'm tired. Taking a nap.
 
Old 09-24-2003, 04:11 PM   #11
Saraev
Member
 
Registered: Apr 2002
Location: Los Gatos, CA
Distribution: boring redhat 9
Posts: 163

Rep: Reputation: 30
OK. I think I've got it. Take this example script, checking the CPU use of klogd.

#!/bin/sh
xKLOGUSE=`ps aux | grep klogd | head -1 | awk -F" " '{print $3}'`
KLOGUSE=`echo $xKLOGUSE | awk -F. '{print $1}'`
echo $KLOGUSE
echo "----spacer line----"

if [ "$KLOGUSE" -gt 10 ]
then
echo "greater than"
else
echo "less than"
fi
echo "--done--"


The first awk statement strips out the CPU use value (sitting at 0.0 almost always on this bit.) The second awk removes the .0 from the end, losing a bit of accuracy, but that can be adjusted. note: Bash seems to like whole number integers, and treat floating point (decimal) numbers as strings.

Now that $KLOGUSE is an integer, the comparison works. You could put that second awk statement on the same line as the first, just piped over from the first awk. Either way.

If you NEED the accuracy of XX.X% instead of XX%, you could just strip out the decimal point and think of 0-999 instead of 0.0-99.0

Lemmie know if this fixes it, though you've probably already figured it out.
 
Old 09-24-2003, 11:39 PM   #12
dolvmin
Member
 
Registered: Jul 2003
Location: Florida
Distribution: Red Hat 7.2/8/9, Fedora Core 1/2/3, Smoothwall, Mandrake 7.0/10, Vecter 4, Arch 0.6, EnGuarde
Posts: 289

Original Poster
Rep: Reputation: 30
YES! That did it! I can't thank you enough man. <cheers!>
 
  


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
if else statements 0.o Programming 7 09-27-2005 03:59 PM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 03:41 AM
Why my else statements always run? lectraplayer Programming 9 04-02-2004 12:38 PM
if statements thesnaggle Linux - Software 1 02-16-2004 10:52 AM
if then statements vapor Programming 4 07-16-2003 07:44 AM


All times are GMT -5. The time now is 06:21 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