如何在不使用“sh”或“bash”命令的情况下运行shell脚本?

时间:2022-11-15 21:48:54

I have a shell script which I want to run without using the "sh" or "bash" commands. For example:

我有一个shell脚本,我想在不使用“sh”或“bash”命令的情况下运行它。例如:

Instead of: sh script.sh

而不是:sh script.sh

I want to use: script.sh

我想使用:script.sh

How can I do this?

我该怎么做呢?

P.S. (i) I don't use shell script much and I tried reading about aliases, but I did not understand how to use them.

P.S. (i)我不怎么使用shell脚本,我试着阅读别名,但是我不知道如何使用它们。

(ii) I also read about linking the script with another file in the PATH variables. I am using my university server and I don't have permissions to create a file in those locations.

(ii)我还阅读了关于将脚本与路径变量中的另一个文件链接的内容。我正在使用我的大学服务器,我没有权限在这些位置创建一个文件。

8 个解决方案

#1


232  

Add a "shebang" at the top of your file:

在你的文件顶部添加一个“shebang”:

#!/bin/bash

And make your file executable (chmod +x script.sh).

并使您的文件可执行(chmod +x script.sh)。

Finally, modify your path to add the directory where your script is located:

最后,修改路径,添加脚本所在的目录:

export PATH=$PATH:/appropriate/directory

(typically, you want $HOME/bin for storing your own scripts)

(通常,您需要$HOME/bin来存储自己的脚本)

#2


43  

These are some of the pre-requisites of using directly the script name:

这些是直接使用脚本名称的一些先决条件:

  1. Add the sha-bang {#!/bin/bash) line at the very top.
  2. 在最顶端添加sha-bang {#!/bin/bash)行。
  3. Using chmod u+x scriptname make the script executable.
  4. 使用chmod u+x scriptname使脚本可执行。
  5. Place the script under /usr/local/bin folder.
  6. 将脚本放在/usr/local/bin文件夹下。
  7. Run the script using just the name of the script.
  8. 只使用脚本的名称运行脚本。

Note: The reason I suggested to place it under /usr/local/bin folder is because most likely that will be path already added to your PATH variable.

注意:我建议将它放在/usr/local/bin文件夹下的原因是,最有可能的是已经添加到path变量的路径。

Update:

If you don't have access to the /usr/local/bin folder then do the following:

如果您没有访问/usr/local/bin文件夹的权限,请执行以下操作:

  1. Create a folder in your home directory and let's call it myscripts.
  2. 在主目录中创建一个文件夹,我们将其命名为myscripts。
  3. Do ls -lart on your home directory, to identify the start-up script your shell is using. It should either be .profile or .bashrc.
  4. 在主目录上执行ls -lart,以标识shell正在使用的启动脚本。它应该是.profile或者.bashrc。
  5. Once you have identified the start up script, add the following line in your script - export set PATH=$PATH:~/myscript.
  6. 确定了启动脚本之后,在脚本中添加以下行——导出设置路径=$PATH:~/myscript。
  7. Once added, source your start-up script or log out and log back in.
  8. 一旦添加,为您的启动脚本提供源代码或注销并重新登录。
  9. Execute your script using script name.
  10. 使用脚本名称执行脚本。

#3


12  

Just make sure it is executable, using chmod +x. By default, the current directory is not on your PATH, so you will need to execute it as ./script.sh - or otherwise reference it by a qualified path. Alternatively, if you truly need just script.sh, you would need to add it to your PATH. (You may not have access to modify the system path, but you can almost certainly modify the PATH of your own current environment.) This also assumes that your script starts with something like #!/bin/sh.

使用chmod +x确保它是可执行的。默认情况下,当前目录不在您的路径上,因此需要以./脚本的形式执行它。sh -或通过限定路径引用它。或者,如果您确实需要脚本。sh,你需要把它添加到你的路径中。(您可能无法修改系统路径,但您几乎可以肯定地修改自己当前环境的路径。)这也假设您的脚本以#!/bin/sh开头。

You could also still use an alias, which is not really related to shell scripting but just the shell, and is simple as:

您还可以使用一个别名,这个别名实际上与shell脚本无关,而仅仅与shell有关,它简单如下:

alias script.sh='sh script.sh'

Which would allow you to use just simply script.sh (literally - this won't work for any other *.sh file) instead of sh script.sh.

这将允许您使用简单的脚本。sh(字面上的意思——这对其他任何东西都不起作用)。而不是sh script.sh。

#4


8  

You have to enable the executable bit for the program.

您必须为程序启用可执行位。

chmod +x script.sh

Then you can use ./script.sh

然后可以使用。/script.sh

You can add the folder to the PATH in your .bashrc file (located in your home directory). Add this line to the end of the file:

可以将文件夹添加到.bashrc文件(位于主目录)中的路径。将这一行添加到文件的末尾:

export PATH=$PATH:/your/folder/here

#5


7  

In this example the file will be called myShell

在本例中,该文件将被称为myShell

First of all we will need to make this file we can just start off by typing the following:

首先,我们需要制作这个文件,我们可以从输入以下内容开始:

sudo nano myShell

sudo nano myShell

Notice we didn't put the .sh extension? That's because when we run it from the terminal we will only need to type myShell in order to run our command!

注意到我们没有使用。sh扩展吗?这是因为当我们从终端运行它时,我们只需要输入myShell来运行我们的命令!

Now, in nano the top line MUST be #!/bin/bash then you may leave a new line before continuing.

现在,在nano中,最上面一行必须是#!然后您可以在继续之前留下一行新的内容。

For demonstration I will add a basic Hello World! response

为了演示,我将添加一个基本的Hello World!响应

So, I type the following:

因此,我输入以下内容:

echo Hello World!

回声Hello World !

After that my example should look like this:

之后我的例子应该是这样的:

#!/bin/bash echo Hello World!

# !/bin/bash回声Hello World !

Now save the file and then run this command:

现在保存文件,然后运行以下命令:

sudo chmod +x myShell

sudo chmod + x myShell

Now we have made the file executable we can move it to /usr/bin/ by using the following command:

现在我们已经使文件可执行,我们可以使用以下命令将它移动到/usr/bin/:

sudo cp myShell /usr/bin/

sudo cp myShell /usr/bin/

Just to make sure that the machine can execute it properly we will need to reboot the machine

为了确保机器能够正确执行,我们需要重新启动机器

I used sudo shutdown -r now

我现在用sudo关机-r

Congrats! Our command is now done! In the terminal we can type myShell and it should say Hello World!

恭喜!我们的命令现在完成了!在终端我们可以输入myShell,它应该会说Hello World!

Happy coding!

编码快乐!

#6


4  

You can type sudo install (name of script) /usr/local/bin/(what you want to type to execute said script)

您可以输入sudo安装(脚本的名称)/usr/local/bin/(您想要键入执行的脚本)

ex: sudo install quickcommit.sh /usr/local/bin/quickcommit enter password

例:sudo quickcommit安装。sh /usr/local/bin/quickcommit输入密码

now can run without .sh and in any directory

现在可以在没有.sh和任何目录下运行

#7


1  

Add . (current directory) to your PATH variable.
You can do this by editing your .profile file.
put following line in your .profile file
PATH=$PATH:.

Just make sure to add Shebang (#!/bin/bash) line at the starting of your script and make the script executable(using chmod +x <File Name>).

添加。(当前目录)到路径变量。您可以通过编辑.profile文件来实现这一点。在.profile文件路径=$PATH中放置以下行。确保在脚本开始时添加Shebang (#!/bin/bash)行,并使脚本可执行(使用chmod +x <文件名> )。

#8


0  

Here is my backup script that will give you the idea and the automation:

以下是我的备份脚本,它将给你一个概念和自动化:

Server: Ubuntu 16.04 PHP: 7.0 Apache2, Mysql etc...

服务器:Ubuntu 16.04 PHP 7.0 Apache2、Mysql等

# Make Shell Backup Script - Bash Backup Script
    nano /home/user/bash/backupscript.sh
        #!/bin/bash
        # Backup All Start
        mkdir /home/user/backup/$(date +"%Y-%m-%d")
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/etc_rest.zip /etc -x "*apache2*" -x "*php*" -x "*mysql*"
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/etc_apache2.zip /etc/apache2
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/etc_php.zip /etc/php
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/etc_mysql.zip /etc/mysql
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/var_www_rest.zip /var/www -x "*html*"
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/var_www_html.zip /var/www/html
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/home_user.zip /home/user -x "*backup*"
        # Backup All End
        echo "Backup Completed Successfully!"
        echo "Location: /home/user/backup/$(date +"%Y-%m-%d")"

    chmod +x /home/user/bash/backupscript.sh
    sudo ln -s /home/user/bash/backupscript.sh /usr/bin/backupscript

change /home/user to your user directory and type: backupscript anywhere on terminal to run the script! (assuming that /usr/bin is in your path)

将/home/user更改为您的用户目录,并键入:backupscript在终端的任何位置运行脚本!(假设/usr/bin在您的路径中)

#1


232  

Add a "shebang" at the top of your file:

在你的文件顶部添加一个“shebang”:

#!/bin/bash

And make your file executable (chmod +x script.sh).

并使您的文件可执行(chmod +x script.sh)。

Finally, modify your path to add the directory where your script is located:

最后,修改路径,添加脚本所在的目录:

export PATH=$PATH:/appropriate/directory

(typically, you want $HOME/bin for storing your own scripts)

(通常,您需要$HOME/bin来存储自己的脚本)

#2


43  

These are some of the pre-requisites of using directly the script name:

这些是直接使用脚本名称的一些先决条件:

  1. Add the sha-bang {#!/bin/bash) line at the very top.
  2. 在最顶端添加sha-bang {#!/bin/bash)行。
  3. Using chmod u+x scriptname make the script executable.
  4. 使用chmod u+x scriptname使脚本可执行。
  5. Place the script under /usr/local/bin folder.
  6. 将脚本放在/usr/local/bin文件夹下。
  7. Run the script using just the name of the script.
  8. 只使用脚本的名称运行脚本。

Note: The reason I suggested to place it under /usr/local/bin folder is because most likely that will be path already added to your PATH variable.

注意:我建议将它放在/usr/local/bin文件夹下的原因是,最有可能的是已经添加到path变量的路径。

Update:

If you don't have access to the /usr/local/bin folder then do the following:

如果您没有访问/usr/local/bin文件夹的权限,请执行以下操作:

  1. Create a folder in your home directory and let's call it myscripts.
  2. 在主目录中创建一个文件夹,我们将其命名为myscripts。
  3. Do ls -lart on your home directory, to identify the start-up script your shell is using. It should either be .profile or .bashrc.
  4. 在主目录上执行ls -lart,以标识shell正在使用的启动脚本。它应该是.profile或者.bashrc。
  5. Once you have identified the start up script, add the following line in your script - export set PATH=$PATH:~/myscript.
  6. 确定了启动脚本之后,在脚本中添加以下行——导出设置路径=$PATH:~/myscript。
  7. Once added, source your start-up script or log out and log back in.
  8. 一旦添加,为您的启动脚本提供源代码或注销并重新登录。
  9. Execute your script using script name.
  10. 使用脚本名称执行脚本。

#3


12  

Just make sure it is executable, using chmod +x. By default, the current directory is not on your PATH, so you will need to execute it as ./script.sh - or otherwise reference it by a qualified path. Alternatively, if you truly need just script.sh, you would need to add it to your PATH. (You may not have access to modify the system path, but you can almost certainly modify the PATH of your own current environment.) This also assumes that your script starts with something like #!/bin/sh.

使用chmod +x确保它是可执行的。默认情况下,当前目录不在您的路径上,因此需要以./脚本的形式执行它。sh -或通过限定路径引用它。或者,如果您确实需要脚本。sh,你需要把它添加到你的路径中。(您可能无法修改系统路径,但您几乎可以肯定地修改自己当前环境的路径。)这也假设您的脚本以#!/bin/sh开头。

You could also still use an alias, which is not really related to shell scripting but just the shell, and is simple as:

您还可以使用一个别名,这个别名实际上与shell脚本无关,而仅仅与shell有关,它简单如下:

alias script.sh='sh script.sh'

Which would allow you to use just simply script.sh (literally - this won't work for any other *.sh file) instead of sh script.sh.

这将允许您使用简单的脚本。sh(字面上的意思——这对其他任何东西都不起作用)。而不是sh script.sh。

#4


8  

You have to enable the executable bit for the program.

您必须为程序启用可执行位。

chmod +x script.sh

Then you can use ./script.sh

然后可以使用。/script.sh

You can add the folder to the PATH in your .bashrc file (located in your home directory). Add this line to the end of the file:

可以将文件夹添加到.bashrc文件(位于主目录)中的路径。将这一行添加到文件的末尾:

export PATH=$PATH:/your/folder/here

#5


7  

In this example the file will be called myShell

在本例中,该文件将被称为myShell

First of all we will need to make this file we can just start off by typing the following:

首先,我们需要制作这个文件,我们可以从输入以下内容开始:

sudo nano myShell

sudo nano myShell

Notice we didn't put the .sh extension? That's because when we run it from the terminal we will only need to type myShell in order to run our command!

注意到我们没有使用。sh扩展吗?这是因为当我们从终端运行它时,我们只需要输入myShell来运行我们的命令!

Now, in nano the top line MUST be #!/bin/bash then you may leave a new line before continuing.

现在,在nano中,最上面一行必须是#!然后您可以在继续之前留下一行新的内容。

For demonstration I will add a basic Hello World! response

为了演示,我将添加一个基本的Hello World!响应

So, I type the following:

因此,我输入以下内容:

echo Hello World!

回声Hello World !

After that my example should look like this:

之后我的例子应该是这样的:

#!/bin/bash echo Hello World!

# !/bin/bash回声Hello World !

Now save the file and then run this command:

现在保存文件,然后运行以下命令:

sudo chmod +x myShell

sudo chmod + x myShell

Now we have made the file executable we can move it to /usr/bin/ by using the following command:

现在我们已经使文件可执行,我们可以使用以下命令将它移动到/usr/bin/:

sudo cp myShell /usr/bin/

sudo cp myShell /usr/bin/

Just to make sure that the machine can execute it properly we will need to reboot the machine

为了确保机器能够正确执行,我们需要重新启动机器

I used sudo shutdown -r now

我现在用sudo关机-r

Congrats! Our command is now done! In the terminal we can type myShell and it should say Hello World!

恭喜!我们的命令现在完成了!在终端我们可以输入myShell,它应该会说Hello World!

Happy coding!

编码快乐!

#6


4  

You can type sudo install (name of script) /usr/local/bin/(what you want to type to execute said script)

您可以输入sudo安装(脚本的名称)/usr/local/bin/(您想要键入执行的脚本)

ex: sudo install quickcommit.sh /usr/local/bin/quickcommit enter password

例:sudo quickcommit安装。sh /usr/local/bin/quickcommit输入密码

now can run without .sh and in any directory

现在可以在没有.sh和任何目录下运行

#7


1  

Add . (current directory) to your PATH variable.
You can do this by editing your .profile file.
put following line in your .profile file
PATH=$PATH:.

Just make sure to add Shebang (#!/bin/bash) line at the starting of your script and make the script executable(using chmod +x <File Name>).

添加。(当前目录)到路径变量。您可以通过编辑.profile文件来实现这一点。在.profile文件路径=$PATH中放置以下行。确保在脚本开始时添加Shebang (#!/bin/bash)行,并使脚本可执行(使用chmod +x <文件名> )。

#8


0  

Here is my backup script that will give you the idea and the automation:

以下是我的备份脚本,它将给你一个概念和自动化:

Server: Ubuntu 16.04 PHP: 7.0 Apache2, Mysql etc...

服务器:Ubuntu 16.04 PHP 7.0 Apache2、Mysql等

# Make Shell Backup Script - Bash Backup Script
    nano /home/user/bash/backupscript.sh
        #!/bin/bash
        # Backup All Start
        mkdir /home/user/backup/$(date +"%Y-%m-%d")
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/etc_rest.zip /etc -x "*apache2*" -x "*php*" -x "*mysql*"
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/etc_apache2.zip /etc/apache2
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/etc_php.zip /etc/php
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/etc_mysql.zip /etc/mysql
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/var_www_rest.zip /var/www -x "*html*"
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/var_www_html.zip /var/www/html
        sudo zip -ry /home/user/backup/$(date +"%Y-%m-%d")/home_user.zip /home/user -x "*backup*"
        # Backup All End
        echo "Backup Completed Successfully!"
        echo "Location: /home/user/backup/$(date +"%Y-%m-%d")"

    chmod +x /home/user/bash/backupscript.sh
    sudo ln -s /home/user/bash/backupscript.sh /usr/bin/backupscript

change /home/user to your user directory and type: backupscript anywhere on terminal to run the script! (assuming that /usr/bin is in your path)

将/home/user更改为您的用户目录,并键入:backupscript在终端的任何位置运行脚本!(假设/usr/bin在您的路径中)