文章目录
第二章 Vim编辑器与Shell命令脚本
一、Vim文本编辑器
1、命令模式中常用的命令
命令 | 作用 |
---|
dd | 删除(剪切)光标所在的整行 |
5dd | 删除(剪切)从光标处开始的5行 |
yy | 复制光标所在的整行 |
5yy | 复制从光标处开始的5行 |
n | 显示搜索命令定位到的下一个字符串 |
N | 显示搜索命令定位到的上一个字符串 |
u | 撤销上一步的操作 |
p | 将之前删除(dd)或复制(yy)过的数据粘贴到光标后面 |
2、末行模式种常用的命令
命令 | 作用 |
---|
:w | 保存 |
:q | 退出 |
:q! | 强制退出 |
:wq! | 强制保存退出 |
:set nu | 显示行号 |
:set nonu | 不显示行号 |
:命令 | 执行该命令 |
:整数 | 跳转到该行 |
😒/one/two | 将当前光标所在的行的第一个one替换成two |
😒/one/two/g | 将当前光标所在行的所有one替换成two |
:%s/one/two/g | 将全文中的所有one替换成two |
?字符串 | 在文本中从下至上搜索该字符串 |
/字符串 | 在文本中从上至下搜索该字符串 |
3、修改主机名称
[root@centos ~]# cat /etc/hostname
centos
[root@centos ~]# vim /etc/hostname
[root@centos ~]# cat /etc/hostname
Centos
4、配置网卡信息
[root@centos ~]# cd /etc/sysconfig/network-scripts/
[root@centos network-scripts]# vim ifcfg-ens160
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=ens160
UUID=c6fe444d-de99-4b51-ad07-a48506b18fda
DEVICE=ens160
ONBOOT=yes
IPADDR=192.168.2.50
NETMASK=255.255.255.0
GATEWAY=192.168.2.1
DNS1=192.168.1.1
[root@centos network-scripts]# nmcli connection down ens160 && nmcli connection up ens160
成功停用连接 "ens160"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6)
[root@centos network-scripts]# ping 192.168.2.1
PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.
64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=1.09 ms
64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=1.06 ms
64 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=1.07 ms
^C
--- 192.168.2.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2025ms
rtt min/avg/max/mdev = 1.056/1.074/1.094/0.015 ms
5、配置软件仓库
[root@centos ~]# cd /etc/yum.repos.d/
[root@centos yum.repos.d]# vim centos8.repo
[BaseOS]
name=BaseOS
baseurl=file:
enabled=1
gpgcheck=0
[AppStream]
name=AppStream
baseurl=file:
enabled=1
gpgcheck=0
[root@centos yum.repos.d]# mkdir -p /media/cdrom
[root@centos yum.repos.d]# mount /dev/cdrom /media/cdrom/
mount: /media/cdrom: WARNING: device write-protected, mounted read-only.
[root@centos yum.repos.d]# vim /etc/fstab
/dev/cdrom /media/cdrom iso9660 defaults 0 0
[root@centos yum.repos.d]# yum install httpd -y
Repository AppStream is listed more than once in the configuration
BaseOS 3.7 MB/s | 2.6 MB 00:00
依赖关系解决。
==========================================================================
软件包 架构 版本 仓库 大小
==========================================================================
安装:
httpd x86_64 2.4.37-43.module_el8.5.0+1022+b541f3b1
AppStream 1.4 M
安装依赖关系:
apr x86_64 1.6.3-12.el8 AppStream 129 k
apr-util x86_64 1.6.1-6.el8 AppStream 105 k
centos-logos-httpd noarch 85.8-2.el8 base 75 k
httpd-filesystem noarch 2.4.37-43.module_el8.5.0+1022+b541f3b1
AppStream 39 k
httpd-tools x86_64 2.4.37-43.module_el8.5.0+1022+b541f3b1
AppStream 107 k
mod_http2 x86_64 1.15.7-3.module_el8.4.0+778+c970deab
AppStream 154 k
安装弱的依赖:
apr-util-bdb x86_64 1.6.1-6.el8 AppStream 25 k
apr-util-openssl x86_64 1.6.1-6.el8 AppStream 27 k
启用模块流:
httpd 2.4
事务概要
==========================================================================
安装 9 软件包
总下载:2.1 M
安装大小:5.6 M
下载软件包:
(1/9): apr-util-1.6.1-6.el8.x86_64.rpm 660 kB/s | 105 kB 00:00
(2/9): centos-logos-httpd-85.8-2.el8.noar 401 kB/s | 75 kB 00:00
(3/9): apr-1.6.3-12.el8.x86_64.rpm 672 kB/s | 129 kB 00:00
(4/9): apr-util-bdb-1.6.1-6.el8.x86_64.rp 457 kB/s | 25 kB 00:00
(5/9): apr-util-openssl-1.6.1-6.el8.x86_6 491 kB/s | 27 kB 00:00
(6/9): httpd-filesystem-2.4.37-43.module_ 631 kB/s | 39 kB 00:00
(7/9): httpd-tools-2.4.37-43.module_el8.5 1.4 MB/s | 107 kB 00:00
(8/9): mod_http2-1.15.7-3.module_el8.4.0+ 1.4 MB/s | 154 kB 00:00
(9/9): httpd-2.4.37-43.module_el8.5.0+102 2.6 MB/s | 1.4 MB 00:00
--------------------------------------------------------------------------
总计 2.8 MB/s | 2.1 MB 00:00
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
准备中 : 1/1
安装 : apr-1.6.3-12.el8.x86_64 1/9
运行脚本: apr-1.6.3-12.el8.x86_64 1/9
安装 : apr-util-bdb-1.6.1-6.el8.x86_64 2/9
安装 : apr-util-openssl-1.6.1-6.el8.x86_64 3/9
安装 : apr-util-1.6.1-6.el8.x86_64 4/9
运行脚本: apr-util-1.6.1-6.el8.x86_64 4/9
安装 : httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_ 5/9
运行脚本: httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1 6/9
安装 : httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1 6/9
安装 : centos-logos-httpd-85.8-2.el8.noarch 7/9
安装 : mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64 8/9
安装 : httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64 9/9
运行脚本: httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64 9/9
验证 : centos-logos-httpd-85.8-2.el8.noarch 1/9
验证 : apr-1.6.3-12.el8.x86_64 2/9
验证 : apr-util-1.6.1-6.el8.x86_64 3/9
验证 : apr-util-bdb-1.6.1-6.el8.x86_64 4/9
验证 : apr-util-openssl-1.6.1-6.el8.x86_64 5/9
验证 : httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64 6/9
验证 : httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1 7/9
验证 : httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_ 8/9
验证 : mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64 9/9
已安装:
apr-1.6.3-12.el8.x86_64
apr-util-1.6.1-6.el8.x86_64
apr-util-bdb-1.6.1-6.el8.x86_64
apr-util-openssl-1.6.1-6.el8.x86_64
centos-logos-httpd-85.8-2.el8.noarch
httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
httpd-filesystem-2.4.37-43.module_el8.5.0+1022+b541f3b1.noarch
httpd-tools-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64
mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64
完毕!
二、编写Shell脚本
1、查看当前所在的工作路径并列出当前目录下所有的文件及属性信息
[root@centos ~]# vim example.sh
#! /bin/bash
#查看当前所在的工作路径并列出当前目录下所有的文件及属性信息
pwd
ls -al
[root@centos ~]# bash example.sh
/root
总用量 96392
dr-xr-x---. 15 root root 4096 4月 13 20:25 .
dr-xr-xr-x. 17 root root 224 11月 13 18:58 ..
drwxr-xr-x. 2 root root 6 11月 13 19:26 公共
drwxr-xr-x. 2 root root 6 11月 13 19:26 模板
drwxr-xr-x. 2 root root 6 11月 13 19:26 视频
drwxr-xr-x. 2 root root 6 11月 13 19:26 图片
drwxr-xr-x. 2 root root 6 11月 13 19:26 文档
drwxr-xr-x. 2 root root 6 11月 13 19:26 下载
drwxr-xr-x. 2 root root 6 11月 13 19:26 音乐
drwxr-xr-x. 2 root root 6 11月 15 19:29 桌面
-rw-------. 1 root root 1086 11月 13 19:10 anaconda-ks.cfg
-rw-------. 1 root root 246 11月 15 19:31 .bash_history
-rw-r--r--. 1 root root 18 5月 11 2019 .bash_logout
-rw-r--r--. 1 root root 176 5月 11 2019 .bash_profile
-rw-r--r--. 1 root root 176 5月 11 2019 .bashrc
drwx------. 13 root root 287 4月 13 20:18 .cache
drwx------. 13 root root 250 4月 13 20:18 .config
-rw-r--r--. 1 root root 100 5月 11 2019 .cshrc
drwx------. 3 root root 25 11月 13 19:23 .dbus
-rw-------. 1 root root 16 11月 13 19:26 .esd_auth
-rw-r--r--. 1 root root 113 4月 13 20:25 example.sh
-rw-------. 1 root root 620 11月 15 19:28 .ICEauthority
-rw-r--r--. 1 root root 1363 11月 13 19:23 initial-setup-ks.cfg
-rwxrw-rw-. 1 root root 98638343 4月 13 15:59 Linux就该这么学.pdf
drwx------. 3 root root 19 11月 13 19:26 .local
drwxr-----. 3 root root 19 11月 13 19:26 .pki
-rw-r--r--. 1 root root 129 5月 11 2019 .tcshrc
-rw-------. 1 root root 11226 4月 13 20:25 .viminfo
-rw-r--r--. 1 root root 1235 11月 15 19:30 yum.txt
2、接受用户参数
参数 | 作用 |
---|
$0 | 当前Shell脚本程序的名称 |
$# | 总共有几个参数 |
$* | 所有位置的参数值 |
$? | 显示上一次命令的执行返回值 |
$1 | 对应第N个位置的参数值 |
操作符 | 作用 |
---|
-eq | 是否等于 |
-ne | 是否不等于 |
-gt | 是否大于 |
-lt | 是否小于 |
-le | 是否等于或小于 |
-ge | 是否大于或等于 |
操作符 | 作用 |
---|
= | 比较字符串内容是否相同 |
!= | 比较字符串内容是否不同 |
-z | 判断字符串内容是否为空 |
[root@centos ~]# vim example.sh
#!/bin/bash
#按位输出参数
echo "The current name of the script is:$0."
echo "There are a total of $# parameters,They are $*, respectively."
echo "The first parameter is $1."
echo "The fifth parameter is $5."
[root@centos ~]# ./example.sh one two three four five six
The current name of the script is:./example.sh.
There are a total of 6 parameters,They are one two three four five six, respectively.
The first parameter is one.
The fifth parameter is five.
3、if条件测试语句
#!/bin/bash
#ping IP判断是否存活,-c规定尝试的次数,-i规定每个数据包的发送间隔时间,-W规定等待超时时间
ping -c 3 -i 0.2 -W 3 $1 &> /dev/null
if [ $? -eq 0 ]
then
echo "Host $1 is On-line."
else
echo "Host $1 is Off-line."
fi
[root@centos ~]# ./host.sh 192.168.2.15
Host 192.168.2.15 is Off-line.
[root@centos ~]# ./host.sh 192.168.2.2
Host 192.168.2.2 is On-line.
[root@centos ~]# vim score.sh
#!/bin/bash
#判断用户输入的分数在那个成绩区间,超出提示不在范围内,read是用来读取用户输入的信息,-p是输出提示信息
read -p "Enter your score (0-100):" Grade
if [ $Grade -ge 85 ] && [ $Grade -le 100 ]
then
echo "$Grade is Excellent!"
elif [ $Grade -ge 75 ] && [ $Grade -lt 85 ]
then
echo "$Grade is Good!"
elif [ $Grade -ge 60 ] && [ $Grade -lt 75 ]
then
echo "$Grade is qualified!"
elif [ $Grade -ge 0 ] && [ $Grade -lt 60 ]
then
echo "$Grade is unqualified!"
else
echo "$Grade is not within the range!"
fi
[root@centos ~]# ./score.sh
Enter your score (0-100):90
90 is Excellent!
[root@centos ~]# ./score.sh
Enter your score (0-100):80
80 is Good!
[root@centos ~]# ./score.sh
Enter your score (0-100):70
70 is qualified!
[root@centos ~]# ./score.sh
Enter your score (0-100):50
50 is unqualified!
[root@centos ~]# ./score.sh
Enter your score (0-100):200
200 is not within the range!
4、for条件循环语句
[root@centos 脚本]# vim ipaddress.txt
192.168.2.1
192.168.2.2
192.168.2.3
192.168.2.4
192.168.2.5
192.168.2.6
192.168.2.7
192.168.2.8
192.168.2.9
192.168.2.10
[root@centos 脚本]# vim CheckHost.sh
#!/bin/bash
hostlist=$(cat ~/脚本/ipaddress.txt)
for IP in $hostlist
do
ping -c 3 -i 0.2 -W 3 $IP &> /dev/null
if [ $? -eq 0 ]
then
echo "Host $IP is Online."
else
echo "Host $IP is Offline."
fi
done
[root@centos 脚本]# ./CheckHost.sh
Host 192.168.2.1 is Online.
Host 192.168.2.2 is Offline.
Host 192.168.2.3 is Offline.
Host 192.168.2.4 is Offline.
Host 192.168.2.5 is Offline.
Host 192.168.2.6 is Offline.
Host 192.168.2.7 is Offline.
Host 192.168.2.8 is Offline.
Host 192.168.2.9 is Offline.
Host 192.168.2.10 is Offline.
5、while条件循环语句
[centos@centos 脚本]$ vim 猜价格.sh
#!/bin/bash
price=$(expr $RANDOM % 1000)
time=0
echo "商品实际价格为0-999之间,猜猜看是多少?"
while true
do
read -p "请输入您猜测的价格:" INT
let time++
if [ $INT -eq $price ] ;
then
echo "恭喜您答对了!实际价格是:$price"
echo "您总共猜测了$time次!"
exit
elif [ $INT -ge $price ] ;
then
echo "太高了!"
else
echo "太低了!"
fi
done
[centos@centos 脚本]$ ./猜价格.sh
商品实际价格为0-999之间,猜猜看是多少?
请输入您猜测的价格:100
太低了!
请输入您猜测的价格:999
太高了!
请输入您猜测的价格:500
太低了!
请输入您猜测的价格:600
太低了!
请输入您猜测的价格:800
太高了!
请输入您猜测的价格:700
太低了!
请输入您猜测的价格:750
太高了!
请输入您猜测的价格:725
太高了!
请输入您猜测的价格:720
太低了!
请输入您猜测的价格:723
太高了!
请输入您猜测的价格:721
恭喜您答对了!实际价格是:721
您总共猜测了11次!
6、case条件测试语句
[centos@centos 脚本]$ vim 判断类型.sh
#!/bin/bash
read -p "Please enter a character and press Enter to confirm:" key
case "$key" in
[a-z]|[A-Z])
echo "You entered a letter.";;
[0-9])
echo "You entered a number.";;
*)
echo "You enter spaces, function keys, or other control character."
esac
[centos@centos 脚本]$ ./判断类型.sh
Please enter a character and press Enter to confirm:6
You entered a number.
[centos@centos 脚本]$ ./判断类型.sh
Please enter a character and press Enter to confirm:z
You entered a letter.
[centos@centos 脚本]$ ./判断类型.sh
Please enter a character and press Enter to confirm:
You enter spaces, function keys, or other control character.
三、计划任务服务程序
1、at命令的参数及作用
参数 | 作用 |
---|
-f | 指定包含命令的任务文件 |
-q | 指定新任务名称 |
-l | 显示待执行任务列表 |
-d | 删除指定待执行任务 |
-m | 任务执行后给用户发邮件 |
2、将系统设置为在今晚23:00自动重启网站服务
[centos@centos ~]$ at 23:00
warning: commands will be executed using /bin/sh
at> systemctl restart httpd
at> <EOT>
job 1 at Thu Apr 20 23:00:00 2023
[centos@centos ~]$ echo "systemctl restart httpd" | at 23:00
warning: commands will be executed using /bin/sh
job 2 at Thu Apr 20 23:00:00 2023
[centos@centos ~]$ at -l
1 Thu Apr 20 23:00:00 2023 a centos
3、crontab命令的参数及作用
参数 | 作用 |
---|
-e | 编辑计划任务 |
-u | 指定用户名称 |
-l | 列出人物列表 |
-r | 删除计划任务 |
4、参数格式
分钟 小时 日期 月份 星期 命令
50 3 2 1 * run command
5、假设在每周一、三、五的凌晨三点二十五分,将某个网站的数据目录打包处理,使其作为一个备份文件
[centos@centos ~]$ crontab -e
no crontab for centos - using an empty one
crontab: installing new crontab
[centos@centos ~]$ crontab -l
25 3 * * 1,3,5 /usr/bin/tar -czvf backup.tar.gz /home/wwwroot