简体   繁体   English

Linux / Ubuntu为sh文件创建可执行命令

[英]Linux/Ubuntu create executable command for sh file

I would like to create executable command for Ubuntu. 我想为Ubuntu创建可执行命令。

I have /dir/bin/start.sh 我有/dir/bin/start.sh

I can execute it this way 我可以这样执行

./dir/bin/start.sh

But I was wondering how to put it to Ubuntu configuration or something, so it will be possible to invoke it from shell with just 但是我想知道如何将其放入Ubuntu配置或其他东西,因此可以仅通过shell调用它

start

You can do this by either: 您可以通过以下任一方式执行此操作:

  • Add the directory of the command to the $PATH variable, thus: 将命令目录添加到$PATH变量中,因此:

     export PATH=$PATH:/dir/bin 

    To do this persistent, you must add this line to the ~/.profile file: 要持久执行此操作,必须~/.profile下行添加到~/.profile文件中:

      echo 'export PATH=$PATH:/dir/bin' >> ~/.profile 
  • Create a shortcut (for instance with ln -s '/dir/bin/start.sh' start ) in a directory that is added to the path; 在添加到路径的目录中创建快捷方式(例如,使用ln -s '/dir/bin/start.sh' start );

     cd <pathdirectory> ln -s '/dir/bin/start.sh' start 
  • Create an alias so that the shell simply replaces the command: 创建一个别名,以便外壳程序简单地替换命令:

     echo 'alias start=./dir/bin/start.sh' >> ~/.profile 

How/Why does this work 工作方式/原因

If you type a command in the shell, it will first look for aliases, and replace the aliases. 如果您在外壳程序中键入命令,它将首先查找别名,然后替换别名。 So if you have defined an alias for start , it will simply replace is by ./dir/bin/start.sh ; 因此,如果您已经为start定义了别名,它将被替换为./dir/bin/start.sh ; so as if you have written it yourself. 就像你自己写的一样。

Next the shell will try to find the interpretation of the command. 接下来,shell将尝试查找命令的解释。 If the command is a file (for instance by using a alias), the file is executed. 如果命令是文件(例如,使用别名),则执行该文件。 Otherwise, the system falls back on $PATH s. 否则,系统将退回到$PATH

$PATH s is a sequence of paths where the system searches for a file with that name. $PATH s是系统在其中搜索具有该名称的文件的路径序列。 It searches the paths in that order and searches through all the files for a file with the same name. 它以该顺序搜索路径,并在所有文件中搜索名称相同的文件。 If such file exists, it runs that file. 如果存在该文件,它将运行该文件。

You can thus do a few things: 因此,您可以做一些事情:

  • Place such program in the directory; 将这样的程序放在目录中; but that's not really a nice option, because different projects are then grouped together; 但这并不是一个很好的选择,因为然后将不同的项目组合在一起;
  • Use a shortcut (the ln solution), in that case you only add a "representative" in the path folder; 使用快捷方式( ln解决方案),在这种情况下,您只需在路径文件夹中添加“代表”即可;
  • Extend the number of paths by performing the PATH=$PATH:/dir/bin , so that it will search your project as well. 通过执行PATH=$PATH:/dir/bin扩展路径数,以便它也可以搜索您的项目。

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

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