##这是我的第二篇文章

bash 脚本的优点

Bash 脚本是一种强大且灵活的工具,可以用于自动化系统管理任务、管理系统资源以及在 Unix/Linux 系统中执行其他例行任务。Shell 脚本的一些优点包括:

  • 自动化:Shell 脚本允许你自动化重复性任务和过程,节省时间并减少手动执行时可能出现的错误。
  • 可移植性:Shell 脚本可以在各种平台和操作系统上运行,包括 Unix、Linux、macOS,甚至通过使用模拟器或虚拟机在 Windows 上运行。
  • 灵活性:Shell 脚本高度可定制,可以轻松修改以满足特定需求。它们还可以与其他编程语言或实用程序结合,创建更强大的脚本。
  • 易访问性:Shell 脚本易于编写,不需要任何特殊工具或软件。它们可以使用任何文本编辑器进行编辑,并且大多数操作系统都有内置的 shell 解释器。
  • 集成:Shell 脚本可以与其他工具和应用程序集成,如数据库、Web 服务器和云服务,从而实现更复杂的自动化和系统管理任务。
  • 调试:Shell 脚本易于调试,大多数 shell 都内置调试和错误报告工具,可以帮助快速识别和修复问题。

运行 shell 脚本的方法有两种:

1, 作为可执行程序运行

2,作为解释器参数运行

1
2
#!/bin/bash
echo "hello World"

当作为可执行程序运行时

1
2
3
4
chmod +x test.sh
#赋予可执行权限
./test.sh
#执行程序

当作为解释器运行时

1
2
3
4
/bin/sh test.sh 
#执行命令
/bin/php test.php
#执行命令

创建 bash 脚本

使用 vi 命令创建一个名为 run_all.sh 的文件

1
vi run_all.sh

在文件中添加以下命令并保存

1
2
3
4
5
6
7
8
#!/bin/bash
echo "今天是 " `date`

echo -e "\n请输入目录路径"
read the_path

echo -e "\n 你的路径包含以下文件和文件夹:"
ls $the_path

打印用户提供的目录内容的脚本

  • 第 1 行:Shebang (<font style="color:rgb(10, 10, 35);">#!/bin/bash</font>) 指向 bash shell 路径。
  • 第 2 行:<font style="color:rgb(10, 10, 35);">echo</font> 命令在终端显示当前日期和时间。注意 <font style="color:rgb(10, 10, 35);">date</font> 在反引号中。
  • 第 4 行:我们希望用户输入一个有效的路径。
  • 第 5 行:<font style="color:rgb(10, 10, 35);">read</font> 命令读取输入并将其存储在变量 <font style="color:rgb(10, 10, 35);">the_path</font> 中。
  • 第 8 行:<font style="color:rgb(10, 10, 35);">ls</font> 命令使用存储路径的变量并显示当前的文件和文件夹。

执行 bash 脚本

1
chmod u+x run_all.sh
  • <font style="color:rgb(10, 10, 35);">chmod</font> 修改文件的所有权以供当前用户使用:<font style="color:rgb(10, 10, 35);">u</font>
  • <font style="color:rgb(10, 10, 35);">+x</font> 将执行权限添加到当前用户。这意味着作为所有者的用户现在可以运行该脚本。
  • <font style="color:rgb(10, 10, 35);">run_all.sh</font> 是我们希望运行的文件。
1
2
3
sh run_all.sh
bash run_all.sh
./run_all.sh

变量命令规范

在 Bash 脚本中,以下是变量命名规范:

  1. 变量名称应以字母或下划线 (<font style="color:rgb(10, 10, 35);">_</font>) 开头。
  2. 变量名称可以包含字母、数字和下划线 (<font style="color:rgb(10, 10, 35);">_</font>)。
  3. 变量名称区分大小写。
  4. 变量名称不应包含空格或特殊字符。
  5. 使用能反映变量用途的描述性名称。
  6. 避免使用保留关键字(如 <font style="color:rgb(10, 10, 35);">if</font>, <font style="color:rgb(10, 10, 35);">then</font>, <font style="color:rgb(10, 10, 35);">else</font>, <font style="color:rgb(10, 10, 35);">fi</font> 等)作为变量名称

收集输入

1,读取用户输入并将其存储在变量中,可以使用 read 命令读取用户输入

1
2
3
4
5
6
7
#!/bin/bash 

echo "What's your name?"

read entered_name

echo -e "\nWelcome to bash tutorial" $entered_name

2,从文件读取

此代码从名为 <font style="color:rgb(10, 10, 35);">input.txt</font> 的文件中读取每一行并将其打印到终端。我们将在本文稍后学习 while 循环。

1
2
3
4
while read line
do
echo $line
done < input.txt

3,命令行参数

在 bash 脚本或函数中,<font style="color:rgb(10, 10, 35);">$1</font> 表示传递的初始参数,<font style="color:rgb(10, 10, 35);">$2</font> 表示传递的第二个参数,以此类推。

此脚本将名字作为命令行参数并打印个性化问候语。

1
echo "Hello, $1!"

我们将 <font style="color:rgb(10, 10, 35);">Zaira</font> 作为参数提供给脚本。

1
2
#!/bin/bash
echo "Hello, $1!"

脚本代码:greeting.sh

输出

显示输出

1
2
3
4
5
6
7
8
#打印到终端
echo "Hello, World!"
#写入文件
echo "This is some text." > output.txt
#追加到文件
echo "More text." >> output.txt
#重定向输出
ls > files.txt

基本 bash 命令

  1. <font style="color:rgb(10, 10, 35);">cd</font>: 切换到不同目录。
  2. <font style="color:rgb(10, 10, 35);">ls</font>: 列出当前目录的内容。
  3. <font style="color:rgb(10, 10, 35);">mkdir</font>: 创建新目录。
  4. <font style="color:rgb(10, 10, 35);">touch</font>: 创建新文件。
  5. <font style="color:rgb(10, 10, 35);">rm</font>: 删除文件或目录。
  6. <font style="color:rgb(10, 10, 35);">cp</font>: 复制文件或目录。
  7. <font style="color:rgb(10, 10, 35);">mv</font>: 移动或重命名文件或目录。
  8. <font style="color:rgb(10, 10, 35);">echo</font>: 将文本打印到终端。
  9. <font style="color:rgb(10, 10, 35);">cat</font>: 连接并打印文件内容。
  10. <font style="color:rgb(10, 10, 35);">grep</font>: 在文件中搜索模式。
  11. <font style="color:rgb(10, 10, 35);">chmod</font>: 更改文件或目录的权限。
  12. <font style="color:rgb(10, 10, 35);">sudo</font>: 以管理权限运行命令。
  13. <font style="color:rgb(10, 10, 35);">df</font>: 显示可用磁盘空间。
  14. <font style="color:rgb(10, 10, 35);">history</font>: 显示先前执行的命令列表。
  15. <font style="color:rgb(10, 10, 35);">ps</font>: 显示有关正在运行的进程的信息。

脚本安全和 set

set 命令:可以用来定制 shell 环境

h:hashall,打开选项后,Shell 会将命令所在的路径hash下来,避免每

次都要查询。通过set +h将h选项关闭

i:interactive-comments,包含这个选项说明当前的 shell 是一个交互式的 shell。所谓的交互式shell, 在脚本中,i选项是关闭的

m:monitor,打开监控模式,就可以通过Job control来控制进程的停

止、继续,后台或者前台执行等

B:braceexpand,大括号扩展

H:history,H选项打开,可以展开历史列表中的命令,可以通过!感叹号来完成,例如“!!”返回上最近的一个历史命令,“!n”返回第 n 个历史命令

set 命令实现脚本安全

-u 在扩展一个没有设置的变量时,显示错误信息, 等同set -o

nounset

-e 如果一个命令返回一个非0退出状态值(失败)就退出, 等同set -o

errexit

-o option 显示,打开或者关闭选项

显示选项:set -o

打开选项:set -o 选项 关闭选项:set +o 选项

-x 当执行命令时,打印命令及其参数,类似 bash -x

bc 命令

bash 计算器实际上是一种编程语言,允许在命令行中输入浮点数表达式,然后解释并计算该 表达式,最后返回结果。bash 计算器能够识别以下内容。

数字(整数和浮点数)

变量(简单变量和数组)

注释(以#或 C 语言中的/* */开始的行)

表达式

编程语句(比如 if-then 语句)

函数

1
2
3
4
5
6
7
8
9
10
-h --help
print this usage and exit
-i --interactive force interactive mode
-l --mathlib use the predefined math routines
-q --quiet
don't print initial banner
-s --standard non-standard bc constructs are errors
-w --warn
warn about non-standard bc constructs
-v --version print version information and exit

浮点数运算是由内建变量 scale 控制的。必须将该变量的值设置为希望在计算结果中保留的小数位数,否则将无法得到期望的结果

scale 变量的默认值是 0。在 scale 值被设置前,bash 计算器的计算结果不包含小数位。 在将其设置成 4 后,bash 计算器显示的结果包含4 位小数。-q 选项可以不显示 bash 计算器冗长 的欢迎信息。

read 命令接受输入

尽管命令行选项和参数是从脚本用户处获取输入的一种重要方式,但有时候脚本还需要更多的交互性。你可能想要在脚本运行时询问用户并等待用户回答。为此,bash shell 提供了read 命令。

使用 read 能把输入值分配给一个或多个 shell 变量,read 从标准输入中读取值,给每个单词分配一个变量,所有剩余单词都被分配给最后一个变量,如果变量名没有指定,默认标准输入的值赋值给系统内置变量REPLY

1
2
3
4
5
-p 指定要显示的提示
-s 静默输入,一般用于密码
-n N 指定输入的字符长度N
-d '字符' 输入结束符
-t N TIMEOUT为N秒

read 命令从标准输入(键盘)或另一个文件描述符中接受输入。获取输入后,read 命令会将数据存入变量。