Unix 简明教程
Unix / Linux - What is Shells?
Shell 为您提供了 Unix 系统的接口。它会从您处收集输入,并根据该输入执行程序。当一个程序完成执行时,它会显示该程序的输出。
A Shell provides you with an interface to the Unix system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program’s output.
Shell 是一个环境,我们可以在其中运行我们的命令、程序和 shell 脚本。Shell 有不同的类型,就像操作系统有不同的类型一样。每种类型的 shell 都有一组自己识别的命令和功能。
Shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions.
Shell Prompt
提示符 $ (称为 command prompt )是由 shell 发出的。在提示符显示时,您可以键入一个命令。
The prompt, $, which is called the command prompt, is issued by the shell. While the prompt is displayed, you can type a command.
在您按下 Enter 后,Shell 会读取您的输入。它通过查看您的输入的第一个单词来确定您想要执行的命令。一个单词是一组不间断的字符。空格和制表符用于分隔单词。
Shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.
以下是一个 date 命令的简单示例,该命令显示当前日期和时间 −
Following is a simple example of the date command, which displays the current date and time −
$date
Thu Jun 25 08:30:19 MST 2009
您可以使用环境教程中说明的环境变量 PS1 来自定义您的命令提示符。
You can customize your command prompt using the environment variable PS1 explained in the Environment tutorial.
Shell Types
在 Unix 中,主要有两种类型的 shell −
In Unix, there are two major types of shells −
-
Bourne shell − If you are using a Bourne-type shell, the $ character is the default prompt.
-
C shell − If you are using a C-type shell, the % character is the default prompt.
Bourne Shell 有以下子类别 −
The Bourne Shell has the following subcategories −
-
Bourne shell (sh)
-
Korn shell (ksh)
-
Bourne Again shell (bash)
-
POSIX shell (sh)
各种 C 型 shell 如下 −
The different C-type shells follow −
-
C shell (csh)
-
TENEX/TOPS C shell (tcsh)
最初的 Unix shell 是由斯蒂芬·R·伯恩在 20 世纪 70 年代中期编写,当时他在新泽西的 AT&T 贝尔实验室工作。
The original Unix shell was written in the mid-1970s by Stephen R. Bourne while he was at the AT&T Bell Labs in New Jersey.
Bourne Shell 是第一个出现在 Unix 系统上的 shell,因此它被称为“shell”。
Bourne shell was the first shell to appear on Unix systems, thus it is referred to as "the shell".
Bourne Shell 通常安装为 /bin/sh ,用于大多数版本的 Unix。出于此原因,对于编写可以在不同版本的 Unix 上使用的脚本,它是首选 shell。
Bourne shell is usually installed as /bin/sh on most versions of Unix. For this reason, it is the shell of choice for writing scripts that can be used on different versions of Unix.
在本章中,我们将介绍基于 Borne Shell 的大多数 Shell 概念。
In this chapter, we are going to cover most of the Shell concepts that are based on the Borne Shell.
Shell Scripts
shell 脚本的基本概念是命令列表,这些命令按执行顺序列出。一个好的 shell 脚本将有注释,在前面加 # 符号,描述步骤。
The basic concept of a shell script is a list of commands, which are listed in the order of execution. A good shell script will have comments, preceded by # sign, describing the steps.
有条件测试,例如值 A 大于值 B,循环允许我们遍历海量数据、文件来读取和存储数据,以及读取和存储数据的变量,并且该脚本可能包含函数。
There are conditional tests, such as value A is greater than value B, loops allowing us to go through massive amounts of data, files to read and store data, and variables to read and store data, and the script may include functions.
我们将在下一节中编写许多脚本。它将是一个简单的文本文件,其中我们将放置所有命令和大量其他所需结构,告诉 shell 环境做什么以及何时执行。
We are going to write many scripts in the next sections. It would be a simple text file in which we would put all our commands and several other required constructs that tell the shell environment what to do and when to do it.
Shell 脚本和函数都经过解释。这意味着它们未被编译。
Shell scripts and functions are both interpreted. This means they are not compiled.
Example Script
假设我们创建一个 test.sh 脚本。请注意,所有脚本都应具有 .sh 扩展名。在向脚本添加任何其他内容之前,您需要提醒系统正在启动 shell 脚本。使用 shebang 构造完成此操作。例如 −
Assume we create a test.sh script. Note all the scripts would have the .sh extension. Before you add anything else to your script, you need to alert the system that a shell script is being started. This is done using the shebang construct. For example −
#!/bin/sh
这告诉系统后续命令将由 Bourne Shell 执行。它被称为 Shebang,因为 # 符号称为井号,而 ! 符号称为惊叹号。
This tells the system that the commands that follow are to be executed by the Bourne shell. It’s called a shebang because the # symbol is called a hash, and the ! symbol is called a bang.
要在包含这些命令的脚本中,您首先放置 Shebang 行,然后再添加命令 −
To create a script containing these commands, you put the shebang line first and then add the commands −
#!/bin/bash
pwd
ls
Shell Comments
您可以按如下方式将注释放入脚本中 −
You can put your comments in your script as follows −
#!/bin/bash
# Author : Zara Ali
# Copyright (c) Tutorialspoint.com
# Script follows here:
pwd
ls
保存上述内容并使脚本可执行 −
Save the above content and make the script executable −
$chmod +x test.sh
Shell 脚本现在可以执行 −
The shell script is now ready to be executed −
$./test.sh
执行后,您会收到以下结果 −
Upon execution, you will receive the following result −
/home/amrood
index.htm unix-basic_utilities.htm unix-directories.htm
test.sh unix-communication.htm unix-environment.htm
Note − 要执行当前目录中可用的程序,请使用 ./program_name
Note − To execute a program available in the current directory, use ./program_name
Extended Shell Scripts
Shell 脚本有几个必需的构造,告诉 shell 环境做什么以及何时执行它。当然,大多数脚本都比上述脚本更复杂。
Shell scripts have several required constructs that tell the shell environment what to do and when to do it. Of course, most scripts are more complex than the above one.
Shell 毕竟是一种真正的编程语言,具有变量、控制结构等。无论脚本多复杂,它仍然只是一个按顺序执行的命令列表。
The shell is, after all, a real programming language, complete with variables, control structures, and so forth. No matter how complicated a script gets, it is still just a list of commands executed sequentially.
以下脚本使用 read 命令,该命令从键盘获取输入并将其分配为 PERSON 变量的值,最后将其打印到 STDOUT。
The following script uses the read command which takes the input from the keyboard and assigns it as the value of the variable PERSON and finally prints it on STDOUT.
#!/bin/sh
# Author : Zara Ali
# Copyright (c) Tutorialspoint.com
# Script follows here:
echo "What is your name?"
read PERSON
echo "Hello, $PERSON"
以下是脚本的一个示例运行 −
Here is a sample run of the script −
$./test.sh
What is your name?
Zara Ali
Hello, Zara Ali
$