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.
yep, you will. "then" is a bash builtin command, not mere syntax. Here it's passed as an option to the if, which it's not. just put the "then" on a new line.
Your code has plenty of other issues too btw... what's the ;; for? Did you copy that from a "case" example? delete it. Also you should not use "[", use "[[" instead for various boring reasons. Basically [ is the /bin/test command whilst [[ is a genuine builtin bash operation, so handles strings more logically. in your example, if $RESULT doesn't contain anything then it will cause an error, using [[ it will know that the variable is empty.
#!/bin/bash
RESULT=${RESULT:-"grep Started /var/log/jboss/mtext/server.log"}
LOG=${LOG:-"/var/log/jboss/mtext/server.log"}
if [[ -e $LOG ]]
then
echo "server.log exist"
if [[ -z $RESULT ]]
then
echo "Jboss started"
else
echo "JBoss not started"
fi
else
echo "server.log doesn't exist"
fi
I think the syntactic should be correct.
The server.log exist and the $RESULT is not empty but it shows even though
Code:
server.log exist (this is correct)
JBoss not started (this is not correct)
I add the echo and the script ölooks as follow now:
Code:
#!/bin/bash
RESULT=${RESULT:-"grep Started /var/log/jboss/mtext/server.log"}
LOG=${LOG:-"/var/log/jboss/mtext/server.log"}
if [[ -e $LOG ]]
then
echo "server.log exist"
echo "RESULT = " $RESULT
if [[ -z $RESULT ]]
then
echo "Jboss started"
else
echo "JBoss not started"
fi
else
echo "server.log doesn't exist"
fi
As I run this script I got:
Code:
server.log exist
RESULT = grep Started /var/log/jboss/mtext/server.log
JBoss not started
1.
One can see the RESULT is not empty.
2.
I wonder why the "grep Started /var/log/jboss/mtext/server.log" doesn't work.
is does "work" you're just setting that as a string.
just use this:
RESULT=$(grep whatever)
much simpler. No idea where you've picked up all this odd syntax from.
In terms of what that command is trying to do, I would suggest a different approach though:
RESULT=$(grep -c whatever)
if [[ $RESULT -gt 0 ]]
then
...
fi
i.e. count the number of times that grep finds a hit. more elegant that way. another common alternative would be to use the exit code of the grep, not the output:
grep -q whatever
if [[ $? -eq 0 ]]
then
... exit code was zero, so grep "worked"
fi
Last edited by acid_kewpie; 08-04-2011 at 06:48 AM.
#!/bin/bash
function grepLog(){
grep "Started in" /var/log/jboss/mtext/server.log
}
LOG=${LOG:-"/var/log/jboss/mtext/server.log"}
if [[ -e $LOG ]]
then
echo "server.log exist"
# echo $(grepLog)
result=$(grepLog)
echo $result
if [[ -z "$result" ]]
then
echo "JBoss started"
else
echo "JBoss not started"
fi
else
echo "server.log doesn't exist"
fi
The ran result is:
Code:
server.log exist
2011-08-04 12:32:54,098 INFO [org.jboss.bootstrap.microcontainer.ServerImpl] (main) JBoss (Microcontainer) [5.0.1 (build: SVNTag=JBPAPP_5_0_1 date=201003301050)] Started in 2m:19s:741ms
JBoss not started
One can see the result is not empty. But it show always "JBoss not started".
Thomas
Last edited by thomas2004ch; 08-04-2011 at 06:55 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.