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.
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
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)
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)
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)
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)
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)
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)
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)
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)
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)