shell脚本中执行命令_如何在Shell脚本中执行命令?

shell脚本中执行命令

Shell is a command-line interpreter that allows the user to interact with the system. It is responsible for taking inputs from the user and displaying the output.

Shell是一个命令行解释器,允许用户与系统进行交互。 它负责从用户那里获取输入并显示输出。

Shell scripts are a series of commands written in order of execution. These scripts can contain functions, loops, commands, variables. Scripts make it easy for the users to save certain sequences of codes that might be used again and again. Shell scripts can also have comments to increase readability.

Shell脚本是按执行顺序编写的一系列命令。 这些脚本可以包含函数, 循环 ,命令, 变量 。 脚本使用户可以轻松地保存某些可能反复使用的代码序列。 Shell脚本也可以带有注释以提高可读性。

A shell script needs to be saved with the extension .sh.

需要使用扩展名.sh保存shell脚本。

To let the Linux system know that the file is a shell script, the file needs to begin with the shebang construct.

为了使Linux系统知道该文件是Shell脚本,该文件必须以shebang构造开头。


#!/bin/bash 
OR
#!/bin/sh

After this, the script can contain commands, functions, loops, conditional checks, etc.

此后,脚本可以包含命令,函数,循环,条件检查等。

A good script always contains comments that make it readable.

一个好的脚本总是包含使其可读的注释。

创建并运行基本的Shell脚本 (Creating and running a basic shell script )

A shell script can be created using vi, cat command, or the normal text editor in GUI.

可以使用vi, cat命令或GUI中的常规文本编辑器来创建Shell脚本。

Let’s create a basic shell script using vi

让我们使用vi创建一个基本的shell脚本


$ vi basic_script.sh

This will take you to the vi editor. Add the following lines:

这将带您到vi编辑器。 添加以下行:


#!/bin/bash
whoami
date

This simple script should display the current user followed by the date.

这个简单的脚本应显示当前用户,然后显示日期。

To save and exit the vi editor:

要保存并退出vi编辑器:

  • Press ESC

    按ESC
  • Type :

    类型:
  • Type in ‘wq’

    输入“ wq”
  • Hit Enter

    按下Enter

By default, the creator of the script does not get executable permission for the file.

默认情况下,脚本的创建者不会获得该文件的可执行权限。

To change that:

要更改此设置:


$ chmod +x basic_script.sh

This will give you (current user) the permission to execute the file.

这将向您(当前用户)授予执行文件的权限。

To run the script :

运行脚本:


$ bash basic_script.sh
Basic Script 1

The first line of output corresponds to ‘whoami’ command and the second line to ‘date’ command.

输出的第一行对应于“ whoami”命令,第二行对应于“ date”命令。

Another way of running the script is :

运行脚本的另一种方法是:


$ ./basic_script.sh 

Running the file this way might require the user to give permission first. Running it with ‘bash’ doesn’t require the permission.

以这种方式运行文件可能需要用户首先授予权限。 使用“ bash”运行它不需要许可。

Bash Permision

The same script is running with ‘bash’ before it, but having permission issues when tried to execute directly. The reason this is happening is that the command bash [filename] only needs read permission from the file.

相同的脚本之前带有“ bash”运行,但是尝试直接执行时存在权限问题。 发生这种情况的原因是命令bash [filename]仅需要从文件读取权限。

Whereas the command ./[filename] run the file as an executable and hence requires the permission to execute. This question has been answered in detail on StackExchange.

而命令./[filename]将文件作为可执行文件运行,因此需要执行权限。 该问题已在StackExchange上得到了详细解答。

Bash

In general it is better to provide executable permission.

通常,最好提供可执行权限。

在Shell脚本中使用变量 (Using variables in shell scripts )

Scripts can include user-defined variables, in fact as scripts get voluminous in size it is essential to have variables that are clearly defined. Variables that are self-descriptive in nature is another quality of a good script.

脚本可以包含用户定义的变量,实际上,随着脚本的大小变得越来越庞大,必须明确定义变量。 本质上具有自描述性的变量是好的脚本的另一种品质。

Add the following lines to the script :

将以下行添加到脚本中:


#!/bin/bash
#This is a comment

#defining a variable
GREETINGS="Hello! How are you"
echo $GREETINGS
Shell Script In Vi

GREETINGS is the variable defined and later accessed using ‘$’.

GREETINGS是定义的变量,以后使用'$'访问。

There should be no space in the line where variables being assigned a value.

行中没有为变量分配值的空间。

Let’s run the script:

让我们运行脚本:

Using Variable Output

从命令行读取输入 (Reading input from the command line)

Shell scripts can be made interactive with the ability to accept input from the command line. Read command can be used to store the command line input in a variable.

可以使Shell脚本具有交互性,并能够接受来自命令行的输入。 读取命令可用于将命令行输入存储在变量中。


#!/bin/bash
#This is a comment

#defining a variable
echo "What is your name?"
#reading input 
read NAME
#defining a variable
GREETINGS="Hello! How are you"
echo $NAME $GREETINGS
                 

A variable NAME has been used to accept input from the command line.

变量NAME已用于接受命令行输入。

On running the script :

在运行脚本时:

Reading Output

定义功能 (Defining functions )

Users can define their own functions in a script. These functions can take multiple arguments.

用户可以在脚本中定义自己的功能。 这些函数可以采用多个参数。

In the script add :

在脚本中添加:


#!/bin/bash
#This is a comment

#defining a variable
echo "What is the name of the directory you want to create?"
#reading input 
read NAME
#defining a variable

echo "Creating $NAME ..."
mkcd ()
{
  mkdir "$NAME" 
  cd "$NAME"
}

mkcd
echo "You are now in $NAME"

This script will ask the user for a directory name. It will then create the directory and cd into it.

该脚本将询问用户目录名称。 然后它将创建目录并cd进入该目录。

Funciton In A Script 1

结论 (Conclusion)

We saw how scripts can be used to run commands in a serial order. Scripts help users in reducing redundancy and save time. Scripts can also be shared among different users.

我们看到了如何使用脚本以串行顺序运行命令。 脚本可帮助用户减少冗余并节省时间。 脚本也可以在不同用户之间共享。

The Scripts we saw in this tutorial were quite basic, scripts can be designed to perform complex tasks as well. To learn more about scripting refer this.

我们在本教程中看到的脚本非常基础,脚本也可以设计为执行复杂的任务。 要了解有关脚本的更多信息,请参考

翻译自: https://www.journaldev.com/41511/execute-command-shell-script

shell脚本中执行命令

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值