如何在Linux中运行Bash脚本?

Bash is the heart of Linux. Most of the time bash commands are used to create a script and execute regularly. But the question is how can we run bash scripts in Linux in different ways? We will look different ways to make the bash script executable and run in Linux

Bash是Linux的心脏。 大多数情况下,bash命令用于创建脚本并定期执行。 但是问题是,如何在Linux中以不同方式运行bash脚本? 我们将寻找使bash脚本可执行并在Linux中运行的不同方法

脚本文件(Script File)

Script file holds required commands those will run in bash. Script file generally has .sh extension and file content starts with #!/bin/bash but keeps in mind if the shell is not bashed the interpreter will change. Here is a simple example shell file.

脚本文件包含将在bash中运行的必需命令。 脚本文件通常具有.sh扩展名,并且文件内容以#!/bin/bash开头,但请记住,如果未破坏shell,解释器将更改。 这是一个简单的示例shell文件。

#!/bin/bash 
 
echo "Hello Poftut"                                                                                                  
echo "Do some backup"

使Sh文件可执行 (Make Sh File Executable)

We can run shell script files directly like a command if we make them executable. Executable is permission to run files as a command. The current user should have execution permission for the file.

如果我们使外壳脚本文件可执行,则可以像命令一样直接运行它们。 可执行文件是作为命令运行文件的权限。 当前用户应具有该文件的执行权限。

$ chmod u+x myshell.sh
Make Sh File Executable
Make Sh File Executable
使Sh文件可执行

Actually the extension of the file is not important but using general conventions will make administrators’ jobs easy. Following extensions can be used too;

实际上,文件的扩展名并不重要,但是使用常规约定将使管理员的工作变得容易。 也可以使用以下扩展名;

  • SH

    SH
  • KSH

    昆士兰州

将脚本添加到PATH(Add Script To The PATH)

We can call a script file without giving the full path and just call like a Linux command. Linux bash has a $PATH variable to locate commands. We can add our script file path to the $PATH variable and call without full path.

我们可以在不提供完整路径的情况下调用脚本文件,而只是像Linux命令一样调用。 Linux bash具有$PATH变量来定位命令。 我们可以将脚本文件路径添加到$ PATH变量中,然后不使用完整路径进行调用。

$ PATH=$PATH:/home/ismail
Add Script To The PATH
Add Script To The PATH
将脚本添加到PATH

使用Bash命令运行脚本 (Run Script With Bash Command)

We can run script files without making them executable or change anything. bash command can be used to interpret the script file and run scripts inside it.

我们可以运行脚本文件而无需使其成为可执行文件或进行任何更改。 bash命令可用于解释脚本文件并在其中运行脚本。

$ bash myshell.sh
Run Script With Bash Command
Run Script With Bash Command
使用Bash命令运行脚本

翻译自: https://www.poftut.com/run-bash-script-linux/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值