Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org How do I use X =< Y w/ IF statements?
 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

 09-23-2003, 11:21 PM #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: 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-23-2003 at 11:26 PM.
 09-24-2003, 01:04 AM #2 Saraev Member   Registered: Apr 2002 Location: Los Gatos, CA Distribution: boring redhat 9 Posts: 163 Rep: 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
 09-24-2003, 01: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: 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? Last edited by dolvmin; 09-24-2003 at 01:10 AM.
 09-24-2003, 01: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: 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 01:15 AM.
 09-24-2003, 01:17 AM #5 Saraev Member   Registered: Apr 2002 Location: Los Gatos, CA Distribution: boring redhat 9 Posts: 163 Rep: 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 01:19 AM.
 09-24-2003, 01: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: 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 03:16 AM.
 09-24-2003, 02: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: I think I got it!!! But I didn't use -ge. Let me test it thoroughly! Last edited by dolvmin; 09-24-2003 at 03:16 AM.
 09-24-2003, 02: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: 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 03:16 AM.
 09-24-2003, 02: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: 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 03:17 AM.
 09-24-2003, 03: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: 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.
 09-24-2003, 03:11 PM #11 Saraev Member   Registered: Apr 2002 Location: Los Gatos, CA Distribution: boring redhat 9 Posts: 163 Rep: 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.
 09-24-2003, 10: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: YES! That did it! I can't thank you enough man.

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post 0.o Programming 7 09-27-2005 02:59 PM mparkhurs Programming 3 06-12-2004 02:41 AM lectraplayer Programming 9 04-02-2004 11:38 AM thesnaggle Linux - Software 1 02-16-2004 09:52 AM vapor Programming 4 07-16-2003 06:44 AM

LinuxQuestions.org

All times are GMT -5. The time now is 09:28 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -