他都, 我的 bash 脚本有问题。 那是我的代码:
#!/bin/bash
java -jar my_app.jar
echo "The present working directory is `pwd`"
如果我通过 ./script_name 执行它,它可以工作,但如果我双击它就不起作用,我得到这个错误:
“无法访问 jarfile my_app.jar”。
然后pwd输出不一样!!!
我的操作系统是 MacOSX,但我需要创建一个也适用于 Linux 的 bash 脚本。
最佳答案
我相信solution Shawn J. Goff 建议并由 Gordon Davisson 评论可以通过使用现代 bash
命令替换语法来改进。我假设脚本和 jar 在同一个文件夹中:
#!/bin/bash
java -jar "$(cd "$(dirname "$0")"; pwd)/my_app.jar"
echo The present working directory is $(cd "$(dirname "$0")"; pwd)
关于bash - 我的 Bash 脚本有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3675078/