Do I need full Pathname to run a shell script?

Discussion in 'macOS' started by jammin0, Oct 3, 2010.

  1. jammin0 macrumors newbie

    Sep 27, 2010
    I'm trying to create a shell script or compile it into an application using automator that runs a JAR file. Everything works great except that I have to specify the full pathname in the shell script. I thought that ./ or .../ or something similar would denote that I want to run the .JAR file that is in the containing folder but I'm not having any luck.

    Anyone know if this is possible?
  2. calderone macrumors 68040


    Aug 28, 2009
    Details are lacking and honestly some of what you said doesn't make sense.

    Despite that, ./ only works if the script is in your working directory. The working directory in automator is the root of your home folder.

    So, if the script is in your home folder, you can work relatively from your home folder. Your best bet is to define a path variable in the beginning of your script. That way, if things move, you can easily change that variable.
  3. Hal Itosis macrumors 6502a

    Hal Itosis

    Feb 20, 2010
    As indicated above, the ../../ trick won't always work (reliably) from within a script.

    This perhaps...

    In a Bash shell script, we can locate its parent via:

    parent=$(cd "${0%/*}" && pwd -P)

    ... so i guess your jar file actually lives in the grandparent then?

    grandpa=$(dirname "$parent")

    ...making the full path to it something like:


    EDIT: Unless by "containing folder" you mean the script and the jar are in the same directory... in which case you can skip the grandparent stuff, and go directly to:


    EDIT2: A shorter way to get the script's parent is: parent=`dirname $0`... but that doesn't always produce its "full" pathname (e.g., like when a script is run via ./scriptname thus bypassing the $PATH mechanism).
  4. jammin0 thread starter macrumors newbie

    Sep 27, 2010
    Thanks for the replies. Let me try and explain. I apologize I am a long DOS user since over 15 years ago and my unix/mac os experience is limited.

    So I think Hal understood better what I am trying to do. I have a folder that contains a shell script whose function is simply to run a JAR file. It works great if I input the entire filename into the script.

    I would like to use this script on multiple machines with multiple users without having to change the filename in each script.

    What I said about Automator previously was not really relevant as I'm only using automator so that I can have an application file instead of having terminal run at the same time, it is going to confuse some of the users and I don't want the program to terminate because someone closes the terminal windows.

    Working Script
    java -jar -Dsun.java2d.noddraw=true /users/admin/downloads/my jar.jar

    What would work in DOS environment assuming that the jar and the script were in the same parent folder but doesn't work here:
    java -jar -Dsun.java2d.noddraw=true my jar.jar

    Hal you might have understood that but your explanation was a little bit beyond me. I think I already know the location of the file, I just want to not have to input it.

    Thanks in advance for any help, this forum is awesome.
  5. Hal Itosis macrumors 6502a

    Hal Itosis

    Feb 20, 2010
    What more can i say?
    Once you put this line in your script...

    parent=$(cd "${0%/*}" && pwd -P)

    ...then the variable "$parent" becomes the full path to the folder containing the script (no matter where it is, on any machine).

    So consequently (presumably),


    is the full path to your jar file.

    [need to use the actual name in place of "your" obviously, and follow normal Unix convention regarding quoting or escaping if there are any spaces (or weird chars) in its name.]

    EDIT: one could add in some extra sanity checks, and try to anticipate errors i suppose...
    parent=$(cd "${0%/*}" >/dev/null && pwd -P)
    [[ -d $parent ]] || {
    	echo "oops... '$parent' isn't a folder." >&2
    	exit 1
    If you're uncertain, use echo to test what the various vars look like.
  6. MisterMe macrumors G4


    Jul 17, 2002
    You seem to going all around Job's barn to do something that can also be done by simply double-clicking on the .jar file. Or am I missing something?
  7. jammin0 thread starter macrumors newbie

    Sep 27, 2010
    Good point but I need to specify other variables when I run it like in my example code. I am not simply running the JAR file or else you would be correct.
  8. jammin0 thread starter macrumors newbie

    Sep 27, 2010
    Finally figured out to use the Jar bundler instead of automator. I could never get your code to work Hal it must have been the fact that I was running the shell script from inside automator. Oh well, the jar bundler let me run the extra parameters that I need and packaged it up nicely as an application that I can run from wherever.

    Thanks everyone for the suggestions and help

Share This Page