向路径添加bash脚本

时间:2021-10-09 00:26:09

I want to add a small script to the linux PATH so I don't have to actually run it where it's physically placed on disk.

我想向linux路径添加一个小脚本,这样我就不必在物理地将它放在磁盘上的地方运行它。

The script is quite simple is about giving apt-get access through a proxy I made it like this:

这个脚本非常简单,它是关于通过我这样做的代理访问apt-get的:

#!/bin/bash
array=( $@ )
len=${#array[@]}
_args=${array[@]:1:$len}
sudo http_proxy="http://user:password@server:port" apt-get $_args

Then I saved this as apt-proxy.sh, set it to +x (chmod) and everything is working fine when I am in the directory where this file is placed.

然后我将它保存为apt-proxy。sh,设置为+x (chmod),当我在这个文件所在的目录时,一切正常。

My question is : how to add this apt-proxy to PATH so I can actually call it as if it where the real apt-get ? [from anywhere]

我的问题是:如何将这个apt-proxy添加到PATH中,这样我就可以把它叫做真正的apt-get了?(从任何地方)

Looking for command line only solutions, if you know how to do by GUI its nice, but not what I am looking for.

寻找命令行唯一的解决方案,如果您知道如何使用GUI它的nice,但不是我正在寻找的。

3 个解决方案

#1


24  

Try this:

试试这个:

  • Save the script as apt-proxy (without the .sh extension) in some directory, like ~/bin.
  • 将脚本保存为某个目录(如~/bin)中的apt-proxy(没有.sh扩展名)。
  • Add ~/bin to your PATH, typing export PATH=$PATH:~/bin
  • 向路径添加~/bin,输入export PATH=$PATH:~/bin
  • If you need it permanently, add that last line in your ~/.bashrc. If you're using zsh, then add it to ~/.zshrc instead.
  • 如果您永远需要它,请在您的~/.bashrc中添加最后一行。如果您正在使用zsh,那么将它添加到~/中。zshrc代替。
  • Then you can just run apt-proxy with your arguments and it will run anywhere.
  • 然后你可以用你的参数运行apt-proxy,它可以在任何地方运行。

Note that if you export the PATH variable in a specific window it won't update in other bash instances.

注意,如果您在一个特定的窗口中导出PATH变量,它将不会在其他bash实例中更新。

#2


7  

You want to define that directory to the path variable, not the actual binary e.g.

您需要将该目录定义为path变量,而不是实际的二进制文件。

PATH=$MYDIR:$PATH

where MYDIR is defined as the directory containing your binary e.g.

MYDIR被定义为包含二进制文件的目录。

PATH=/Users/username/bin:$PATH

You should put this in your startup script e.g. .bashrc such that it runs each time a shell process is invoked.

您应该将它放在启动脚本中,例如.bashrc,以便在每次调用shell进程时运行它。

Note that order is important, and the PATH is evaluated such that if a script matching your name is found in an earlier entry in the path variable, then that's the one you'll execute. So you could name your script as apt-get and put it earlier in the path. I wouldn't do that since it's confusing. You may want to investigate shell aliases instead.

注意,顺序很重要,并且对路径进行评估,以便如果在PATH变量的早期条目中找到匹配您名字的脚本,那么您将执行该脚本。因此,您可以将脚本命名为apt-get,并将其放在路径的前面。我不会这么做,因为这很让人困惑。您可能需要研究shell别名。

I note also that you say it works fine from your current directory. If by that you mean you have the current directory in your path (.) then that's a potential security risk. Someone could put some * variant of a common utility (e.g. ls) in a directory, then get you to cd to that directory and run it inadvertently.

我还注意到您说它从当前目录运行良好。如果您的意思是您的路径(.)中有当前目录,那么这就是潜在的安全风险。有人可以在一个目录中放置一个常用工具(例如ls)的某种木马变体,然后将您的cd发送到该目录并在不经意间运行它。

#3


0  

make an alias to the executable into the ~/.bash_profile file and then use it from anywhere or you can source the directory containing the executables you need run from anywhere and that will do the trick for you.

将可执行文件的别名改为~/。bash_profile文件,然后从任何地方使用它,或者您可以从包含需要从任何地方运行的可执行文件的目录中找到它,这将为您提供解决方案。

#1


24  

Try this:

试试这个:

  • Save the script as apt-proxy (without the .sh extension) in some directory, like ~/bin.
  • 将脚本保存为某个目录(如~/bin)中的apt-proxy(没有.sh扩展名)。
  • Add ~/bin to your PATH, typing export PATH=$PATH:~/bin
  • 向路径添加~/bin,输入export PATH=$PATH:~/bin
  • If you need it permanently, add that last line in your ~/.bashrc. If you're using zsh, then add it to ~/.zshrc instead.
  • 如果您永远需要它,请在您的~/.bashrc中添加最后一行。如果您正在使用zsh,那么将它添加到~/中。zshrc代替。
  • Then you can just run apt-proxy with your arguments and it will run anywhere.
  • 然后你可以用你的参数运行apt-proxy,它可以在任何地方运行。

Note that if you export the PATH variable in a specific window it won't update in other bash instances.

注意,如果您在一个特定的窗口中导出PATH变量,它将不会在其他bash实例中更新。

#2


7  

You want to define that directory to the path variable, not the actual binary e.g.

您需要将该目录定义为path变量,而不是实际的二进制文件。

PATH=$MYDIR:$PATH

where MYDIR is defined as the directory containing your binary e.g.

MYDIR被定义为包含二进制文件的目录。

PATH=/Users/username/bin:$PATH

You should put this in your startup script e.g. .bashrc such that it runs each time a shell process is invoked.

您应该将它放在启动脚本中,例如.bashrc,以便在每次调用shell进程时运行它。

Note that order is important, and the PATH is evaluated such that if a script matching your name is found in an earlier entry in the path variable, then that's the one you'll execute. So you could name your script as apt-get and put it earlier in the path. I wouldn't do that since it's confusing. You may want to investigate shell aliases instead.

注意,顺序很重要,并且对路径进行评估,以便如果在PATH变量的早期条目中找到匹配您名字的脚本,那么您将执行该脚本。因此,您可以将脚本命名为apt-get,并将其放在路径的前面。我不会这么做,因为这很让人困惑。您可能需要研究shell别名。

I note also that you say it works fine from your current directory. If by that you mean you have the current directory in your path (.) then that's a potential security risk. Someone could put some * variant of a common utility (e.g. ls) in a directory, then get you to cd to that directory and run it inadvertently.

我还注意到您说它从当前目录运行良好。如果您的意思是您的路径(.)中有当前目录,那么这就是潜在的安全风险。有人可以在一个目录中放置一个常用工具(例如ls)的某种木马变体,然后将您的cd发送到该目录并在不经意间运行它。

#3


0  

make an alias to the executable into the ~/.bash_profile file and then use it from anywhere or you can source the directory containing the executables you need run from anywhere and that will do the trick for you.

将可执行文件的别名改为~/。bash_profile文件,然后从任何地方使用它,或者您可以从包含需要从任何地方运行的可执行文件的目录中找到它,这将为您提供解决方案。