Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Reading Shell Script Results
July 5, 2005, 2:07 pm
rate this thread
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,
Re: Reading Shell Script Results
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.
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
Execute command via shell and return the complete output as a string
Hope that helps.
Re: Reading Shell Script Results
To the original poster, melih:
You could use
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
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,
and this is true
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
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
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
The result is that the above script will output
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
Again, those are single back quotes.
Do this to see the results of what was found:
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