Shell Script Problem

Discussion in 'macOS' started by IndianaJones, Mar 29, 2009.

  1. IndianaJones macrumors newbie

    Joined:
    Jan 30, 2009
    #1
    Hi all,

    I have a small script that doesnt work well enough. Here is the code.

    #!/bin/bash

    echo $1
    app=$1

    procname=`ps -ef | grep -i $app | grep -v grep`
    echo $procname

    output
    -------
    ./example firefox

    firefox
    501 247 134 0 16:13.29 ?? 67:29.49 /Applications/Firefox.app/Contents/MacOS/firefox-bin -psn_0_131104 501 4079 2386 0 0:00.00 ttys000 0:00.01 /bin/bash ./example firefox 501 4080 4079 0 0:00.00 ttys000 0:00.00 /bin/bash ./example firefox

    But when I execute the the following code on the bash shell the output is different.

    app=firefox
    procname=`ps -ef | grep -i $app | grep -v grep`
    echo $procname

    output
    -------
    501 247 134 0 16:41.88 ?? 68:48.90 /Applications/Firefox.app/Contents/MacOS/firefox-bin -psn_0_131104

    The outputs are different so what could be the problem?
    Thanks
     
  2. lostngone macrumors demi-god

    lostngone

    Joined:
    Aug 11, 2003
    Location:
    Anchorage
    #2
    You are greping for $app so grep is seeing the terminal/vty line of you entering your script name and what you are looking for(firefox).

    There are a LOT better ways of doing it but here is the dirty way to get it to work.

    procname=`ps -ef | grep -i $app | grep -v grep | grep -v example`
     
  3. belvdr macrumors 601

    Joined:
    Aug 15, 2005
    #3
    To ensure you can rename the script and not have to modify the code:

    Code:
    PGM=`basename $0`
    procname=`ps -ef | grep -i $app | grep -v grep | grep -v ${PGM}`
    
     
  4. IndianaJones thread starter macrumors newbie

    Joined:
    Jan 30, 2009

Share This Page