Reading Shell Script Results

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • melih.onvural@gmail.com

    #1

    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

  • Melih Onvural

    #2
    Re: Reading Shell Script Results

    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

    Comment

    • Erwin Moller

      #3
      Re: Reading Shell Script Results

      melih.onvural@g mail.com wrote:
      [color=blue]
      > 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[/color]

      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.



      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

      Comment

      • jroseve

        #4
        Re: Reading Shell Script Results


        To the original poster, melih:

        You could use
        [color=blue]
        > if [ "`tail file | grep blah`" ]; then
        > echo got it
        > fi[/color]

        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
        [color=blue]
        > ""[/color]

        and this is true
        [color=blue]
        > "anything in the quotes"[/color]

        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
        [color=blue]
        > tail file | grep blah[/color]

        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
        [color=blue]
        > execute what is inside of the quotes and put the output of the
        > execution here[/color]

        The result is that the above script will output
        [color=blue]
        > got it[/color]

        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:
        [color=blue]
        > found=`tail file`[/color]

        Again, those are single back quotes.

        Do this to see the results of what was found:
        [color=blue]
        > echo $found[/color]

        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

        Comment

        Working...