473,416 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Reading Shell Script Results

Group,

I want to get into a remote server, tail a file, and see if the last
line is an error or not. I think that I've figured out how to shell
over and tail the file. I have the specific server information and
filename/location all stored in a database. I can't figure out how to
get the result of the tail into a variable so that I can search it for
an ERROR string. Any ideas?

thanks in advance,

melih

Jul 17 '05 #1
3 2521
Sorry to clutter everyone's box, but it turns out that exec is already
built to do this. The answer to the question is use the exec function.

melih

Jul 17 '05 #2
me***********@gmail.com wrote:
Group,

I want to get into a remote server, tail a file, and see if the last
line is an error or not. I think that I've figured out how to shell
over and tail the file. I have the specific server information and
filename/location all stored in a database. I can't figure out how to
get the result of the tail into a variable so that I can search it for
an ERROR string. Any ideas?

thanks in advance,

melih


Hi,

If you can write the command (including the tail) and execute succesfully in
a shell, you can also give it to PHP and let PHP execute it.
Then you catch the result.

http://nl2.php.net/manual/en/ref.exec.php

It contains many usefull links to functions you might want to use.

I think shell_exec is usefull for you, but just scan through them and decide
for yourself.

shell_exec:
Execute command via shell and return the complete output as a string

Hope that helps.

Regards,
Erwin Moller
Jul 17 '05 #3

To the original poster, melih:

You could use
if [ "`tail file | grep blah`" ]; then
echo got it
fi
to check if the tail end of file "file" contains "blah", and if it
does, then do something like echo "got it". This is just an example.
I don't know what file you're tailing, what you're looking for in it,
or what you do when you find it. Maybe this will give you some ideas.

Let me explain what this does. Block if statements in bash shell
scripts (seems to me that you are using bash? I'm a Linux programmer
and new to Mac OSX.) well they start with "if" and end with "fi". In
between go the instructions you want to execute, on one or more lines.
To keep it simple, put one instruction on each line.

Notice the use of square brackets in the first line. The left one has
a space on both sides. This matters. The right one has a space on
the
left and a semicolon on the right followed by a space. This matters
also. In the middle is a string delimited by double quotes. For the
moment ignore what is in the double quotes. What we are doing here
just testing the logical value of a string. Well, this is easy. When
done this way a string is true when it is not empty. For example,
this
is false
""
and this is true
"anything in the quotes"
So what have I put in the quotes? I have put in the quotes the result
of "tail file" piped through "grep blah". Try this at the command
line
tail file | grep blah
where "file" is some file in your current directory and "blah" is some
text that file either contains or doesn't contain. Try it both
ways--blah as something in the file and blah as something not in the
file. Note | is a pipe. It is a character that often shares the \
key. It is not
a 1, l or I (one, letter L or letter I) even though it might look like
that.

You will see that if grep finds blah in the tail end of the file that
the above test prints something.

So now look at the ` marks that are just inside the " marks. These
marks are called "single back quotes". The key often shares the ~.
Not the ' which shares the ". A pair of single back quotes means
execute what is inside of the quotes and put the output of the
execution here
The result is that the above script will output
got it
If file "file" contains "blah".

Does all this make sense? The bash shell scripting language is a
venerable, old language, and it is in some ways archaic. Yet, it is
still very widely used and is awesomely powerful especially when used
together with the normal linux command set.

I'm going to stop in a moment, as I've probably given too much help
already. One more clue. You asked for a way to put the results of
tail into a variable. I don't know why you want to do it, but try
this:
found=`tail file`
Again, those are single back quotes.

Do this to see the results of what was found:
echo $found


Have fun!

-Joe
--
jroseve
------------------------------------------------------------------------
jroseve's Profile: http://www.macosx.com/forums/member.php?userid=38248
View this thread: http://www.macosx.com/forums/showthread.php?t=229387
macosx.com - The Answer to Mac Support - http://www.macosx.com

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Laura P | last post by:
Hi, I wasn't sure whether this should be posted in the Java are or in a Solaris thread, so I shall post it in both. Sorry for the duplication. I am new to Solaris and am having trouble...
1
by: speedster | last post by:
Hi. I need some help converting some php to perl. $meminfo = shell_exec( "free -o" ); I need to execute "free-o" in shell and get the results. It grabs memory information about a linux...
2
by: Shabam | last post by:
Up to now I've been doing this manually via the command shell. However I'm sure I can automate this using a script with better results. Can someone show me how I can do this? cp -pr...
1
by: David Kanter | last post by:
Hi, I have some VB scripts which start by opening an application, in this case it is a game called FEAR (some of you may be familiar). I am using Visual Studio 2005 Professional Edition running...
4
by: Anastasios Hatzis | last post by:
I'm looking for a pattern where different client implementations can use the same commands of some fictive tool ("foo") by accessing some kind of API. Actually I have the need for such pattern for...
3
by: telduivel | last post by:
Can someone please help me with this: I have a python script, that at some point calls a linux bash script (.sh). Starting the shell script is the last thing my python script needs to do, so I...
5
by: dg.google.groups | last post by:
Hi all, Is there any standard way to tell if the user is running from a module or from an interactive shell like IDLE or IPython? The best I've come up with so far is for a function to look at...
5
by: inetquestion | last post by:
I am looking for a web interface for shell commands or shell scripts. Does anyone know of any exexisting php scripts which would solve this requirement? PHP form accepts input from a user, then...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.