macos 如何使shell脚本全局化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3560326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a shell script global?
提问by zebraman
I am on Mac's OS 10.6, and I am trying to learn a thing or two about shell scripting. I understand how to save a shell script and make it executable, but I am wondering what I can do or where I can save the file to make it global (that is, accessible no matter what folder I am in).
我在 Mac 的 OS 10.6 上,我正在尝试学习有关 shell 脚本的一两件事。我了解如何保存 shell 脚本并使其可执行,但我想知道我能做什么或我可以在哪里保存文件以使其成为全局文件(也就是说,无论我在哪个文件夹中都可以访问)。
For example, if I save a .sh file in the /Users/username/ directory and make it executable, I can only execute that script in that specific directory. If I navigate to /Users/username/Downloads, for example, I can't execute the script.
例如,如果我将 .sh 文件保存在 /Users/username/ 目录中并使其可执行,则我只能在该特定目录中执行该脚本。例如,如果我导航到 /Users/username/Downloads,我将无法执行该脚本。
Also, any suggestions of resources for learning more about shell scripting would be helpful. Thanks
此外,任何有关了解更多有关 shell 脚本的资源的建议都会有所帮助。谢谢
回答by unbeli
/usr/local/bin
would be the most appropriate location. Mac OS X has it in the PATH
by default
/usr/local/bin
将是最合适的位置。PATH
默认情况下,Mac OS X 中有它
回答by Bryan Oakley
Traditionally, such scripts either go in ~/bin
(ie: the bin directory in your home directory) or /usr/local/bin/
The former means the script will only work for you, the latter is for scripts you want anybody on the system to be able to run.
传统上,此类脚本要么进入~/bin
(即:您的主目录中的 bin 目录),要么/usr/local/bin/
前者意味着该脚本仅适用于您,后者则适用于您希望系统上的任何人都能够运行的脚本。
If you put it in ~/bin
, you may need to add that to your PATH environment variable. /usr/local/bin
should already be on the path.
如果你把它放进去~/bin
,你可能需要把它添加到你的 PATH 环境变量中。/usr/local/bin
应该已经在路上了。
回答by Piqué
There are two ways to do it -
有两种方法可以做到——
- Put your script in usr/local/bin and make sure it is executable(chmod +x my_script)(This is already set in the path, you can check by doing an echo $PATH)
- Create a folder in your home directory called bin. (For your personal scripts)
- cd ~ (Takes you to your home directory)
- mkdir bin (create a bin folder)
- vim .bash_profile (to set path environment variable)
- export PATH=~/bin:$PATH (Press i then add this line and then do esc and type :wq)
- Now you can just type the name of your script and run it from anywhere you want.
- 把你的脚本放在 usr/local/bin 中并确保它是可执行的(chmod +x my_script)(这已经在路径中设置了,你可以通过执行 echo $PATH 来检查)
- 在您的主目录中创建一个名为 bin 的文件夹。(对于您的个人脚本)
- cd ~ (带你到你的主目录)
- mkdir bin(创建一个bin文件夹)
- vim .bash_profile(设置路径环境变量)
- export PATH=~/bin:$PATH (按 i 然后添加这一行,然后执行 esc 并键入 :wq)
- 现在,您只需键入脚本的名称并从您想要的任何位置运行它。
** NOTE: If you want to run the script with a shortened command rather than typing your entire filename, add the following to your .bash_profile:alias myscript='my_script.sh'
Then you can run the script by simply typing myscript
. (you can sub in whatever alias you'd like)
**注意:如果您想使用缩短的命令运行脚本而不是输入整个文件名,请将以下内容添加到您的 .bash_profile 中:alias myscript='my_script.sh'
然后您只需输入.bashrc即可运行脚本myscript
。(您可以使用任何您喜欢的别名)
回答by AnonymousX
This one is super easy if you are familiar with your bashrc file! This will entirely use just your .bashrc file and takes 2 seconds to accomplish.
如果您熟悉 bashrc 文件,这将非常容易!这将完全使用您的 .bashrc 文件并需要 2 秒钟才能完成。
(I use Arch Linux Manjaro so I use .bashrc located in my home directory)
(我使用 Arch Linux Manjaro,所以我使用位于我的主目录中的 .bashrc)
The code to be placed in your .bashrc file:
要放置在 .bashrc 文件中的代码:
# Simple bashrc method to launch anything in terminal from any directory
YOURCOMMAND () {
cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}
As you can see, first you use the simple 'cd' command and give it the directory of the scripts location, then use '&&' so that you can make the next command executed right after, and finally open your script just as you would normally! Super easy and saved right in your .bash file! :)
如您所见,首先使用简单的 'cd' 命令并为其指定脚本位置的目录,然后使用 '&&' 以便您可以立即执行下一个命令,最后像您一样打开脚本一般!超级简单,直接保存在您的 .bash 文件中!:)
Hope I've helped someone!
希望我帮助过某人!
Sincerely,
真挚地,
AnonymousX
匿名X
回答by Sher Mohammad
In mac operating system
在 mac 操作系统中
- Open bash ~/.bashrc file.
- add path of your script in your bashrc file , using export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
- Open your ~./bash_profilefile and add [[ -s ~/.bashrc ]] && source ~/.bashrc
- open new terminal window Now whenever you will open your terminal your script will be loaded
- 打开 bash ~/.bashrc 文件。
- 在 bashrc 文件中添加脚本的路径,使用 export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
- 打开你的~./bash_profile文件并添加[[ -s ~/.bashrc ]] && source ~/.bashrc
- 打开新的终端窗口 现在,无论何时打开终端,您的脚本都会被加载
回答by purushothaman poovai
On using bash shell, write that script as function and then put it to the .bashrc or source the file which containing that function by "source file_name"
在使用 bash shell 时,将该脚本编写为函数,然后将其放入 .bashrc 或通过“源文件名”获取包含该函数的文件
Now execute the script by function call in the shell.
现在通过 shell 中的函数调用执行脚本。
回答by You
Either saving it in /usr/bin
(or any other directory present in PATH
) or editing PATH
to include the directory you saved it in will basically make it run in any directory.
将它保存在/usr/bin
(或存在于 中的任何其他目录PATH
)或编辑PATH
以包含您保存它的目录基本上会使它在任何目录中运行。
回答by d1gl3r
from the working directory of 'script.sh'" mv [script.sh] /usr/local/bin"( not tested but seems to be the least complex way IMO.)
来自'script.sh'" mv [script.sh] /usr/local/bin"的工作目录(未测试但似乎是IMO最简单的方式。)
回答by Frxstrem
You should put it in the global executable directory on your machine. I think that would usually be /usr/bin
on Unix-based operating systems (this would however most often require super user privileges on that machine).
你应该把它放在你机器上的全局可执行目录中。我认为这通常是/usr/bin
在基于 Unix 的操作系统上(但是,这通常需要该机器上的超级用户权限)。
You could also put it in any other directory that is in the $PATH
environment variable, although it would only work for those users who have that directory in that variable.
您也可以将它放在$PATH
环境变量中的任何其他目录中,尽管它只适用于在该变量中具有该目录的用户。
You can find the value of $PATH
by typing echo $PATH
in a shell. The directories are separated by:
.
您可以$PATH
通过echo $PATH
在 shell 中键入来找到的值。目录由:
.