一、回顾
git
文件版本控制
#安装git
yum -y install git
#创建仓库
mkdir /gitrepo
cd /gitrepo
#初始化文件夹
git init
.git
#创建文件和目录
touch abc
mkdir efg
#将文件提交到缓存
git add .
#将缓存区域的文件提交仓库
git commit -m "没有理由"
#推送到远程仓库
git push
#获取远程仓库的更新
git pull
#克隆远程仓库
git clone
#分支,提高代码的灵活性
#检查分支
git branch
# *所在的行为的当前分支
#创建新分支
git branch 新分支名称
#跳转分支
git checkoutout 分支名称
#在跳转分支的同时建立新分支
git checkout -b 新分支名称
#删除分支
git checkout -d|D 分支名称
#合并分支
#跳转到主合并分支
git checkout a
git merge a
#合并冲突
手动解决
二、shell脚本编写:
[root@shell ~]# vim helloworld.sh
执行脚本的方式:
(1).[root@shell ~]# bash helloworld.sh
(2).[root@shell ~]# sh helloworld.sh
(3).[root@shell ~]# source helloworld.sh
(4).[root@shell ~]# chmod +x helloworld.sh
[root@shell ~]# ./helloworld.sh
安装nginx脚本编写:
[root@shell ~]# vim nginx.sh
[root@shell ~]# chmod +x nginx.sh
[root@shell ~]# ./nginx.sh
[root@shell ~]# vim test0001.sh
[root@shell ~]# bash test0001.sh 1 2 3 4 5
1
2
3
4
5
[root@shell ~]# bash test0001.sh wo hi ha ha ha
wo
hi
ha
ha
ha
[root@shell ~]# bash test0001.sh a b c d e
a
b
c
d
e
[root@shell ~]# vim useradd.sh
[root@shell ~]# bash useradd.sh hahaha 123
更改用户 hahaha 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@shell ~]# su hahaha
[hahaha@shell root]$
[root@shell ~]# vim shell001.sh
[root@shell ~]# bash shell001.sh 1 2 3 a b c
1 2 3 a b c
[root@shell ~]# vim shell001.sh
[root@shell ~]# bash shell001.sh 1 2 3 a b c
1
2
3
a
b
c
[root@shell ~]# vim shell001.sh
[root@shell ~]# bash shell001.sh 1 2 3 4 5 a b c d
1
2
3
4
5
a
b
c
d
0
9
shell001.sh
[root@shell ~]# test 3 -lt 2
[root@shell ~]# echo $?
1
[root@shell ~]# test 3 -gt 2
[root@shell ~]# echo $?
0
[root@shell ~]# aaa="abc"
[root@shell ~]# echo $aaa
abc[root@shell ~]# test $aaa == "abc"
[root@shell ~]# echo $?
0
[root@shell ~]# test $aaa == "aaaa"
[root@shell ~]# echo $?
1
[root@shell ~]# unset aaa
[root@shell ~]# echo $aaa
[root@shell ~]#
安装nginx:
判断是否安装
先要安装epel,没有的话就安装,有的话就卸载了再安装
三、文件/目录操作符:
[root@shell ~]# touch abc
[root@shell ~]# ls -lh abc
-rw-r--r--. 1 root root 0 7月 26 11:26 abc
[root@shell ~]# [ -e "abc" ]
[root@shell ~]# echo $?
0
[root@shell ~]# [ -e "lllll" ]
[root@shell ~]# echo $?
1
[root@shell ~]# [ -w "lllll" ]
[root@shell ~]# echo $?
1
[root@shell ~]# [ -w "abc" ]
[root@shell ~]# echo $?
0
[root@shell ~]# [ -x "abc" ]
[root@shell ~]# echo $?
1
[root@shell ~]# ls -l abc
-rw-r--r--. 1 root root 0 7月 26 11:26 abc
[root@shell ~]# chmod +x abc
[root@shell ~]# [ -x "abc" ]
[root@shell ~]# echo $?
0
[root@shell ~]# [ -z "abc" ]
[root@shell ~]# echo $?
1
[root@shell ~]#
四、与或判断
1.或运算判断
2.与运算判断
3.多重判断
[root@shell ~]# vim mulu.sh
[root@shell ~]# chmod +x mulu.sh
[root@shell ~]# ./mulu.sh
1新增文件 2删除文件 3查找文件 4修改文件
请输入序号选择功能:1
[root@shell ~]# ls
abc
anaconda-ks.cfg
hahahaha.txt
helloworld.sh
if.sh
mulu.sh
[root@shell ~]# ./mulu.sh
1新增文件 2删除文件 3查找文件 4修改文件
请输入序号选择功能:2
[root@shell ~]# ls
abc
anaconda-ks.cfg
helloworld.sh
if.sh
mulu.sh
五、read运用
输入内容的时候显示
[root@shell ~]# read -p "输入一个数据" s
输入一个数据abc
[root@shell ~]# echo $s
abc
输入内容的时候不显示
[root@shell ~]# read -p "输入一个数据" -s s
输入一个数据[[root@shell ~]# echo $s
lalalala
多个变量
[root@shell ~]# read -p "三个变量" a b c
三个变量12 16 15
[root@shell ~]# echo $a
12
[root@shell ~]# echo $b
16
[root@shell ~]# echo $c
15
案列:
[root@shell ~]# vim zhuce.sh
[root@shell ~]# bash zhuce.sh
[root@shell ~]# bash zhuce.sh
username:ha
password:ha
更改用户 ha 的密码 。
passwd:所有的身份验证令牌已经成功更新。
账户ha注册成功
加入-s,输入密码不显示
[root@shell ~]# vim zhuce.sh
[root@shell ~]# bash zhuce.sh
username:la
password:更改用户 la 的密码 。
passwd:所有的身份验证令牌已经成功更新。
账户la注册成功
六、监听
[root@shell ~]# yum -y install inotify-tools
[root@shell ~]# mkdir /abc
[root@shell ~]# nohup inotifywait -mr /abc
nohup: 忽略输入并把输出追加到"nohup.out"
重新开终端,创建目录/文件
[root@shell ~]# touch /abc/haha.txt
[root@shell ~]# cat nohup.out
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
/abc/ CREATE haha.txt
/abc/ OPEN haha.txt
/abc/ ATTRIB haha.txt
/abc/ CLOSE_WRITE,CLOSE haha.txt
[root@shell ~]# mkdir /abc/lalala
[root@shell ~]# cat nohup.out
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
/abc/ CREATE haha.txt
/abc/ OPEN haha.txt
/abc/ ATTRIB haha.txt
/abc/ CLOSE_WRITE,CLOSE haha.txt
/abc/ CREATE,ISDIR lalala
/abc/ OPEN,ISDIR lalala
/abc/ CLOSE_NOWRITE,CLOSE,ISDIR lalala
七、循环语法
[root@shell ~]# vim for.sh
[root@shell ~]# bash for.sh 我 喜 欢 长 相 思
我
喜
欢
长
相
思
[root@shell ~]# vim city.sh
[root@shell ~]# bash city.sh
青岛是个好地方
庆阳是个好地方
长沙是个好地方
[root@shell ~]# vim showuser.sh
[root@shell ~]# bash showuser.sh
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
[root@shell ~]# vim showuser.sh
[root@shell ~]# bash showuser.sh
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
sshd
postfix
chrony
user3
user4
mysql
hahaha
abc
ha
la
[root@shell ~]# vim gushi.txt
[root@shell ~]# vim showgushi.sh
[root@shell ~]# bash showgushi.sh
床前明月光
疑是地上霜
举头望明月
低头思故乡
八、awk的使用
[root@shell ~]# awk -F ":" '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
sshd
postfix
chrony
user3
user4
mysql
hahaha
abc
九、sed的使用
语法格式
Sed 选项 (定位符)指令 文件名 (定位符)指令--想对文件的哪一步进行操作
选项:
不加选项-n默认先全文打印再执行命令打印第二行
[root@localhost day04]# sed "2p" /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
加上-n不全文打印
[root@localhost day04]# sed -n "2p" /etc/hosts
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
打印第三行
[root@localhost day04]# sed -n "3p" /etc/passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
打印第一到第三行
[root@localhost day04]# sed -n "1,3p" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
打印奇数行 (行数从1开始每次自加2)
[root@localhost day04]# sed -n "1~2p" /etc/passwd
打印偶数行 (行数从2开始每次自加2)
[root@localhost day04]# sed -n "2~2p" /etc/passwd
打印第二行以及后面相邻的三行(行数,+数字)--表示行数以及后面相邻的数字行
[root@localhost day04]# sed -n "2,+3p" /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
正则定位
Sed可以使用正则匹配需要数据然后编辑数据
过滤root开头的行
[root@localhost day04]# grep "^root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost day04]# sed -n "/^root/p" /etc/passwd
root:x:0:0:root:/root:/bin/bash
过滤三位数
[root@localhost day04]# grep -E "[0-9]{3}" /etc/passwd
[root@localhost day04]# sed -rn "/[0-9]{3}/p" /etc/passwd
sed修改配置:
Sed修改配置
Sed编辑指令
常用指令
P(print):打印行
d(delete):删除行
c(replace):替换行
s( substitution):替换关键字
=:打印行号
过滤数据
Print指令
[root@localhost day04]# sed -n "/IPADDR/p" /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.164.50
[root@localhost day04]# grep "IPADDR" /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.164.50
[root@localhost day04]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 4.7G 13G 28% /
devtmpfs 471M 0 471M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 8.6M 479M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 157M 858M 16% /boot
tmpfs 98M 4.0K 98M 1% /run/user/42
tmpfs 98M 32K 98M 1% /run/user/0
/dev/sr0 8.8G 8.8G 0 100% /run/media/root/CentOS 7 x86_64
[root@localhost day04]# df -h /
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 4.7G 13G 28% /
[root@localhost day04]# df -h | sed -n "/\/$/p"
/dev/mapper/centos-root 17G 4.7G 13G 28% /
[root@localhost day04]# sed -n "1p;3p;6p" /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
[root@localhost day04]# sed -n '2!p' /etc/passwd
删除数据(delete指令)
如果不指定定位符默认匹配全文匹配 指定-i选项就可以删除原文件
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]# sed 'd' /etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]# cat /etc/fstab > /tmp/fstab
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# sed '1d' fstab 删除一行
[root@localhost tmp]# sed '1,3d' fstab 删除一到三行
[root@localhost tmp]# sed '/dev/!d' fstab 删除不包含dev的行(取反 !在定位符的后面)
[root@localhost tmp]# sed '/^#/d' fstab删除以#开头的行
[root@localhost tmp]# sed '/^$/d' fstab删除空行
永久删除--删除1-4行
[root@localhost tmp]# sed -i '1,4d' fstab
替换行(replace c以行为单位)
[root@localhost tmp]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost tmp]# sed '/4$/c 127.0.0.1 localhost' /etc/hosts
127.0.0.1 localhost
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost tmp]# sed '4c XXXX' /etc/shells 把第四行替换成XXXX
s( substitution):替换关键字
格式:s/旧的/新的
新建文件
[root@localhost tmp]# cd /root/shell/day04
[root@localhost day04]# pwd
/root/shell/day04
[root@localhost day04]# vim test.txt
[root@localhost day04]# cat test.txt
2046 2048 2046 2046
1001 2046 1999 1888
2046 2046 2046 2046
s默认是每一行 数字s(数字行) 最后一个/区域默认就是第一个旧的--g(每一个旧的)
区域(哪一个)s/旧的/新的/区域(哪一个旧的)
[root@localhost day04]# sed 's/2046/XXXX/' test.txt 将每一行的第一个2046替换成XXXX
XXXX 2048 2046 2046
1001 XXXX 1999 1888
XXXX 2046 2046 2046
[root@localhost day04]# sed 's/2046/XXXX/g' test.txt 将每一行的每一个2046替换成XXXX
XXXX 2048 XXXX XXXX
1001 XXXX 1999 1888
XXXX XXXX XXXX XXXX
[root@localhost day04]# sed 's/2046/XXXX/2' test.txt 将每一行的每二个2046替换成XXXX
2046 2048 XXXX 2046
1001 2046 1999 1888
2046 XXXX 2046 2046
[root@localhost day04]# sed 's/2046/(&)/' test.txt 将每一行的2046替换成(2046)---&可以使用旧的内容
(2046) 2048 2046 2046
1001 (2046) 1999 1888
(2046) 2046 2046 2046
[root@localhost day04]# sed '2s/2046/XXXX/g' test.txt 将每二行所有的2046替换成XXXX
2046 2048 2046 2046
1001 XXXX 1999 1888
2046 2046 2046 2046
[root@localhost day04]# sed '2s/2046//g' test.txt 新的内容为空,代表删除这个数据
2046 2048 2046 2046
1001 1999 1888
2046 2046 2046 2046
[root@localhost day04]# sed -n '2s/2046/XXXX/p' test.txt 将第二行的第一个2046替换成XXXX并打印出来
1001 XXXX 1999 1888
将替换符改为其他字符也可以达到同样的效果
[root@localhost day04]# sed 's/2046/XXXX/g' test.txt
XXXX 2048 XXXX XXXX
1001 XXXX 1999 1888
XXXX XXXX XXXX XXXX
[root@localhost day04]# sed 's#2046#XXXX#g' test.txt #号
XXXX 2048 XXXX XXXX
1001 XXXX 1999 1888
XXXX XXXX XXXX XXXX
[root@localhost day04]# sed 's,2046,XXXX,g' test.txt ,号
XXXX 2048 XXXX XXXX
1001 XXXX 1999 1888
XXXX XXXX XXXX XXXX
[root@localhost day04]# sed 's220462XXXX2g' test.txt
XXXX 2048 XXXX XXXX
1001 XXXX 1999 1888
XXXX XXXX XXXX XXXX
[root@localhost day04]# echo "hello the world" | sed -r 's/^(.)(.*)(.)$/\3\2\1/'
dello the worlh
[root@localhost day04]# echo "hello the world" | sed -r 's/^(.)(.*)(.)$/\3\2\1/'dello the worlh
[root@localhost day04]# echo "hello the world" | sed -r 's/^(.)(.*)(.)$/\2\3\1/'
ello the worldh
[root@localhost day04]# echo "hello the world" | sed -r 's/^(.)(.*)(.)$/\1\1\3/'
Hhd
sed多行文本处理:
Sed多行文本处理
文本块指令
常用指令
i(insert):插入 (没有-i选项无法写入文件)
a(append):追加
r(read):读取文件
w(write):写入文件|导出文件
1.插入指令(i 行前写入)
格式:sed ‘定位符i 指定内容’ 文件
[root@localhost day04]# sed '2i ABC_XYZ' test.txt 在第二行的前面插入指定内容
2046 2048 2046 2046
ABC_XYZ
1001 2046 1999 1888
2046 2046 2046 2046
[root@localhost day04]# sed '3i ABC_XYZ' test.txt 在第三行的前面插入指定内容
2046 2048 2046 2046
1001 2046 1999 1888
ABC_XYZ
2046 2046 2046 2046
[root@localhost day04]# sed '/2046/i ABC\nXYZ' test.txt 在包含2046的行前面插入指定内容
ABC
XYZ
2046 2048 2046 2046
ABC
XYZ
1001 2046 1999 1888
ABC
XYZ
2046 2046 2046 2046
[root@localhost day04]# sed '/1888/i ABC\nXYZ' test.txt 在包含1888的行前面插入指定内容
2046 2048 2046 2046
ABC
XYZ
1001 2046 1999 1888
2046 2046 2046 2046
2.追加指令(a 行后插入)
格式:sed ‘定位符a 指定内容’ 文件
[root@localhost day04]# sed '2a ABC_XYZ' test.txt在第二行的后面插入指定内容
2046 2048 2046 2046
1001 2046 1999 1888
ABC_XYZ
2046 2046 2046 2046
[root@localhost day04]# sed '3a ABC_XYZ' test.txt在第三行的后面插入指定内容
2046 2048 2046 2046
1001 2046 1999 1888
2046 2046 2046 2046
ABC_XYZ
[root@localhost day04]# sed '/2046/a ABC\nXYZ' test.txt在包含2046的行后面插入指定内容
2046 2048 2046 2046
ABC
XYZ
1001 2046 1999 1888
ABC
XYZ
2046 2046 2046 2046
ABC
XYZ
[root@localhost day04]# sed '/1888/a ABC\nXYZ' test.txt在包含1888的行后面插入指定内容
2046 2048 2046 2046
1001 2046 1999 1888
ABC
XYZ
2046 2046 2046 2046
3.导入指令(r 将其他文件内容导入)
将/etc/hosts文件内容导入到test.txt文件的第二行的后面
[root@localhost day04]# sed '2r /etc/hosts' test.txt 2046 2048 2046 2046
1001 2046 1999 1888
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
2046 2046 2046 2046
将/etc/hosts文件内容导入到test.txt文件的每一行的后面(r 默认每一行)
[root@localhost day04]# sed 'r /etc/hosts' test.txt
2046 2048 2046 2046
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
1001 2046 1999 1888
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
2046 2046 2046 2046
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
将/etc/hosts文件内容导入到test.txt文件包含1888行的后面
[root@localhost day04]# sed '/1888/r /etc/hosts' test.txt
2046 2048 2046 2046
1001 2046 1999 1888
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
2046 2046 2046 2046
4.导出指令(w 将文件内容导出到其他文件中)
Sed ’w 新文件’ 当前文件
将test.txt所有内容(默认定位符匹配所有行)另存到一个新文件中
[root@localhost day04]# sed 'w copy_test.txt' test.txt
2046 2048 2046 2046
1001 2046 1999 1888
2046 2046 2046 2046
[root@localhost day04]# ls
copy_test.txt python.txt test.txt
[root@localhost day04]# cat copy_test.txt
2046 2048 2046 2046
1001 2046 1999 1888
2046 2046 2046 2046
将test.txt文件中包含1888的行内容另存到新文件中
[root@localhost day04]# sed '/1888/w 1888.txt' test.txt
2046 2048 2046 2046
1001 2046 1999 1888
2046 2046 2046 2046
[root@localhost day04]# cat 1888.txt
1001 2046 1999 1888
将test.txt文件中2到3的行内容另存到新文件中
[root@localhost day04]# sed '2,3w line.txt' test.txt
2046 2048 2046 2046
1001 2046 1999 1888
2046 2046 2046 2046
[root@localhost day04]# cat line.txt
1001 2046 1999 1888
2046 2046 2046 2046
练习:
[root@shell ~]#vim net.sh
# !/bin/bash
# 备份
cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak
read -p "请输入指定ip地址:" ip
# 替换dhcp 为 none
sed -i '/dhcp/s/dhcp/none/g' /etc/sysconfig/network-scripts/ifcfg-ens33
# 在文档最后添加5行
# IPADDR
sed -i '$aIPADDR='"$ip"'' /etc/sysconfig/network-scripts/ifcfg-ens33
# NETMAST
sed -i '$aNETMASK=255.255.255.0' /etc/sysconfig/network-scripts/ifcfg-ens33
# GATEWAY
sed -i '$aGATEWAY=10.1.1.2' /etc/sysconfig/network-scripts/ifcfg-ens33
# DNS1
sed -i '$aDNS1=8.8.8.8' /etc/sysconfig/network-scripts/ifcfg-ens33
# DNS2
sed -i '$aDNS2=114.114.114.114' /etc/sysconfig/network-scripts/ifcfg-ens33
# 修改uuid
# 修该主机名称
read -p "请输入主机名称" hn
hostnamectl set-hostname $hn
# 停用selinux
setenforce 0
sed -i '/SELINUX=enforcing/cSELINUX=disabled' /etc/selinux/config
# 停用防火墙
systemctl stop firewalld
systemctl disable firewalld
# 停用NetworkManage
systemctl stop NetworkManager
systemctl disable NetworkManager
# 重启网络服务
systemctl restart netword