script for executing a program and doing string comparison
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.