20.2 shell脚本结构和执行
20.3 date命令用法
20.4 shell脚本中的变量
shell脚本介绍
shell是一种脚本语言
可以使用逻辑判断、循环等语法
可以自定义函数
shell是系统命令的集合
shell脚本可以实现自动化运维,能打打增加我们的运维效率
shell脚本结构和执行
开头需要加#!/bin/bash (解释器)
以#开头的行作为解释
脚本的名字以.sh结尾,用于区分这个是一个shell脚本
执行方法:2种
chmod +x XX.sh
./XX.sh
bash 1.sh
查看脚本的执行过程 sh -x 1.sh ---sh换成bash也可以
查看脚本是否语法错误 sh -n 1.sh ---sh换成bash也可以
date命令用法
date --查看时间
cal --查看日历
date -d “2017-09-08 00:00:00” ---更改时间戳
date +%s -d “2017-09-08 00:00:00 ---更改时间戳
date +%F-%T
date -d “+1 day” --1天后
date -d “-1 day” --1天前
date -d “+1 mouth” ---1小时后
date -d “-1 mouth” ----1小时前
date -d “+1 year” ---1年后
date -d "-1 year" --1年前
%W 星期
shell脚本中的变量
变量:当脚本中石油某个字符串笔记频繁且字符串长度很长时就应该使用变量代替
使用条件语句长使用变量
a=1
if[$a<2;then...;
引用某个命令结果时,用变量替代: n=wc -l 1.txt`
内置变量:
$0,$1,$2 $0:表示脚本本身,$1第一个参数,$2第二个参数 $#表示参数个数
数学运算变量
a=1 b=2 c=$(($a+$b))或者c=$[$a+$b]