How to read from file and convert from string to integer?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read from file and convert from string to integer?
# 1  
Old 10-14-2014
How to read from file and convert from string to integer?

Hi all,

I am trying to write a script to be able to
  1. Run top command
  2. Pick the PIDs that are taking more than 90% of CPU time
  3. Get more details like what is the script name and location for that PID
  4. Script should run until I manually kill it by ctrl + C
I have come up with following script for this:

Code:
#!/bin/bash
while true; do
filename=`date +"%Y%m%d%H%M%S"`
touch $filename
chmod 755 $filename
top -bn 1 | grep "^ " | awk '{ printf("%-8s %-8s  %-8s  %-8s\n", $1, $9, $10, $12); }' | head -n 10 >> $filename

while read pid cpu mem command
        do
                if [ $cpu -ge 90 ]; then
                        echo "For $pid" >> SlownessStatusReporter_out
                        cat /proc/$pid/cmdline >> SlownessStatusReporter_out
                fi
        done < $filename

sleep 5
done

But I think I am not doing data type conversion properly for $cpu at line
Code:
if [ $cpu -ge 90 ]; then

I think when I read from $filename cpu is coming as string and inside if condition, I am trying to use it as integer. This guess is from the fact that I keep getting error for the line if [ $cpu -ge 90 ]; then as below:
Code:
./SlownessStatusReporter.sh: line 15: [: %CPU: integer expression expected
./SlownessStatusReporter.sh: line 15: [: 0.0: integer expression expected
./SlownessStatusReporter.sh: line 15: [: 0.0: integer expression expected
./SlownessStatusReporter.sh: line 15: [: 0.0: integer expression expected
./SlownessStatusReporter.sh: line 15: [: 0.0: integer expression expected
./SlownessStatusReporter.sh: line 15: [: 0.0: integer expression expected
./SlownessStatusReporter.sh: line 15: [: 0.0: integer expression expected
./SlownessStatusReporter.sh: line 15: [: 0.0: integer expression expected
./SlownessStatusReporter.sh: line 15: [: 0.0: integer expression expected
./SlownessStatusReporter.sh: line 15: [: 0.0: integer expression expected

When I tried to put $cpu and 90 in double quotes or tried to use >= instead of -ge, I get a different error as below:
Code:
./SlownessStatusReporter.sh: line 15: [: %CPU: unary operator expected
./SlownessStatusReporter.sh: line 15: [: 2.0: unary operator expected
./SlownessStatusReporter.sh: line 15: [: 2.0: unary operator expected
./SlownessStatusReporter.sh: line 15: [: 0.0: unary operator expected
./SlownessStatusReporter.sh: line 15: [: 0.0: unary operator expected
./SlownessStatusReporter.sh: line 15: [: 0.0: unary operator expected
./SlownessStatusReporter.sh: line 15: [: 0.0: unary operator expected
./SlownessStatusReporter.sh: line 15: [: 0.0: unary operator expected
./SlownessStatusReporter.sh: line 15: [: 0.0: unary operator expected
./SlownessStatusReporter.sh: line 15: [: 0.0: unary operator expected

Can someone please suggest me how correct this error so my script can work.
I am using:
Code:
$ uname -a
Linux hostname 2.6.18-308.13.1.el5 #1 SMP Thu Jul 26 05:45:09 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux
$ echo $SHELL
/bin/bash
$

Thanks in advance.

Last edited by rbatte1; 10-14-2014 at 06:11 AM.. Reason: Added LIST=1 for initial list and CODE tags round output
# 2  
Old 10-14-2014
You might have a few issues to deal with. The use of a file is not really needed as you can use a pipe directing into your while read .... statement:-
Code:
top -bn 1 | grep "^ " | while read pid . . . . . . . cpu mem . command .
do
   :
   :

The full-stops in the while read part are just place holders, so you can see I pick out the columns you are after without needed an awk at all.

That will get the values, but if you look at your output (content currently in $filename) presumably has the header record in it, so the first read gets the value %CPU which is not an integer. The other lines do get numeric values, but they are decimal rather than integers and are therefore considered by the test as strings.

We can test and ignore the %CPU record, but the decimal will be a problem. Are you worried about the tenths, or can we truncate them? We can use variable substitution to split the string into the two parts like this:-
Code:
whole_bit="${cpu%.*}"
tenth_bit="${cpu#*.}"

You then have the two parts to work with as you please. I'm guessing that as your later test if for over 90% CPU, then the tenths are not a problem and you can just test if $whole_bit is greater than or equal to 90. I'd probably use the -ge notation.


Does this help, or does it just leave you stuck elsewhere?



Robin
# 3  
Old 10-14-2014
As rbatte1 said, you can collect most of what you do into a few lines; here: the awk command:
Code:
 top -bn 1 | awk 'NR>7 && $9 > 90 { printf "For %-8s %s\n", $1,  $12 > "Slow_Report"} NR > 17 {exit}'

You may have to adapt this a little, bit I think you get the logics: it assumes the first 7 lines to be the header, then, if field 9 is greater than 90, it prints sth to the slow_report file. After analyzing 10 lines, it quits.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-24-2014
Hi rbatte1 and RudiC,
Thanks for your inputs. RudiC I followed your advice and it worked with little tailoring.
My apology for being late to respond here.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert string number to a integer

I have data as below "ROWS merge process complete. thousand rows changed" I need to get a variable assigned the value of 1000. I mean convert the string thousand to 1000. Any help or pointer. Please use CODE tags as required by forum rules! (6 Replies)
Discussion started by: dsravanam
6 Replies

2. Shell Programming and Scripting

How to convert string into integer in shell scripting?

Hi All, sessionid_remote=$(echo "select odb_sessionid from sysopendb where odb_dbname='syscdr';" | sudo -u cucluster ssh ucbu-aricent-vm93 "source /opt/cisco/connection/lib/connection.profile; $INFORMIXDIR/bin/dbaccess sysmaster@ciscounity") for sid in $sessionid_remote;do if * ]];... (2 Replies)
Discussion started by: deeptis
2 Replies

3. Shell Programming and Scripting

[Solved] need to convert decimal to integer

Using below command awk 'NR==FNR{A=$1;next} {sum+=($2*A)}END{OFMT="%20f";print int(sum)}' Market.txt Product.txt answer:351770174.00000 how to convert this to 351770174. when i try with below command i am getting different result. awk 'NR==FNR{A=$1;next}... (3 Replies)
Discussion started by: katakamvivek
3 Replies

4. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

5. Shell Programming and Scripting

Convert to Integer

Hi fellows!! i'm doing something which is not working out for me properly which i don't understand why nowdate=`date +%s` echo $nowdate now the problem how to convert a date which is stored in a variable mydate="22/Oct/2011" mydate=`date -d '$mydate' +%s` it gives error... (11 Replies)
Discussion started by: me_newbie
11 Replies

6. Programming

read integer from file C

Hi , Can you have a look at my code it should display integer in file but it doesn't #include <stdio.h> /* This function reads two values from a file. */ int demonstrate_fscanf(void) { FILE *InFile; int b, numValuesRead; InFile = fopen("fadata.rtf", "r"); if... (3 Replies)
Discussion started by: guidely
3 Replies

7. Shell Programming and Scripting

Read Variable From Fille And Convert it to Integer

I read 3 variables from from Inputfile.txt the third one "startnumber" is a number when i compare it with 9 ($startnumber -le 9) it give's me a "unary operator expected", i know that -le is for number comparison. What i need is to convert $startnumber to integer (i have try to do it with expr but... (8 Replies)
Discussion started by: marios
8 Replies

8. Shell Programming and Scripting

how to convert string to an integer and how to do calculations like add.,sub.,mult. on it

How to convert string into an integer or number For example : % set tim = `date` % echo $tim Tue Feb 22 16:25:08 IST 2011 here How to increment time by 10 hrs like 16+10 , here 16 is a string in date cmd. .. how to convert 16 to an integer and added to a another nimber ? Thanks... (3 Replies)
Discussion started by: sbhamidi
3 Replies

9. Programming

How i can read a long integer from standar input and a string with as many characters as specified..

how i can read a long integer from standar input and a string with as many characters as specified in the number? i thing that i must use the read command ofcourse.... (6 Replies)
Discussion started by: aintour
6 Replies

10. UNIX for Dummies Questions & Answers

convert from an integer to a string

i want to convert from an integer to a string..in unix...i am writing a C program with embedded SQL... I remeber using itoa...but for some reason it doesnt work......i cant find it in the manual..... Maybe that is the wrong command..... but i have checked Dev Studio.....and it doest exist in the... (6 Replies)
Discussion started by: mojomonkeyhelper
6 Replies
Login or Register to Ask a Question