Shell-day01
Shell课程大纲
1. Shell基本概述
2. Shell的变量定义
3. Shell数值运算
4. Shell的流程控制 if语句
5. Shell的循环语句 for while循环
6. Shell的数组函数
7. Shell内置命令
8. Shell正则表达式 Awk Grep Sed
什么是Shell
Shell就是一个命令解释器。
Shell分为交互式Shell和非交互式Shell。
交互式Shell就是命令行上面一条一条命令的执行
非交互式Shell就是以脚本的方式运行。
通过变量$-来查看是否是交互式或者非交互式Shell
交互式和非交互式的区别
[root@shell01 ~]
himBH
[root@shell01 ~]
[root@shell01 ~]
echo $-
[root@shell01 ~]
hB
h
i
m
B
H
什么是Shell脚本
1. 系统命令的堆积
2. 特定的格式。特定的语法 组成的一个文件
3. 以.sh为结尾的。
为什么要学习Shell脚本
自动化的运维
减少不必要的工作
提高工作的效率
学习Shell脚本,需要什么技能
1. 要对Vim文本编辑器熟悉。 .vimrc要会运用
2. 要有Linux命令的基础应用。 最少要熟悉80个以上的常用linux命令
3. 对正则表达式要熟悉,对Awk Sed Grep 三剑客要熟练使用。
4. 熟悉常见的linux服务,服务配置,网络,优化,日志的等的操作。
如何学好Shell脚本呢
多练 多思考
要自己的思路,开发思维
变量名要规范
不要拿来主义,特别是针对新手
Shell脚本能干什么
1. 基础配置
2. 安装软件
3. 配置变更
4. 业务部署 Git jenkins Shell 进行自动化代码上线
5. 日常的备份 通过定时任务+脚本
6. 信息收集 Zabbix + Shell 自动化的监控系统的状态
7. 日志分析 ELK
8. 服务扩容/缩容 Zabbix + Shell
Shell脚本规范
1. 脚本统一放在一个目录下
[root@shell01 ~]
2. 建议使用Vim文本编辑器,有高亮显示
3. 以.sh为结尾
4. 脚本的开头要写
5.
6. 脚本带上作者相关信息
7. 成对的符号,要一次书写完成 '' ""
8. 格式的语法也要一次书写完成 if ;then fi for i in do done
Shell的执行方式
脚本从上到下 从左到到右的方式进行执行,脚本执行时遇到子脚本,会执行子脚本,子脚本执行完成后,退出子脚本,继续执行脚本中的内容。
脚本运行时,会向系统内核请求一个进程。脚本就会在该进程下进行运行,终止操作。
执行通过哪些方式进行执行:
1. bash script-name 或者 sh script-name
[root@shell01 /scripts]
[root@shell01 /scripts]
pwd
[root@shell01 /scripts]
/scripts
[root@shell01 /scripts]
/scripts
[root@shell01 /scripts]
total 4
-rw-r--r-- 1 root root 4 2020-02-19 11:10 test.sh
2. path/script-name(绝对路径) 或者 ./script-name
[root@shell01 /scripts]
total 4
-rw-r--r-- 1 root root 4 2020-02-19 11:10 test.sh
[root@shell01 /scripts]
-bash: /scripts/test.sh: Permission denied
[root@shell01 /scripts]
-bash: ./test.sh: Permission denied
[root@shell01 /scripts]
[root@shell01 /scripts]
total 4
-rwxr--r-- 1 root root 4 2020-02-19 11:10 test.sh
[root@shell01 /scripts]
/scripts
[root@shell01 /scripts]
/scripts
3. source script-name 或者 . script-name
4. cat script-name | bash
5. bash < script-name
登录式Shell和非登录式
登录式 通过用户名和密码的方式进行登录的
非登录式 不是通过用户和密码的方式进行登录的
exit logout
exit 可以退出登录式和非登录式Shell
logout 只能退出登录式Shell,不能退出非登录式Shell
脚本其实就是一个非登录式Shell
Shell变量定义
变量的概述
什么是变量?
数据的一种传递方式。 用一个固定的字符串去表示一个不固定的字符串,数值,内容,数据等。
方便后续调用
变量名的规范
建议采用驼峰式方式
Hostname_Ip
变量名由字母,数字,下划线组成,不能使用空格,短横岗,特殊符号尽量不用
见名知其义
ip
Ip
ip1
Host_Ip
host_ip
Host_ip
Shell变量定义的方式
1. 用户自定义变量
2. 系统环境变量: 系统定义好的,主要是定义系统的信息的
3. 位置参数变量 变量名是固定的,意思也是固定的,通过脚本进行传递数据
4. 预定义的变量 脚本中已经定义好的,变量名是固定的,作用也是固定的 $- $? $$ $@ $*
变量的实践
1. 用户自定义变量
[root@shell01 /scripts]
[root@shell01 /scripts]
qiudao
[root@shell01 /scripts]
-bash: shell: command not found
[root@shell01 /scripts]
[root@shell01 /scripts]
qiudao shell
[root@shell01 /scripts]
[root@shell01 /scripts]
qiudao
[root@shell01 /scripts]
[root@shell01 /scripts]
qiudaoshell
[root@shell01 /scripts]
[root@shell01 /scripts]
qiudao-shell
[root@shell01 /scripts]
qiudao/shell
[root@shell01 /scripts]
qiudao
[root@shell01 /scripts]
$Name
[root@shell01 /scripts]
qiudao
单引号:强引用,里面是什么,就输出什么,吃什么吐什么
双引号:弱引用,吃什么吐什么,会解析变量 $ $()
不加引号:在变量定义值时,出现空格,就不会看做是一个整体的值。吃什么吐什么,解析变量
反引号 `` 优先执行反引号里面的命令 跟 $() 含义一样
[root@shell01 /scripts]
qiudao money is 0000000000000
[root@shell01 /scripts]
$Name money is $10000000000000
[root@shell01 /scripts]
qiudao money is 0000000000000
[root@shell01 /scripts]
qiudao money is '0000000000000'
[root@shell01 /scripts]
qiudao money is $10000000000000
[root@shell01 /scripts]
"$Name" money is $10000000000000
注意:遇到特殊符号时,使用撬棍进行转义,取消其特殊含义。
2. 系统环境变量
[root@shell01 /scripts]
qiudao
[root@shell01 /scripts]
[root@shell01 /scripts]
echo $Name
[root@shell01 /scripts]
[root@shell01 /scripts]
[root@shell01 /scripts]
qiudao
[root@shell01 /scripts]
qiudao
[root@shell01 /scripts]
[root@shell01 scripts]
qiudao
[root@shell01 scripts]
[root@shell01 /scripts]
Name=qiudao
gitcvs.dbTableNamePrefix
[root@shell01 /scripts]
XDG_SESSION_ID=47
HOSTNAME=shell01
TERM=xterm
[root@shell01 /scripts]
qiudao
[root@shell01 /scripts]
[root@shell01 /scripts]
[root@shell01 /scripts]
#!/bin/bash
echo "当前系统登录的用户为:$USER"
echo "当前所在的目录位置为:$PWD"
echo "当前系统的主机名为:$HOSTNAME"
echo "当前系统用户的家目录为:$HOME"
echo "当前系统登录的用户的UID为:$UID"
echo "当前系统登录的终端IP为:$SSH_CONNECTION"
[root@shell01 /scripts]
当前系统登录的用户为:root
当前所在的目录位置为:/scripts
当前系统的主机名为:shell01
当前系统用户的家目录为:/root
当前系统登录的用户的UID为:0
当前系统登录的终端IP为:10.0.0.1 63017 10.0.0.7 22
3. 预定义变量及位置变量
[root@shell01 /scripts]
#!/bin/bash
echo "当前shell的脚本名为:$0"
echo "当前shell的第一个位置变量为:$1"
echo "当前shell的第二个位置变量为:$2"
echo "当前shell的第三个位置变量为:$3"
echo "当前shell的所有位置变量为:$*"
echo "当前shell的所有位置变量为:$@"
echo "当前shell的位置变量的个数为:$#"
echo "当前shell脚本的PID号为:$$"
echo "上一条命令的执行结果为:$?"
[root@shell01 /scripts]
当前shell的脚本名为:/scripts/path.sh
当前shell的第一个位置变量为:nginx
当前shell的第二个位置变量为:php
当前shell的第三个位置变量为:mysql
当前shell的所有位置变量为:nginx php mysql
当前shell的所有位置变量为:nginx php mysql
当前shell的位置变量的个数为:3
当前shell脚本的PID号为:8994
上一条命令的执行结果为:0
$* $@的区别 了解即可
$* 把参数作为一个整体字符串返回
$@ 把参数一个一个进行返回
[root@shell01 /scripts]
#!/bin/bash
test() {
echo "未加引号时对比"
echo $*
echo $@
echo "加入引号之后对比"
for i in "$*"
do
echo $i
done
echo "###################"
for j in "$@"
do
echo $j
done
}
test nginx php mysql ansible
[root@shell01 /scripts]
未加引号时对比
nginx php mysql ansible
nginx php mysql ansible
加入引号之后对比
nginx php mysql ansible
nginx
php
mysql
ansible
位置变量: $1 $2 .... $9 ${10}
将命令的执行结果赋值给变量
[root@shell01 /scripts]
[root@shell01 /scripts]
shell01
[root@shell01 /scripts]
[root@shell01 /scripts]
18
[root@shell01 /scripts]
19
[root@shell01 /scripts]
[root@shell01 /scripts]
[root@shell01 /scripts]
[root@shell01 /scripts]
[root@shell01 /scripts]
2020-02-19
[root@shell01 /scripts]
[root@shell01 /scripts]
[root@shell01 /scripts]
test01.txt test02.txt test03.txt test04.txt test05.txt test06.txt test07.txt test08.txt test09.txt test10.txt
[root@shell01 /scripts]
[root@shell01 /scripts]
total 16
drwxr-xr-x 2 root root 186 2020-02-19 15:56 oldboy
-rw-r--r-- 1 root root 440 2020-02-19 15:12 path.sh
-rw-r--r-- 1 root root 285 2020-02-19 15:25 test.sh
-rw-r--r-- 1 root root 196 2020-02-19 15:57 test.tar.gz
-rw-r--r-- 1 root root 308 2020-02-19 14:52 var.sh
[root@shell01 /scripts]
[root@shell01 /scripts]
[root@shell01 /scripts]
oldboy/test01.txt
oldboy/test02.txt
oldboy/test03.txt
oldboy/test04.txt
oldboy/test05.txt
oldboy/test06.txt
oldboy/test07.txt
oldboy/test08.txt
oldboy/test09.txt
oldboy/test10.txt
[root@shell01 /scripts]
[root@shell01 /scripts]
oldboy/test01.txt oldboy/test02.txt oldboy/test03.txt oldboy/test04.txt oldboy/test05.txt oldboy/test06.txt oldboy/test07.txt oldboy/test08.txt oldboy/test09.txt oldboy/test10.txt