How to find the location of a bash file?

血红的双手。 提交于 2019-12-25 02:45:17

问题


I've made an executable jar file for a terminal game I've been working on. So far, I opened it by typing java -jar name.jar in the Terminal. This worked, but when I made a .sh file with the same command, the jar file couldn't be accessed. (situation [1])

Later, I realised that if I specify where the jar file is, it does open. (situation [2]) But the jar file and bash file are in the same folder called "playgame", and if I were to move that folder elsewhere, that file path specified in the bash file won't be valid anymore. So, is there a way to specify the location of the bash file, no matter where it is?

The content of the bash file in situation [1]:

#! /bin/bash
java -jar game.jar

(doesn't work)

situation [2]:

#! /bin/bash
java -jar Desktop/playgame/game.jar

(does work, but unreliable)

I have used chmod +rx bashfile.sh to make the bash file executable.

I have tried it with a .command file instead of .sh file, it did the same.

Also, I'm on a MacOS Mojave 10.14.2 if that's of any importance.

Thanks in advance!


回答1:


You can also use shebang

(printf '#! /usr/bin/env java -jar\n'; cat game.jar) > game
chmod +x game

and if game's dir is in PATH you can invoke just

$ game


来源:https://stackoverflow.com/questions/54503231/how-to-find-the-location-of-a-bash-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!