Thread: unix command line, grep a command line command's output in a script

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    80

    unix command line, grep a command line command's output in a script

    Code:
    #!/bin/bash
    
    
    if grep -q Safari.app `pa ax`; then
        echo found
    else
        echo not found
    fi
    Really clueless on scripts. Goal: run the command line call
    pa ax
    which'll give all the running processes, then grep for "Safari.app" in it and do something if found. Any ideas? Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try
    pa ax | grep -q Safari.app
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    (Oops, first, correction of me: it's 'ps ax' not 'pa ax')

    Hi, thanks for the suggestion. That works on the command line, but I don't know how it should be used in a script. I tried:

    Code:
    #!/bin/bash
    
    if ps ax | grep -q Safari.app; then
        echo found
    else
        echo not found
    fi
    which seemingly worked (output found when Safari was running), but then I changed 'Safari.app' to 'SafaXXri.app' and it still said found. Any suggestions? Thanks.

  4. #4
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Oh, I've worked out why. On the command line this
    ps ax | grep Safari.app
    outputs
    54882 ?? S 39:17.90 /Applications/Safari.app/Contents/MacOS/Safari -psn_0_24999894
    58634 s001 R+ 0:00.00 grep Safari.app
    two positive hits, two lines, one of which is the grep itself! So
    ps ax | grep SafXari.app
    on the command line outputs
    58632 s001 S+ 0:00.00 grep SafXari.app
    So it doesn't matter what you put there, it's always going to find one posative hit at least. So the problem isn't the way I've used the line in the script.
    So I suppose the question is, and it's probably quite a simple answer, how to filter out
    58632 s001 S+ 0:00.00 grep SafXari.app
    like lines from the grep output?
    Thanks.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use a different tool perhaps?
    Code:
    $ ps ax | grep firefox
     2305 ?        Sl     1:43 /usr/lib/firefox/firefox
     3725 pts/4    S+     0:00 grep --color=auto firefox
    $ pgrep firefox
    2305
    pgrep knows to exclude itself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Tried that but my command line hasn't got that command/tool.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe
    Code:
    $ ps ax | awk '$5 ~ /firefox/'
     2305 ?        Sl    17:58 /usr/lib/firefox/firefox
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    OK, thanks, but that doesn't work in the if situation! I looked in awk's manual page, having looked in grep's manual page to see what q meant, but couldn't see a quiet / silent option for awk. How to use awk in that if line?

  9. #9
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Or, how to assign the output of ps ax to a variable, then grep that variable, like what I'm trying to do here but what isn't working at all?:

    Code:
    #!/bin/bash
    
    
    var = ps ax
    if var | grep -q Safari.app; then
        echo found
    else
        echo not found
    fi

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    $ var=$(ps ax)
    $ echo $var | grep -q firefox
    $ echo $?
    0
    $ echo $var | grep -q zarniwoop
    $ echo $?
    1
    You can then write
    Code:
    if [ $? -eq 0 ]; then
    else
    fi
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Code:
    var=$(ps ax)
    echo $var | grep -q safari
    echo $?
    
    
    
    
    if [ $? -eq 0 ]; then
        echo found
    else
        echo not found
    fi
    outputs:
    1
    found

    Then putting safaXXri for the search, it still outputs:
    1
    found

    I have no idea what's going on!

    I guess this isn't possible in a script? edit: sorry, feeling a bit defeatist this morning

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're testing the result of echo $?, not the result of grep.

    Code:
    $ echo $var | grep -q zarniwoop
    $ if [ $? -eq 0 ]; then echo yes; else echo no ; fi
    no
    $ echo $var | grep -q firefox
    $ if [ $? -eq 0 ]; then echo yes; else echo no ; fi
    yes
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Ah, yup, that's working, many thanks Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line how to handle optional command.
    By ovid in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2010, 11:41 PM
  2. Having command line displayed in output
    By OxKing033 in forum C Programming
    Replies: 12
    Last Post: 03-19-2008, 11:25 AM
  3. Using unix command line to sort random array
    By lostmyshadow in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 07:14 PM
  4. sending output to a file from the command line...
    By GGrrl in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2003, 10:11 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

Tags for this Thread