Unix 简明教程

Shell Scripting Tutorial

Shell 脚本是一种计算机程序,由 Unix/Linux shell 运行,它可能是以下项之一:

A shell script is a computer program designed to be run by the Unix/Linux shell which could be one of the following:

  1. The Bourne Shell

  2. The C Shell

  3. The Korn Shell

  4. The GNU Bourne-Again Shell

Shell 是一个命令行解释器,shell 脚本执行的典型操作包括文件处理、程序执行和打印文本。

A shell is a command-line interpreter and typical operations performed by shell scripts include file manipulation, program execution, and printing text.

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
$

本教程的后继部分将详细介绍 Unix/Linux Shell 脚本。

Subsequent part of this tutorial will cover Unix/Linux Shell Scripting in detail.