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.
I want to write a script which can parse data I get from a java tool. Basically the tool prints out a number of lines, but I'm only interested in the last line, which basically gives the status of a server. I'd like to parse this but I'm not certain what tools I should use.
Take the following example output of the tool:
Code:
./status.sh SERVER1
... lots of junk ...
00I: The server "SERVER1" is STARTED
./status.sh SERVER1
... lots of junk ...
00I: The server "SERVER1" is STARTING
./status.sh SERVER1
... lots of junk ...
09I: The server "SERVER1" cannot be reached. It appears to be stopped.
What I'd like to do, is to be able to operate based on the data returned from the "status.sh" script. If someone could recommend what tools would be best for this job I'd really appreciate it (grep -E ? awk?). I don't need any source code (although pseudo code would be welcome ).
I probably should of said I don't really have an issue with getting the last line, its more a case of getting the status itself that's confusing me. Since I need to check for the different kinds of status.
Some pseudo code of what I'm trying to do would look like this:
Code:
populate a variable "STATUS" from status.sh
case $STATUS in
STARTED)
echo "...";;
STOPPED)
echo "...";;
STARTING)
echo "...";;
esac
The problem I have is with populating that STATUS variable. Perhaps it's just the way I'm thinking about it that's confusing me and this can be solved in an easier way.
The problem I have is with populating that STATUS variable. Perhaps it's just the way I'm thinking about it that's confusing me and this can be solved in an easier way.
So the format is always the same (the status is any text after ‘is’ on the last line)? In that case, what about the following:
Code:
STATUS=$( ./status.sh SERVER1 | sed -ne '${s/^.*is //;p}' )
If you mean you want the last word in a given line, you can get it with cut, which should be faster than awk. Grep only handles lines, so that wouldn't be what you want.
:echo '00I: The server "SERVER1" is STARTING' | cut -d' ' -f6
STARTING
If you want to perform an action based on status, you may be able to use the exit status.
:if ls nodir 2>/dev/null; then echo exists; else echo noexist; fi
noexist
You can also capture the specific code with $?.
If none of this helps, maybe restate the problem more precisely.
osor/digiot: Both methods work fine. Only slight problem is that for the server when it's stopped the last word is "stopped.". Not exactly a significant problem, just feels a little hackish (case's would be "STARTED", "STARTING" and "stopped.") I'm really just doing this as a learning experience so I'm not in any rush to get this completed. I'm more interested in ways to solve it.
Quote:
If you want to perform an action based on status, you may be able to use the exit status.
This could be worthwhile. The problem is status.sh basically just sets some environment variables and runs a java application, I've no idea if it will respect exit status', so I didn't even try. I'll be sure to try it tomorrow when I'm at the machine again.
I tried getting the exit status of the status.sh script, but unfortunately it always returned a successful "0".
ghostdog74/digiot:
That script works fine (using toupper()), and so far I have the following;
Code:
STATUS=""
# Get Server Status
get_server_status()
{
# grab the status of the server
STATUS=`./serverStatus.sh $SERVER | awk 'END{
pattern="STARTED|STARTING|STOPPED"
if ( toupper($0) ~ pattern) {
print toupper($NF)
}
}'`
case $STATUS in
STARTED)
echo "Server \"$SERVER\" is Started";;
STARTING)
echo "Server \"$SERVER\" is Starting";;
STOPPED.)
echo "Server \"$SERVER\" is Stopped";;
*)
echo "Status Unknown for \"$SERVER\"";;
esac
}
SERVER=Server1
get_server_status
SERVER=Server2
get_server_status
How does this look? Any suggestions for improvements? I'm not very familiar with bash coding etiquette so if there's a better way to do it I'm interested to hear
How does this look? Any suggestions for improvements? I'm not very familiar with bash coding etiquette so if there's a better way to do it I'm interested to hear
Cheers,
As far as less drastic revisions, I write functions as
Code:
get_server_status() {
foo
}
rather than
Code:
get_server_status()
{
foo
}
but I'm probably in the minority there and that's purely style and irrelevant to anything else.
Less trivially, unless there's a reason not to, I'd get in the habit of using $(foo) for command expansion rather than `foo`. I usually use `foo` on the command line because it's quicker but $(foo $(bar)) sort of stuff nests better. Lastly, functions can take arguments, so I'd probably delete the assignments to SERVER and say
and replace all $SERVER instances in the function with $1.
Actually, lastly lastly, you might want your functions and scripts returning exit codes. That's probably why serverStatus.sh returns 0 no matter what - it doesn't set an exit code and return >0 when it can't get a status. But, yeah, like ghostdog74 says, "keep up the good work".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.