LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > SUSE / openSUSE
User Name
Password
SUSE / openSUSE This Forum is for the discussion of Suse Linux.

Notices


Reply
  Search this Thread
Old 06-16-2010, 03:48 PM   #1
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Rep: Reputation: 15
Need help with script


Hi All,

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.

Thanks.
 
Old 06-16-2010, 03:54 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
http://www.freeos.com/guides/lsst/ch03sec03.html
http://www.freeos.com/guides/lsst/
 
0 members found this post helpful.
Old 06-16-2010, 03:57 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
Old 06-17-2010, 10:23 AM   #4
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Original Poster
Rep: Reputation: 15
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.

Thanks once again.
 
Old 06-17-2010, 12:28 PM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Code:
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.
 
Old 06-18-2010, 08:52 AM   #6
s_linux
Member
 
Registered: Jul 2009
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks for your help, that is really works...

But I'm confused with this
Quote:
Also it seems you need negation here - put a "!" after the if to negate what follows it.
I don't really understand. if Could you please give me little more info on it that would appreciated.

Thanks again.
 
Old 06-18-2010, 09:37 AM   #7
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
  


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
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 04:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > SUSE / openSUSE

All times are GMT -5. The time now is 11:02 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