如何找到bash文件的位置? [英] How to find the location of a bash file?

查看:759
本文介绍了如何找到bash文件的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为正在进行的终端游戏制作了一个可执行的jar文件.到目前为止,我通过在终端中键入java -jar name.jar来打开它.这行得通,但是当我使用相同的命令创建.sh文件时,无法访问jar文件. (情况[1])

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])

后来,我意识到如果指定jar文件在哪里,它确实会打开. (情况[2])但是jar文件和bash文件位于称为"playgame"的同一文件夹中,如果我将该文件夹移动到其他位置,则bash文件中指定的文件路径将不再有效. 因此,有一种方法可以指定bash文件的位置,无论它在哪里?

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?

情况[1]中bash文件的内容:

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

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

(无效)

情况[2]:

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

(可以,但是不可靠)

我已经使用chmod +rx bashfile.sh使bash文件可执行.

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

我尝试使用.command文件而不是.sh文件进行了尝试,

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

此外,如果这很重要,我正在使用MacOS Mojave 10.14.2.

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

谢谢!

推荐答案

您还可以使用舍邦

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

,如果game的目录在PATH中,则可以调用

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

$ game

这篇关于如何找到bash文件的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆