linux shell脚本 编程

本教程详细介绍了Linux Shell脚本编程,从基础知识到高级应用,包括脚本编写、运行、特殊符号、重定向、数学运算、循环控制、条件判断、函数及正则表达式等内容,并提供了多个实战例子,助你掌握Shell编程精髓。

介绍

白树明
敲代码的流川枫
天易IT学院
北京天易博通科技有限公司 执行总裁
12小时视频
https://www.bilibili.com/video/BV1j541157Sr?p=1

流老师的笔记地址
https://book.apeland.cn/details/274/
在这里插入图片描述

本示例在 zhan 192.168.121.37 上面执行

P01_为什么学习shell    22:52
P02_shell介绍    12:04
P03_理解shell脚本精髓    15:46
P04_shell脚本语法规范1    29:21
P05_shell语法2    32:13
P06_shell格式化输出    25:28
P07_shell程序交互    19:30
P08_变量介绍    15:17
P09_shell变量类型    08:50
P10_shell变量管理    13:31
P11_shell数组    23:37
P12_shell数据比较运算    16:49
P13_文件类型判断    15:03
P14_其他运算    09:42
P15_if语法    25:34
P16_for语法    26:19
P17_shell循环控制    31:33
P18_while语法    22:58
P19_while循环控制与语句嵌套    19:14
P20_until循环    07:15
P21_case语句    19:37
P22_函数介绍    16:17
P23_nginx_启动管理脚本实战    33:46
P24_正则表达式之特殊字符    21:00
P25_正则表达式POSIX字符    16:24
P26_shell对文件的操作    24:29
P27_sed命令详解    18:01
P28_shell_awk基本语法    28:39
P29_awk高级用法1    24:00
P30_awk高级语法2    19:27
P31_监测一个主机状态    23:26
P32_监控一个服务端口    20:57
P33_监控memory使用率    14:17
P34_统计使用内存或CPU前十名进程    25:10
P35_io_队列长度监控    22:34
P36_监控脚本总结    14:45
P36_nginx_install_1    25:26
P38_nginx_install_02    23:30
P39_lamp安装脚本    16:23
P40_mysql_备份脚本    32:06
P41_mysql_备份脚本排错方法    18:30
P42_创建user01-user20用户    23:53

P01_为什么学习shell 22:52

shell perl python 三种脚本

shell 学习分为几个阶段

  1. 能看懂shell脚本
    2)能改shell脚本
    3)能自己写shell脚本
    4)能优化shell脚本

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

P02_shell介绍 12:04

shell是一个程序,采用C语言编写,是用户和linux内核沟通的桥梁。
在这里插入图片描述

kernel: 管理硬件,驱动硬件做事
shell: 命令解释器
user: 用户接口对接用户

[root@aliyunleo ~]# echo "hello world"
hello world
[root@aliyunleo ~]# echo "hello world" | sed 's/world/dddd/'
hello dddd
[root@aliyunleo ~]# echo $USER
root

P03_理解shell脚本精髓 15:46

如何书写一个shell
shell脚本运行
shell中的特殊符号
管道
重定向
shell中数学运算
脚本退出

shell脚本就是将完成一个任务的所有命令按照执行的先后顺序,自伤而下写入到一个文本文件中,然后给予执行权限。

第一个shell脚本

第一个shell脚本编写过程,自动安装nginx

step1  进入 相应目录 新建nginx_install.sh 文件
/opt/shell/shell_03
touch   nginx_install.sh

step2   编辑文件 

yum install -y wget gcc pcre-devel zlib-devel
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar xf nginx-1.16.0.tar.gz
cd nginx-1.16.0
./configure --prefix=/usr/local/nginx
make -j 1
make install

step3 
chmod 700 nginx_install.sh 

step4   执行脚本 
./nginx_install.sh  

step5 
--启动nginx  
/usr/local/nginx   会有相应的文件 
进入 /usr/local/nginx/sbin
执行 ./nginx  启动 nginx  

step6   关闭防火墙  
sysctmctl stop firewalld
systemctl disable firewalld

step7 测试
到实体机上面访问相应网站,本示例为 192.168.121.37
http://192.168.121.37

在这里插入图片描述

P04_shell脚本语法规范1 29:21

shell 语法有:

  1. 如何书写一个脚本
  2. shell 脚本运行
  3. shell 中的特殊符号
  4. 管道
  5. 重定向
  6. shell 中数学运算
  7. 脚本退出

1 如何定义一个脚本

  1. #定义脚本的执行环境 #! 是组合,不是注释
    [root@shell37 sbin]# which bash
    /usr/bin/bash

#!/usr/bin/bash

2.# 是注释

  1. 脚本信息
    #Author: weilei
    #Created Time: 2021/03/03 16:33:33
    #Release: 2.2
    #Script Description: nginx install script

编辑VIM可以自动出这个

  1. 脚本组成
    #解释环境
    #注释说明
    #执行代码

2 如何运行一个脚本

touch a1.sh
echo “hello world”

  1. 给执行权限 chmod 700 a1.sh 然后执行 ./a.sh
  2. 使用解释器直接执行 不需要给予权限 bash a1.sh | sh a1.sh
  3. 查看当前环境的解释器 cat /etc/shells
    [root@shell37 shell_04]# cat /etc/shells
    /bin/sh
    /bin/bash
    /usr/bin/sh
    /usr/bin/bash
    /bin/tcsh
    /bin/csh

3 shell 中的特殊符号

符号 解释
~ 家目录 cd ~ 代表进入用户家目录
执行历史命令 !! 执行上一条命令
$ 变量中取内容符
±*/% 数学运算
& 后台执行
* 通配符 匹配所有
? 通配符 匹配除回车外 任意一个字符
; 多个命令之间用;
| 管道符 ,上一个命令输出作为下一个命令输入
\ 转移字符
`` 反引号 命令中执行命令 echo “ today is date +%F” 输出 today is : 2021-03-03
‘’ 单引号不解释变量 echo ‘$USER’ 输出 $USER
“” 脚本中的字符串 echo “$USER” 输出 root

在这里插入图片描述

P05_shell语法2 32:13

shell 语法有:

  1. 如何书写一个脚本
  2. shell 脚本运行
  3. shell 中的特殊符号
  4. 管道
  5. 重定向
  6. shell 中数学运算
  7. 脚本退出

5.重定向

> 重定向输入
>> 重定向追加输入
< 重定向输出 wc -l < /etc/passwd
<< 重定向追加输出 fdisk /dev/sdb <<EFO … EOF

划分磁盘空间指令

对fdisk 指令需要单独学习,暂时没有懂 20210304

----查看所有磁盘
[root@shell37 ~]# fdisk -l    

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b815d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     4810751     2097152   82  Linux swap / Solaris
/dev/sda3         4810752    41943039    18566144   83  Linux


----显示  /dev/sda3 是否有分区 
[root@shell37 ~]# fdisk -l /dev/sda3

Disk /dev/sda3: 19.0 GB, 19011731456 bytes, 37132288 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes



disk_partition.sh 

#!/usr/bin/bash
#Author:wl
#Create Time : 20210304
#Script description: disk partition script
fdisk /dev/sda3 <<EOF
n
p
4

+100M
w
EOF

执行 bash disk_partition.sh 命令后 ,看到 /dev/sda3 已经分配了 /dev/sda3p4 磁盘空间 

[root@shell37 ~]# fdisk -l /dev/sda3

Disk /dev/sda3: 19.0 GB, 19011731456 bytes, 37132288 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x1173942e

     Device Boot      Start         End      Blocks   Id  System
/dev/sda3p4         1093632     1298431      102400   83  Linux

6. shell 中数学运算

expr


[root@shell37 shell_04]# expr 1 + 1
2
[root@shell37 shell_04]# expr 1 - 1
0
[root@shell37 shell_04]# expr 1 \* 2
2
[root@shell37 shell_04]# expr 10 /  4
2
[root@shell37 shell_04]# expr 1 + 2.2.
expr: non-integer argument



[root@shell37 shell_04]# expr 1 + 0
1
[root@shell37 shell_04]# echo $?    --- 打印上一条结果  
0
[root@shell37 shell_04]# expr 1 + 2.2
expr: non-integer argument
[root@shell37 shell_04]# echo $?
2
[root@shell37 shell_04]# expr 1 + 7 &> /dev/null ; echo $?
0

let

[root@shell37 shell_04]# let a=1+2
[root@shell37 shell_04]# echo $a
3

bc


[root@shell37 shell_04]# bc 
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 

1+1
2
3*3
9
5/4
1
(1+2)*3/2+444
448
scale =2                  -------设置2位小数点
4/3
1.33




[root@shell37 ~]# echo "scale=2; 142*1000/2333" | bc; 
60.86
[root@shell37 ~]# echo "`echo "scale=2; 142*1000/2333" | bc;` %"
[root@shell37 ~]# echo "当 前 内存 使用   率  :  `echo "scale=2; 142*1000/2333" | bc;` %"
当前 内存 使用   率  :  60.86 %



$(()) 做运算

#  其中的数据只能是整数  
[root@shell37 shell]# echo $((18*10))
180

7. 脚本退出

exit


vim exit.sh
#!/bin/bash
echo "haha"
exit 2

[root@shell37 shell]# bash exit.sh
haha
[root@shell37 shell]# echo $?
2

P06_shell格式化输出 25:28

-n — 后面不换行
-e — 后面解释为转移字符
\t —tab
\b – 删除 前一个字符
\n —换行且光标移至行首

倒计时 time.bash

版本0.1


vim time.sh
#!/bin/bash
for time in `seq 9 -1 0`;do
  echo $time
done
~


[root@shell37 shell]# sh time.sh
9
8
7
6
5
4
3
2
1

版本1.0

 echo -n -e "\b$time"    ---  -n 不换行 -e  解释后面的
echo             ---最后换行 
#!/bin/bash
for time in `seq 9 -1 0`;do
  echo -n -e "\b$time"   
  sleep 1
done
echo           
~
~

水果商店

#!/bin/bash
echo -e "\t\t\t\t\t Fruits Shop"
echo -e "\t1) Apple"
echo -e "\t2) Orange"
echo -e "\t3) Banana"

[root@shell37 shell]# sh fruits.sh 
                                         Fruits Shop
        1) Apple
        2) Orange
        3) Banana

颜色代码

echo -e "\033[背景色; 文字色m 字符串 \033[特效m
echo -e “\033[41;36m someting here \033[0m”

背景色范围: 40–47
文字颜色范围: 30-37
特效: 0-8

echo -e "\033[41;31m someting here \033[4m"

本机示例没有效果 ,愿意可能是自己本机的vim的原因???

P07_shell程序交互 19:30

第七节 程序交互

read 命令选项

-p 打印信息
-t 限定时间
-s 不回显
-n 输入字符个数

在这里插入图片描述

#!/bin/bash
clear
echo -n -e "Login:"
read
echo -n -e "Password:"
read
echo "account:    password:   "



[root@shell37 shell]# sh read_command.sh
Login:ss
Password:pwd
account:    password:   
[root@shell37 shell]# 

vim read_command2.sh 
#!/bin/bash
clear
echo -n -e "Login:"
read name
echo -n -e "Password:"
read pwd
echo "account: $name   password: $pwd  "


[root@shell37 shell]# sh read_command2.sh 
Login:ss
Password:pwd
account: ss   password: pwd  
[root@shell37 shell]# 
#!/bin/bash
clear
echo -n -e "Login:"
read name
echo -n -e "Password:"
#-s security do not show text
#-t9  9 seconds exit
#-n6  max length is 6
read -s -t9 -n6 pwd
echo
echo "account: $name   password: $pwd  "

#!/bin/bash
clear
read -p "Login:" name
echo -n -e "Password:"
read pwd
echo
echo "account: $name   password: $pwd  "
~

Password:sss
account: wel   password: sss  
[root@shell37 shell]# 

[root@shell37 shell]# vim mockLogin.sh 
#!/usr/bin/bash
echo "CentOS Linux 7 (Core)"
echo "Kernel `uname -r ` an `uname -m` \n   "

echo -n -e "$HOSTNAME   login: "
read acc
read -p "password: "
read pwd


[root@shell37 shell]# sh mockLogin.sh 
CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 an x86_64 \n   
shell37   login: wl
password: ll

[root@shell37 shell]# 

P08_变量介绍 15:17

变量介绍
变量分类
变量管理

P09_shell变量类型 08:50

变量分类:
1.本地变量:用户私有变量,只有本用户使用放在加目录 .bash_profile/ .bashrc文件中
2.全局变量:所有用户都可以使用, 保存在 /etc/profile / etc/bashrc 文件中
3.用户自定义变量:脚本中用户自定义的变量

NAME='LILI'
AGE=22
SCORE=100

echo "name:$NAME,age:$AGE,score:$SCORE"
~

[root@shell37 shell]# sh vartest.sh 
name:LILI,age:22,score:100

P10_shell变量管理 13:31

在这里插入图片描述

1.变量定义 XX=123;
2.变量读取 echo $XX;
3.取消变量 unset XX

注意 变量名与等号之间没有空格
老师建议变量名为大写

[root@shell37 shell]# NAME=LILI           ----- OK 
[root@shell37 shell]# NAME2 = LEILEI      ----- FAILED 
bash: NAME2: command not found...



[root@shell37 shell]# XX=33
[root@shell37 shell]# echo $XX;
33
[root@shell37 shell]# unset XX;
[root@shell37 shell]# echo $XX
[root@shell37 shell]# vim /etc/profile
GENDER='male'        ---- 本地变量 
export SEX='man'     ----全局变量 
[root@shell37 shell]# echo $GENDER

[root@shell37 shell]# echo $SEX

[root@shell37 shell]# source /etc/profile
[root@shell37 shell]# echo $GENDER
male
[root@shell37 shell]# echo $SEX
man



P11_shell数组 23:37

数组介绍
基本数组
关联数组
案例分享

基本数组

[root@shell37 shell]# vim arr.sh 
#!/bin/bash
#test array
#01   批量声明数组  及 打印某个数组项
username=(weilei xiaosan lisi zhangsan)
echo ${
   
   username[2]}


#02  单独声明某个数组项
ARRAY2=('1' '2')
ARRAY2[2]='c'
echo ${
   
   ARRAY2[2]}

# 查看所有数组
declare -a



#  ARRAY2=1 2 c D
# single    c   
echo ${
   
   ARRAY2[2]
概要:   DevCon 实用工具是一种命令行实用工具,可以替代设备管理器。使用 DevCon,您可以启用禁用、重新启动、更新、删除查询单个设备或一组设备。DevCon 提供与开发人员有关但无法在设备管理器中看到的信息。   您可以将 DevCon 用于 Windows 2000 、Windows XPWindows vista。不能将 Devcon 用于 Microsoft Windows 95、Windows 98、或 Windows Millennium Edition。   下载:http://download.microsoft.com/download/1/1/f/11f7dd10-272d-4cd2-896f-9ce67f3e0240/devcon.exe 用法及参数说明:   devcon.exe [-r] [-m:\\] [...]   -r 如果指定它,在命令完成后若需要则重新启动计算机。    是目标计算机的名称。    是将要执行的命令(如下所示)。   ... 是命令需要的一个或多个参数。   要获取关于某一特定命令的帮助,请键入:devcon.exe help   classfilter 允许修改类别筛选程序。   classes 列出所有设备安装类别。   disable 禁用与指定的硬件或实例 ID 匹配的设备。   driverfiles 列出针对设备安装的驱动程序文件。   drivernodes 列出设备的所有驱动程序节点。   enable 启用与指定的硬件或 实例 ID 匹配的设备。   find 查找与指定的硬件或 实例 ID 匹配的设备。   findall 查找设备,包括那些未显示的设备。   help 显示此信息。   hwids 列出设备的硬件 ID。   install 手动安装设备。   listclass 列出某一安装类别的所有设备。   reboot 重新启动本地计算机。   remove 删除与特定的硬件或 实例 ID 匹配的设备。   rescan 扫描以发现新的硬件。   resources 列出设备的硬件资源。   restart 重新启动与特定的硬件或 实例 ID 匹配的设备。   stack 列出预期的设备驱动程序堆栈。   status 列出设备的运行状态。   update 手动更新设备。   UpdateNI 手动更新设备,无用户提示   SetHwID 添加、删除更改根枚举设备的硬件 ID 的顺序。 示例:   devcon -m:\\test find pci\* 列出计算机 test 上的所有已知 PCI 设备。(通过使用 -m,您可以指定一个目标计算机。您必须使用“进程间通信”(IPC) 访问此计算机。)   devcon -r install Windows directory\Inf\Netloop.inf *MSLOOP 安装一个新的 Microsoft 环回适配器实例。这将创建一个新的根枚举设备节点,使用此节点您可以安装“虚拟设备”,如环回适配器。如果需要重新启动计算机,此命令还将以安静模式重启计算机。   devcon classes 列出所有已知的安装类别。输出结果包含短的未本地化的名称(例如,“USB”)描述性名称(例如,“通用串行总线控制器”)。 禁用启用网卡的步骤:   1.用devcon hwids PCI*命令得到所有以PCI开头的设备。这时会列出很多设备,那么哪个才是网卡对应的呢?   2.打开设备管理器,展开网络适配器,找到网卡的名称,然后记住到刚才得到的列表中找对应的Name,然后你会在下面看到好几个ID,随便挑一个就行   3.用devcon disable "PCI\VEN_11AB&DEV_4380&SUBSYS_301B17AA&REV_10"禁用网卡(启用的话讲disable换成enable就行了)   4.其实用PCI开头得到的几组设备中一般第一个就是网卡设备 sysdzw 16:01 2010-11-16
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值