LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-06-2009, 01:48 PM   #1
b-neva
LQ Newbie
 
Registered: May 2009
Posts: 23

Rep: Reputation: 0
script for executing a program and doing string comparison


Hi there,

I am fairly new to linux still and I'm currently trying to write a shell script (which I have never done) that is supposed to run the xrestop(like top) program. What I want to do is execute the program and check the identifier column to see if a certain program is running. If it is running I want to strip the data from that row of the terminal say every 5 seconds or so and put it in a text file. Is this going to be a complicated thing? Any ideas?


heres a list of the columns when you run xrestop program

res-base Wins GCs Fnts Pxms Misc PxmMem Other Total PID Identifier


Thanks

Last edited by b-neva; 05-06-2009 at 01:52 PM.
 
Old 05-06-2009, 02:28 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
man cut
 
Old 05-06-2009, 02:40 PM   #3
b-neva
LQ Newbie
 
Registered: May 2009
Posts: 23

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
man cut
but that's only for files correct? xrestop is running in the terminal and I was wondering if I could search it while it's running and then put it to a file.
 
Old 05-06-2009, 03:52 PM   #4
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
It seems to me that it would be easier and more straightforward to write a simple shell script to parse the process tree (man ps) and write the result to the status file.
 
Old 05-06-2009, 03:59 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
If you use the option -b you can take a snapshot into the standard output. To catch it every 5 seconds, use sleep 5 and embed your code in an infinite loop, for example
Code:
#!/bin/bash
while true
do
  xrestop -b | grep some_app | some other commands here
  other commands here
  sleep 5
done
 
Old 05-07-2009, 02:38 PM   #6
b-neva
LQ Newbie
 
Registered: May 2009
Posts: 23

Original Poster
Rep: Reputation: 0
Code:
while true
do    
    memory=`ps -eo vsz,cmd | grep spry | cut -f 1 -d \ `
    memory=`echo $memory / 7500 | bc`
    i=0
    while [$i -lt $memory]
    do
    	echo -n "="
    	i = `echo $i + 1 | bc`
    done
    echo
done
this is what I have so far, it's not working once I put the while loop in (I think my syntax/logic is just wrong). What I am trying to do is get the memory/7500 so I have a small number usually in the 20-30 range and get it to print out that many = so by the end I will have somewhat of a "graph"

Code:
memory=`expr $memory/7500`;echo $memory
Just another question, I had this instead of the "memory=`echo $memory / 7500 | bc`" statement but it would just print out the value of memory then 5000. So like 2553435000 instead of the actual evaluation, what was I doing wrong?
 
Old 05-07-2009, 02:49 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The only problem I see in your script is the incorrect usage of blank spaces:

1) put blank space around the square brackets:
Code:
[ $i -lt $memory ]
2) don't put blank spaces around the = sign in an assigment:
Code:
i=`echo $i + 1 | bc`
3) put blank spaces around the arithmetic operator:
Code:
expr $memory / 7500
 
Old 05-07-2009, 03:02 PM   #8
b-neva
LQ Newbie
 
Registered: May 2009
Posts: 23

Original Poster
Rep: Reputation: 0
Ok I tried your fixes.

Code:
memory=`expr $memory / 7500`
if I try this, I get an expr syntax error, it won't work unless I have no spaces between $memory/7500

Quote:
(standard_in) 1: syntax error
./memory.sh: line 6: [: 0: unary operator expected
Ok so I get this I think just because it's trying to find the "spry" program but doesn't find it. If I am running the program then it stops the errors and starts printing the "=" now I just have a few logic problems to workout with that.

thanks
 
Old 05-07-2009, 03:17 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Yes, when the variable memory is not allocated (it has zero length) you get a bunch of syntax errors. You can avoid the problem adding a simple test:
Code:
memory=`ps -eo vsz,cmd | grep spry | cut -f 1 -d \ `
if [ -n "$memory" ]
then
  your commands here
fi
Another note: you can modify the ps command to retrieve just the virtual memory size of the spry process (if any):
Code:
ps -o vsz= -C spry
this will look for the spry process only and will give just the memory, without the header (see the = sign after vsz).
 
Old 05-07-2009, 03:52 PM   #10
b-neva
LQ Newbie
 
Registered: May 2009
Posts: 23

Original Poster
Rep: Reputation: 0
Alright, the program seems to be doing what it's supposed to be doing. Thanks a lot for your help I appreciate it.
 
  


Reply



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
ksh script - string comparison sharathkv25 Programming 3 09-19-2008 01:30 AM
string comparison in for loop harsshal Linux - Software 15 03-03-2008 11:39 PM
String Comparison gjagadish Programming 6 11-06-2007 03:34 AM
String comparison algorithm MicahCarrick Programming 4 04-11-2006 01:26 AM
Perl String comparison Xris718 Linux - General 5 04-01-2005 01:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:27 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
Open Source Consulting | Domain Registration