服务器管理、网络配置和备份的Shell脚本示例
1. 解析日期和时间数据
在处理基于时间的数据时,尤其是处理大量此类数据时,可能会让人感到头疼。不过,在很多情况下,我们会使用日期和时间信息进行索引,比如为备份文件命名。因此,学会从 date
命令中获取信息并将其存入变量,让代码尽可能模块化非常重要。
1.1 准备工作
不需要安装任何特殊工具,只需确保Linux机器正常运行即可。
1.2 操作步骤
使用 date
命令提取所需的日期和时间信息,具体步骤如下:
1. 在文本编辑器中输入以下脚本:
#!/bin/bash
# V1.0 / Jasmin Redzepagic / 01/11/2021 Initial script version
# Distribution allowed under GNU Licence V2.0
# This part of our script is just plain using date command to assign
# values to "obviously named variables". This further shows two
# things - how to assign a variable value from external command,
# and how to use that principle on date and time data.
hour=$(date +%H)
minute=$(dat