OSX How do I have a Shell script change directory to the one the script is in?
Asked Answered
V

4

6

I'm using Apple's automator to create a Shell Script. I can get it to run if I change directory specifically to where the jar file is. But what if I want to change to directory to wherever the Shell script happens to be running?

Right now I have the following, which works:

cd desktop/CommonDenom/
java -XstartOnFirstThread -jar CommonDenom.jar

I know there's a way to target whatever directory the Shell script is launched from, but I can't seem to get anything to work. Please be specific with instructions as I havent been using Automator very long. Unless someone can specify how ot writ ethe script without Automator. Thanks in advance.

Verjuice answered 9/3, 2014 at 16:10 Comment(0)
I
4

A standard idiom for this in shell scripts is dirname $0. The $0 variable is the path to the script that was executed, and the dirname command takes a path and strips off the last component to leave the path to the containing directory

cd "`dirname $0`"
Inharmonious answered 9/3, 2014 at 19:4 Comment(6)
Thanks. This question kind of answers itself, but if you'd please confirm: cd "dirname $0" is specifically a shell script command, correct? This fails in terminal and rightly so, for it has no context if not part of a script.Verjuice
@ Ian Roberts cd "dirname $0" doesn't work. It comes back with "The action "Run Shell Script" encountered an error. Check the action's properties and try running the workflow again. Here's what I'm trying: cd "dirname $0" cd CommonDenom/ java -XstartOnFirstThread -jar CommonDenom.jar And here is what works: cd /Applications/CommonDenom java -XstartOnFirstThread -jar CommonDenom.jar The She script is in Macintosh HD/Applications. The rest of the files incuding the jar are in /Macintosh HD/Appications/CommonDenom/Verjuice
wait. are those ticks supposed to be in there? cd "<tick>dirname $0<tick>" or were you just putting those in to format for the post?Verjuice
@StuartKuredjian the back ticks are necessary. They're the way to run one command and capture its output in a string for use as part of another command.Inharmonious
Thanks. It doesn't seem to be working though. I've created an Automator Application with the "Run Shell Script" action that has the following lines: ' cd "dirname $0"' open test.txt And it returns the error: The action "Run Shell Script" encountered an error. Check the action's properties and try running the workflow again."Verjuice
FYI, I just used this in a shell script on my Mac and changed it slightly due to some odd errors. The extra quotes around $0 might be necessary if there are spaces in the path. cd "`dirname -- "$0"`"Cone
M
1

Just wanted to weigh in here. I've seen some people with this problem. If you are JUST on OSX and making your own scripts. This will do the trick :) kind of a hack, but works like a charm.

#! /bin/bash 

sudo /Applications/XAMPP/xamppfiles/xampp startapache

open /Applications/XAMPP/htdocs
Misbecome answered 26/9, 2014 at 13:48 Comment(0)
S
1
#!/bin/bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd "${DIR}"

...the rest of your script...

Credits to Ian C. on AskDifferent: https://apple.stackexchange.com/a/179064

Supervisor answered 7/3, 2020 at 10:3 Comment(0)
E
0

I am not sure you mean "you want to change directory to wherever the script is run from" because you will be in the directory the script is run from when you start anyway, so there will be no need to change directory there. I think you mean you want the script to stay wherever it starts, yet still be able to find the jar file.

In which case, you probably jus need the following without any changing directory:

java -XstartOnFirstThread -jar ~/Desktop/CommonDemon/CommonDemon.jar
Eure answered 9/3, 2014 at 16:45 Comment(3)
"because you will be in the directory the script is run from when you start anyway" - no, you don't necessarily are. Assume your current working directory is /foo/bar, then you can run a script using /some/other/dir/someScripts.sh and the current directory will not be the one where the script is locatedUnpleasantness
Good point, thank you. Maybe the OP can help out with a clearer statement of what he wants...Eure
Mark is correct. If the .jar file that is being launched is placed in Applications or Desktop or wherever, when Terminal first opens it is not in the same directory as the launched file. Therefore I need a way to tell the script to first change to the same directory the launched file is. I had followed the instructions using dirnmame $0 in another post and it wasn't working. I'll try it again now that i have fresh eyes,Verjuice

© 2022 - 2024 — McMap. All rights reserved.