SUSE / openSUSEThis Forum is for the discussion of Suse Linux.
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.
I have a command which is application related and I get a output some thing like this when I execute the command...
Quote:
application running
So I need to write script to see if application is running, if application state running run some command else run echo command to state application is not running.
So I need to run the first command, then based on output (Application status), run the second command else echo command.
Help will be appreciated.
if [[ $(command) = "application running" ]]
then this
else that
fi
Where:
command = the name of the command you're running.
this = the command(s) you want to run if it IS running.
else = the command(s) you want to run if it is NOT running.
Thanks for your reply...
I have an other question with if command...
if i have a command something like this...
Quote:
if [[ $(tail -1 /var/opt/tmp/application.log) = "Application - error" ]; then
echo "Application is not running"
else
echo "some command"
that works great if tail command exactly the same as "Application - error" and if it is not the same, let's say some thing like "Application - error errorcode" how can we define that. Error code wont be the same all the time.
if tail -1 /var/opt/tmp/application.log | grep "Application - error"
then
echo "Application is not running"
else
echo "some command"
The return code of the grep command will be non-zero (false) if the text you're looking for isn't found. You can see that by typing the first line (without the if) at command line and hitting return then typing "echo $?". If it finds it the $? will be 0 - if not it will be non-zero (typically 1 unless some other error occurs).
Note that "-" is used for signifying flags so you might have to escape it so grep knows you're looking for literal "-" by prepending it with a "\".
Also it seems you need negation here - put a "!" after the if to negate what follows it.
Disregard it - I misread what you were attempting to do.
You're saying if you see "error" it means the application isn't running. I was thinking you wouldn't see anything if the application isn't running so would need to negate it but that isn't the case.
Negation is sometimes needed if you're only doing an "if" and "then" but not an "else".
For example if you ran:
Code:
ps -ef |grep -v grep |grep java
It returns a zero exit status if it is running and a non-zero exit status if it isn't.
If you wanted to only do something if it was NOT running then you'd do:
Code:
if ! ps -ef |grep -v grep |grep java
then java -xm (etc...)
fi
That would tell it to start the java process you're interested in ONLY if it is not running.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.