第二章 Vim编辑器与Shell命令脚本

第二章 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	//IP地址
NETMASK=255.255.255.0	//子网掩码
GATEWAY=192.168.2.1	//网关地址
DNS1=192.168.1.1	//DNS地址
//重启网卡
[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//ping网关测试是否联通
[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、配置软件仓库
//进入yum源目录
[root@centos ~]# cd /etc/yum.repos.d/
//创建软件仓库配置文件
[root@centos yum.repos.d]# vim centos8.repo
[BaseOS]
name=BaseOS
baseurl=file:///media/cdrom/BaseOS
enabled=1
gpgcheck=0
[AppStream]
name=AppStream
baseurl=file:///media/cdrom/AppStream
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
//安装httpd服务
[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、查看当前所在的工作路径并列出当前目录下所有的文件及属性信息
//创建并编辑example.sh脚本
[root@centos ~]# vim example.sh
#! /bin/bash
#查看当前所在的工作路径并列出当前目录下所有的文件及属性信息
pwd
ls -al
//运行脚本
[root@centos ~]# bash example.sh 
/root
总用量 96392
dr-xr-x---. 15 root root     4096 413 20:25 .
dr-xr-xr-x. 17 root root      224 1113 18:58 ..
drwxr-xr-x.  2 root root        6 1113 19:26 公共
drwxr-xr-x.  2 root root        6 1113 19:26 模板
drwxr-xr-x.  2 root root        6 1113 19:26 视频
drwxr-xr-x.  2 root root        6 1113 19:26 图片
drwxr-xr-x.  2 root root        6 1113 19:26 文档
drwxr-xr-x.  2 root root        6 1113 19:26 下载
drwxr-xr-x.  2 root root        6 1113 19:26 音乐
drwxr-xr-x.  2 root root        6 1115 19:29 桌面
-rw-------.  1 root root     1086 1113 19:10 anaconda-ks.cfg
-rw-------.  1 root root      246 1115 19:31 .bash_history
-rw-r--r--.  1 root root       18 511 2019 .bash_logout
-rw-r--r--.  1 root root      176 511 2019 .bash_profile
-rw-r--r--.  1 root root      176 511 2019 .bashrc
drwx------. 13 root root      287 413 20:18 .cache
drwx------. 13 root root      250 413 20:18 .config
-rw-r--r--.  1 root root      100 511 2019 .cshrc
drwx------.  3 root root       25 1113 19:23 .dbus
-rw-------.  1 root root       16 1113 19:26 .esd_auth
-rw-r--r--.  1 root root      113 413 20:25 example.sh
-rw-------.  1 root root      620 1115 19:28 .ICEauthority
-rw-r--r--.  1 root root     1363 1113 19:23 initial-setup-ks.cfg
-rwxrw-rw-.  1 root root 98638343 413 15:59 Linux就该这么学.pdf
drwx------.  3 root root       19 1113 19:26 .local
drwxr-----.  3 root root       19 1113 19:26 .pki
-rw-r--r--.  1 root root      129 511 2019 .tcshrc
-rw-------.  1 root root    11226 413 20:25 .viminfo
-rw-r--r--.  1 root root     1235 1115 19:30 yum.txt
2、接受用户参数

文件测试所用的参数

参数作用
$0当前Shell脚本程序的名称
$#总共有几个参数
$*所有位置的参数值
$?显示上一次命令的执行返回值
$1对应第N个位置的参数值

可用的整数比较运算符

操作符作用
-eq是否等于
-ne是否不等于
-gt是否大于
-lt是否小于
-le是否等于或小于
-ge是否大于或等于

常见的字符串比较运算符

操作符作用
=比较字符串内容是否相同
!=比较字符串内容是否不同
-z判断字符串内容是否为空
//创建并编辑example.sh脚本
[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条件测试语句
//创建并编辑host.sh脚本
#!/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.
//创建并编辑host.sh脚本
[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条件循环语句
//创建IP地址簿
[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
//创建并编辑检测IP地址簿的CheckHost脚本
[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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liebe1*1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值