C shell integer tests, less than and more than, not working as desired


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting C shell integer tests, less than and more than, not working as desired
# 1  
Old 02-04-2009
C shell integer tests, less than and more than, not working as desired

Hello, I am creating a script that gives me system load info when i start a shell or log in via ssh(in the /etc/profile.d/ folder).

I created it successfully for bash and want it to work with c-shell as well.
This is where i'm having problems, the integer test in the if sentence does not work as it does with bash.

This is the script:

Code:
#!/bin/csh


if ( "$USER" != "root" ) then
    


echo "Measuring CPU load"


# ut1 and ut2 finds the %usage of the cpu for user and system. $ut makes them into one number
top -n 2 | grep 'Cpu(s)' | tail -1 > /tmp/cpuinfo.patrol.$$
set filename="/tmp/cpuinfo.patrol.$$"
chmod +rwx $filename
set ut1=`cat $filename | awk '{print $2}' | sed -e 's/\..*//'`
set ut2=`cat $filename | awk '{print $3}' | sed -e 's/\..*//'`
@ ut = ( $ut1 + $ut2 )

# $mu og $m find information of used and free memory on the system
set mu = `free -m | grep cache: | awk '{print $3}'`
set m = `free -m | grep cache: | awk '{print $4}'`
# $c finds %idle cpu.
set c = `cat $filename | awk '{print $5}' | sed -e 's/\%.*//'`
# $tm finds the total memory amount.
set tm = `free -m | grep Mem: | awk '{print $2}'`
# $cu does the same as $c, but filters .xx
set cu = `cat $filename | awk '{print $5}' | sed -e 's/\..*//'`


echo ""
echo ""
echo ""
echo "==================================================="
echo ""
echo "CPU usage: ${ut}%"
echo "Used Memory: ${mu}mb of ${tm}mb"
echo ""
echo  "    You have ${m}MB of free memory available"
echo  "    ${c}% of the total CPU capacity is idle"
echo ""
echo ""
 

if ( "$cu" > "20" ) then
    if ( "$m" < "500" )then
        echo "Starting heavy jobs now might not be a good idea."
    else
        echo "You do not have much cpu capacity available, keep an eye on your memory levels"
    endif
else if ( "$cu" > "20"  &&  "$cu" < "50" ) then
    if ( "$m" < "1000" ) then
        echo "You do not have a lot of cpu or memory capacity available."
    else if  ( "$m" > "1000"  &&  "$m" < "2000" ) then
        echo "You do not have a lot of cpu capacity available, but you do have an acceptable amount of memory."
    else if ( "$m" > "2000" ) then
        echo "You have less than 50% idle cpu capacity, but you do have a good amount of memory available."
    endif
else if ( "$cu" > "50"  &&  "$cu" < "80" ) then
    if ( "$m" < "1000" ) then
        echo "You have a good amount of cpu capacity available, but keep an eye on the memory. "
    else if ( "$m" < "2000"  &&  "$m" > "1000" ) then
        echo "You have a good amount of cpu and memory capacity available."
    else if ( "$m" > "2000" ) then
        echo "You have a good amount of cpu capacity and a good amount of memory available."
    endif
else if ( "$cu" > "80" ) then
    if ( "$m" < "1000" ) then
        echo "You have a lot of idle cpu capacity, but not a lot of memory"
    else if ( "$m" < "2000"  &&  "$m" > "1000" ) then
        echo "You have a lot of idle cpu capacity, and an acceptable amount of free memory."
    else if ( "$m" > "2000" ) then
        echo "You have loads of cpu and memory capacity available"
    endif
endif


echo ""
echo "==================================================="
echo ""
rm $filename
endif

The problem must be in this part: if ( "$cu" > "20" ) then

When i run this, it seems to run fine, but i get the comment: You do not have much cpu capacity available, keep an eye on your memory levels.
This is when i have like 80% free cpu.

I figured it might be because it thinks $cu is a string, but shouldn't i get an error msg if that was the case?

So can you guys help me figure out why this test is not working?
And if you've got any ideas to make this script a bit more simple, i'm open for suggestions. If you need any more info or want to se the bash script, just tell me and i'll post it.
# 2  
Old 02-04-2009
It doesn't look like string / integer comparison is the problem.... rather the logic.

You have:
if ( "$cu" > "20" ) then

And then later:
else if ( "$cu" > "20" && "$cu" < "50" ) then

doesn't look like it'll ever make it here...

or here:
else if ( "$cu" > "50" && "$cu" < "80" ) then

I think you need your tests in reverse order:

if ( $cu > 80 ) ....
else if ( $cu > 50 ) ...
else if ( $cu > 20 ) ...
# 3  
Old 02-05-2009
Thank you for your reply!

What i find strange is that the logic seems to work fine in my bash version of this script

It's got a different syntax but it's pretty much the same.
Code:
if [ "$cu" \< "20" ]; then
    if [ "$m" \> "500" ];then
        echo "Starting heavy jobs now might not be a good idea."
    else
        echo "You do not have much cpu capacity available, keep an eye on your m
emory levels"
    fi
elif [ "$cu" \> "20"  -a  "$cu" \< "50" ]; then
    if [ "$m" \< "1000" ]; then
        echo "You do not have a lot of cpu or memory capacity available."
    elif  [ "$m" \> "1000"  -a  "$m" \< "2000" ]; then
        echo "You do not have a lot of cpu capacity available, but you do have a
n acceptable amount of memory."
    elif [ "$m" \> "2000" ]; then
        echo "You have less than 50% idle cpu capacity, but you do have a good a
mount of memory available."
    fi
elif [ "$cu" \> "50"  -a  "$cu" \< "80" ]; then
    if [ "$m" \< "1000" ]; then
        echo "You have a good amount of cpu capacity available, but keep an eye 
on the memory. "
    elif [ "$m" \< "2000"  -a  "$m" \> "1000" ]; then
        echo "You have a good amount of cpu and memory capacity available."
    elif [ "$m" \> "2000" ]; then
        echo "You have a good amount of cpu capacity and a good amount of memory
 available."
    fi
elif [ "$cu" \> "80" ]; then
    if [ "$m" \< "1000" ]; then
        echo "You have a lot of idle cpu capacity, but not a lot of memory"
    elif [ "$m" \< "2000"  -a  "$m" \> "1000" ]; then
        echo "You have a lot of idle cpu capacity, and an acceptable amount of f
ree memory."
    elif [ "$m" \> "2000" ]; then
        echo "You have loads of cpu and memory capacity available"
    fi
fi

About the logic; I think the first line really was supposed to be
Code:
 if ( "$cu" < "20" )

i fooled around with it when it did not work. I just can't understand how it can end up echoing this line:
Code:
 "You do not have much cpu capacity available, keep an eye on your m
emory levels"

When $cu clearly is at more than 20. Shouldn't it just skip the whole if sentence and go to the next else if?(yes this happens after changing to < instead of > )

I'm gonna try to build the logic like you suggested and see if that gives a better result Smilie
# 4  
Old 02-06-2009
Ok, i remade the whole thing just the way you suggested and it worked perfectly.
Thank you for helping me! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Needed shell script to append desired text to each line in a file

Hi, I had generated a report in my tool as followsoutput.txt 43.35 9 i needed the script to generate a new file like below i want to append the text to each of these lines of my filenewoutputfile.txt should be Total Amount : 43.35 Record Count:9 Regards, Vasa Saikumar. ... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

2. Shell Programming and Scripting

Cron job is not working in the desired manner on IBM AIX

Hi, I have created a cron job on IBM AIX but it is not working in desired manner ! Here are the steps which I have followed :- #!/bin/ksh #------------------------------------------------------------------ find /some/file/at/the/user/side/test.log -exec cp {}... (8 Replies)
Discussion started by: acidburn_007
8 Replies

3. UNIX for Dummies Questions & Answers

if statement not working as desired

Hello all, I am trying to write a post-commit hook script using bash script. What I am trying to do here is: Developers check in their files to a branch. I check the repository and based on the commit I email QA people. QA verifies and moves the files to a prod branch and email is sent... (1 Reply)
Discussion started by: kminkeller
1 Replies

4. Shell Programming and Scripting

Shell Integer with nested foreach

I am scripting in tcsh and here is what I currently have: foreach group (g1 g2 g3 g4) set ppl = `cat $group.file.with.list.of.ppl.in.row.format` set label = 1 @ label += 1 foreach ppls ($ppl) echo $label >> file end end ... (0 Replies)
Discussion started by: bonesy
0 Replies

5. UNIX for Dummies Questions & Answers

chmod not working as desired

my file had permission -rw-rw-r-- I did chmod +rwx, expecting everything to now be rwx, but it is -rwxrwxr-x why doesn't o have x permission? thanks. (2 Replies)
Discussion started by: JamesByars
2 Replies

6. Shell Programming and Scripting

how do i generate random integer using only shell script

Hi All, I need to generate 4 digit random no using only shell script. Please help in this ASAP. Thanks in advance... Regards, sridhar. (1 Reply)
Discussion started by: sridhusha
1 Replies

7. Shell Programming and Scripting

shell scripting tests

hi all, i will have a shell scripting exam by thursday and i need a solved exams to get ready for my exam.that is very important for me.i really appreciate your help. (0 Replies)
Discussion started by: shaimaa
0 Replies

8. UNIX for Dummies Questions & Answers

practice tests- Unix Korn Shell Scripting

Hi, I am about to take certification for "Unix Korn Shell Scripting", which is conducted by brain bench. ( http://www.brainbench.com/xml/bb/common/testcenter/taketest.xml?testId=46) The test consists of 40 single and multiple choice questions that must be answered in 60 minutes. The pass mark... (0 Replies)
Discussion started by: eswasas
0 Replies

9. Shell Programming and Scripting

Script not working as desired

Reborg, Sorry to bother you. I have tried the code you suggested and it's not creating new files after they satisfy the criteria. If any of the files don't satisfy the criteria it should not create the files at all. Please see my output below. (39 Replies)
Discussion started by: mhssatya
39 Replies

10. Shell Programming and Scripting

conversion to integer not working

Hello.. I have a file containg as below: My need is to sum the values... but the error is showing as interger conversion..so i put int() but again not working..please help..here is my code Thanks in advance esham (7 Replies)
Discussion started by: esham
7 Replies
Login or Register to Ask a Question