Ubuntu 简明教程
Ubuntu - Scripting
由于我们有能力处理之前章节中介绍过的命令行,因此创建可执行简单作业的脚本很常见。 Scripting 通常用于自动化管理任务。让我们按照以下步骤创建一个简单的脚本。该脚本将用于显示分配给机器的 IP 地址。
Since we have the ability to work with the command line which we covered in the previous chapter, it is common to create scripts which can perform simple jobs. Scripting is normally used to automate administrative tasks. Let’s create a simple script using the following steps. The script will be used to display the IP address assigned to the machine.
Step 1 − 打开编辑器。就像 Windows 中的记事本一样,Ubuntu 有一个文本编辑器。在搜索对话框中输入编辑器的关键字。然后双击 Text Editor 选项。
Step 1 − Open the editor. Just like notepad in Windows, Ubuntu has a text editor. In the search dialog box, enter the keyword of editor. Then double-click on the Text Editor option.

将弹出以下编辑器屏幕。
The following editor screen pops up.

Step 2 − 在编辑器中输入以下文本。
Step 2 − Enter the following text in the editor.
originalAddress=@(ifconfig | grep “inet addr” | head –n 1 | cut –d “:” –f 2 | cut –d “ “ –f 1)
echo $originalAddress
Step 3 − 将文件另存为 write-ip.sh。
Step 3 − Save the file as write-ip.sh.

现在,一旦你保存了文件,我们需要为该文件分配一些执行权限。否则,我们将无法执行该文件。
Now once you have saved the file, we need to assign the file some execute rights. Otherwise, we will not be able to execute the file.
Step 4 − 转到命令提示符,导航到 Desktop 位置并发出以下命令。
Step 4 − Go to the command prompt, navigate to the Desktop location and issue the following command.
chmod a+x write-ip.sh
上述命令将提供对该文件的执行权限。
The above command will provide execute permissions to the file.

Step 5 − 现在,我们可以通过发出以下命令来执行该文件。
Step 5 − Now, we can execute the file by issuing the following command.
./write-ip.sh
输出将是分配给机器的 IP 地址,如下面的屏幕截图所示。
The output will be the IP address assigned to the machine as shown in the following screenshot.
