简体   繁体   English

如何在没有 Brew 的情况下重新创建 Brew 别名?

[英]How to Recreate Brew Aliasing, Without Brew?

Due to a security update in my organisation, I can no longer use brew to tap into my organisation's repo to install a package. I can, however, manually download the.jar files that brew was installing.由于我组织的安全更新,我不能再使用 brew 进入我组织的存储库来安装 package。但是,我可以手动下载 brew 正在安装的 .jar 文件。

So previously I did:所以之前我做了:

brew tap <repo>
brew install <package>
<package> # Run the package from anywhere

And I could run the package from anywhere, just by typing in terminal.我可以从任何地方运行 package,只需在终端中输入即可。 Easy peasy.十分简单。

Normally Brew installs in usr/local/Cellar/<package>/some/internal/structure/<package.exe> .通常 Brew 安装在usr/local/Cellar/<package>/some/internal/structure/<package.exe>中。 But somewhere along the way it does something with aliases and symlinks and $PATH [which I am confused by] so that I can run the given package from /usr/local/bin , which is in my $PATH, by just typing <package> anywhere in terminal.但是在此过程中的某个地方,它使用别名和符号链接以及 $PATH [我对此感到困惑]进行了一些操作,以便我可以从/usr/local/bin运行给定的 package,它位于我的 $PATH 中,只需键入<package>在终端的任何地方。

I am trying to recreate this behaviour.我正在尝试重现这种行为。 I was able to manually download the jar files and put them in a folder /usr/local/bin/<package> .我能够手动下载 jar 文件并将它们放在文件夹/usr/local/bin/<package>中。 And if I run java -jar /usr/local/bin/<package>/<package.exe> then everything runs fine.如果我运行java -jar /usr/local/bin/<package>/<package.exe>然后一切运行正常。

How do I get it so that I can run <package> from anywhere in terminal, like with Brew?我如何获得它以便我可以从终端的任何地方运行<package> ,就像使用 Brew 一样? Also, just to be 100% clear, I want to choose the alias;另外,为了 100% 清楚,我想选择别名; I want to be able to type "abc" to run the jar files.我希望能够键入“abc”来运行 jar 文件。

/usr/local/bin/ is likely in your PATH variable already. /usr/local/bin/可能已经在您的PATH变量中。 If you want to check, print it to the terminal with echo "$PATH" .如果要检查,请使用echo "$PATH"将其打印到终端。 If it isn't, you can pick one of the other directories in there or add it to.如果不是,您可以选择其中一个其他目录或将其添加到。 If you want to add that directory to your PATH variable, you want to add this to the relevant dot file (likely ~/.bashrc ):如果你想将该目录添加到你的PATH变量,你想将其添加到相关的点文件(可能是~/.bashrc ):

export PATH="/usr/local/bin:$PATH"

PATH is a colon separated list of directories where your system will look for executables. PATH是一个冒号分隔的目录列表,您的系统将在其中查找可执行文件。

Now, you can just write a short script to run java for you.现在,您只需编写一个简短的脚本来为您运行 java。 For example, if we have a jar file called foo.jar, you could make a short script that runs java with the full path of foo.jar like this:例如,如果我们有一个名为 foo.jar 的 jar 文件,您可以使用 foo.jar 的完整路径制作一个运行 java 的简短脚本,如下所示:

/usr/local/bin/foo : /usr/local/bin/foo :

#!/bin/bash

java -jar '/path/to/foo.jar' "$@"

sneaky edit : Make sure you give this file executable permissions:偷偷摸摸的编辑:确保你给这个文件可执行权限:

chmod +x /usr/local/bin/foo

Now, on the terminal, if I run foo without a full path, it will run this script.现在,在终端上,如果我在没有完整路径的情况下运行foo ,它将运行这个脚本。

The "$@" is just going to pass any arguments you sent to this script along into the java program. "$@"只是将您发送到此脚本的任何 arguments 传递到 java 程序中。

This isn't the only option to achieve this.这不是实现这一目标的唯一选择。 You mentioned aliases as well.您也提到了别名。 You could write an alias to your.bashrc that does the same thing:你可以为 your.bashrc 写一个别名来做同样的事情:

alias foo='java -jar "path/to/foo.jar"'

A symlink wouldn't really be the best option here.符号链接在这里并不是最好的选择。 This would be okay if your jar file was not in the PATH and you wanted it there.如果您的 jar 文件不在PATH中并且您想要它在那里,这将没问题。 BUT , the PATH variable is really only for files that can be executed directly.但是PATH变量实际上只适用于可以直接执行的文件。 As you already know, jar files cannot.如您所知,jar 个文件不能。

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

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