简体   繁体   English

如何在Mac OS X上的bash脚本中更改当前工作目录?

[英]How to change current working directory within a bash script on Mac OS X?

I'm struggling with a little problem, I never wrote a bash script before and it may be a little thing for someone who knows how it's done correctly. 我正在努力解决一个小问题,我以前从未写过一个bash脚本,对于知道它是如何正确完成的人来说这可能是一件小事。

I have the following folder structure (cannot be changed): 我有以下文件夹结构(无法更改):

MyApp.app
   |_Contents
       |_Java
           |_MyApp.jar
       |_MacOS
           |_launch.sh

The launch.sh script should start the MyApp.jar executable and currently looks as follows: launch.sh脚本应该启动MyApp.jar可执行文件,目前看起来如下:

#!/bin/bash
SCRIPT_DIR=$(cd "$(dirname "$0")/../Java"; pwd)
exec "$SCRIPT_DIR/jre/bin/java" -Xms256m -XX:PermSize=64m -jar "MyApp.jar"

When starting the launch.sh script, the second line should change the current working directory from MyApp.app/Contents/MacOS to MyApp.app/Contents/Java in order to call subsequently java -jar MyApp.jar (3rd line) with its correct root directory as working directory... 启动launch.sh脚本时,第二行应将当前工作目录从MyApp.app/Contents/MacOS更改MyApp.app/Contents/Java ,以便随后调用java -jar MyApp.jar (第3行)及其将根目录更正为工作目录...

But as soon as the MyApp.jar starts, I print out the current working directory within my Java application with: 但是一旦MyApp.jar启动,我就打印出我的Java应用程序中的当前工作目录:

System.out.println(System.getProperty("user.dir"));

... and it prints out the directory of the launch.sh script, namely MyApp.app/Contents/MacOS instead of MyApp.app/Contents/Java . ...并打印出launch.sh脚本的目录,即MyApp.app/Contents/MacOS而不是MyApp.app/Contents/Java

Any ideas how to properly change the directory within the bash scripts are highly appreciated : ) ... ?! 如何正确更改bash脚本中的目录的任何想法都非常感激:)...?!

You could always work with the cd command (change directory). 您始终可以使用cd命令(更改目录)。 But a more optimal solution, if you had to change directories, may be using the pushd & popd commands to build a stack. 但是, 如果必须更改目录,更优化的解决方案可能是使用pushdpopd命令来构建堆栈。

In this case scenario you could hardcode the file path(s) that the script will have to navigate to during execution : 在这种情况下,您可以硬编码脚本在执行期间必须导航到的文件路径:

_java=/Applications/MyApp.app/_Contents/_Java;

Then using pushd and popd (push directory onto stack, pop directory off of stack) : 然后使用pushd和popd(将目录推入堆栈,从堆栈弹出目录):

# Currently in /Applications/MyApp.app/_Contents/_MacOS/
pushd $_java;

# Do what you have to do in the _Java working directory (./ = current working directory)
exec "./jre/bin/java" -Xms256m -XX:PermSize=64m -jar "MyApp.jar";

# Print the working directory and return to bottom of stack
pwd; popd;

Nothing against the usage of cd, but pushd & popd answer the question as well and are more flexible than cd in some situations, especially when you really have to traverse a file hierarchy on several occasions during execution. 没有什么可以反对cd的使用,但pushdpopd回答了这个问题,并且在某些情况下比cd更灵活,特别是在执行期间你必须多次遍历文件层次结构时。 for more information man pushd , man popd , google or related articles such as : https://unix.stackexchange.com/questions/77077/how-do-i-use-pushd-and-popd-commands 更多信息man pushdman popd ,google或相关文章,例如: https//unix.stackexchange.com/questions/77077/how-do-i-use-pushd-and-popd-commands

Besides that, even for day-to-day usage, cd is great, but in my case I always move around and it's nice to be in my /Library/LaunchDaemons/ and to simply pushd ~/Documents/Scripts/ ; 除此之外,即使是日常使用,CD也很棒,但在我的情况下,我总是四处走动,很高兴能够进入我的/ Library / LaunchDaemons /并简单地 pushd ~/Documents/Scripts/ ; vim bashp0rn.sh ; vim bashp0rn.sh ; popd ; popd ; and back in /Library/LaunchDaemons/ to load & launch ( launchctl ). 然后返回/ Library / LaunchDaemons /加载和启动( launchctl )。

Now , with that said, you don't have to change to your apps working directory in order to launch the application : 现在 ,说到这一点,您无需更改应用程序工作目录即可启动应用程序:

... in order to call subsequently java -jar MyApp.jar (3rd line) with its correct root directory as working directory ... ...为了随后调用java -jar MyApp.jar(第3行),其正确的根目录作为工作目录...

If that is your sole requirement && if that is your requirement because in this case you are trying to complete the path by assigning pwd 's output to $SCRIPT_DIR , then I would suggest hardcoding the full path and using the eval command instead as such : 如果这是你的唯一要求&&如果这是你的要求,因为在这种情况下你试图通过将pwd的输出分配给$SCRIPT_DIR来完成路径,那么我建议硬编码完整路径并使用eval命令代替:

#!/bin/bash

app='/Applications/MyApp.app/_Contents/_Java/jre/bin/java -Xms256m -XX:PermSize=64m -jar "MyApp.jar"';

Now from anywhere in the file hierarchy you can evaluate the variable : 现在,从文件层次结构的任何位置,您都可以评估变量:

eval $app;

Try to do it like this: 尝试这样做:

cd "$(dirname "$0")/../Java"
SCRIPT_DIR=$(pwd)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM