shell编程-7
shell学习第7天
- sed的学习
- 1.sed是什么
- 2.sed有两个空间pattern hold
- 3.sed的语法
- 4. sed里单引号和双引号的区别:
- 5.sed的查找方式
- 6.sed的命令
- sed的标签用法
- sed的a命令:追加
- sed的i命令:根据行号插入
- sed的c命令:整行替换
- sed的r命令
- sed的s命令:替换
- sed的d命令:删除
- sed中的&符号
- 7.怎么替换有深度的文件
- 8. 练习(要是awk更好,就用awk)
- 9.小知识点
- 判断进程是否起来 pidof
- 正则中的 \w 和 \W \s和\b
- \1
- 输出文本的第一行和最后一行
- 编写一个一键更换ip地址,并检测ip地址是否合法的脚本。
- 一键更换ip升级版
sed的学习
1.sed是什么
sed - stream editor for filtering and transforming text
用于过滤和转换文本的Sed流编辑器 —>
文字流编辑器Sed is a stream editor. A stream editor is used to perform basic text transformations on
an input stream (a file or input from a pipeline).Sed是一个流编辑器。流编辑器用于在上执行基本的文本转换
输入流(来自管道的文件或输入)。
sed是脚本中修改文本或者文本替换的最佳工具
简单的例子:修改selinux的配置文件 把enforcing改成disabled
[root@gh-shell 1-22] cat /etc/selinux/config # This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted [root@gh-shell 1-22] sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
2.sed有两个空间pattern hold

-i选项直接对源文件进行修改
不接-i就是输出到屏幕 不改源文件
pattern space 输出之后就会清空里边的内容
3.sed的语法



执行多条语句,先打印1-10行,再打印20行,30行
[root@gh-shell 1-22] sed -n '1,10p;20p;30p' /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
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sc13:x:1012:1012::/home/sc13:/bin/bash
[root@gh-shell 1-22]#
cat passwd -n|sed -n ‘1 ~ 3p’:每隔三行输出一下
4. sed里单引号和双引号的区别:

[root@gh-shell 1-22] num1=10
[root@gh-shell 1-22] num2=20
[root@gh-shell 1-22] cat -n /etc/passwd|sed -n "${num1},${num2}p"10 operator:x:11:0:operator:/root:/sbin/nologin11 games:x:12:100:games:/usr/games:/sbin/nologin12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin13 nobody:x:99:99:Nobody:/:/sbin/nologin14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin15 dbus:x:81:81:System message bus:/:/sbin/nologin16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin17 tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin18 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin19 postfix:x:89:89::/var/spool/postfix:/sbin/nologin20 chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[root@gh-shell 1-22]#
5.sed的查找方式
-
根据行号
通过 -p [root@gh-shell 1-22] sed -n '1p;2p' passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin [root@gh-shell 1-22]# -
根据模式—>正则表达式=字符+特殊符号

[root@gh-shell 1-22] sed -n '/bash/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
sc1:x:1000:1000::/home/sc1:/bin/bash
sc5:x:1004:1004::/home/sc5:/bin/bash
sc6:x:1005:1005::/home/sc6:/bin/bash
sc7:x:1006:1006::/home/sc7:/bin/bash
sc8:x:1007:1007::/home/sc8:/bin/bash
sc9:x:1008:1008::/home/sc9:/bin/bash
。
。
。
[root@gh-shell 1-22]#
-n太好用了,只显示匹配处理的行
[root@gh-shell 1-22] sed -rn '/^#|^$/!p' /etc/grub2.cfg
'不打印出以#开头,还有空行的行'
//$/p 找出以/结尾的行,需要用到转义符号
[root@gh-shell 1-22] df -h|sed -n '/\/$/p'
/dev/mapper/centos-root 50G 2.1G 48G 5% /
[root@gh-shell 1-22]#
6.sed的命令
sed的标签用法

玩一下:
[root@gh-shell 1-22] tail -1 passwd
gaofei12:x:1032:1032::/home/gaofei12:/bin/bash
[root@gh-shell 1-22] sed -i -r 's/(^[0-Z]+)(.*)/\1/' passwd
[root@gh-shell 1-22] tail -1 passwd
gaofei12
[root@gh-shell 1-22]#
{=;p}先执行=号命令,作用输出行号,然后执行打印内容命命sed -E-n'/\b(\w+)\s+\1\b/{=;p}'two-cities-dup1.txt
sed的a命令:追加
[root@gh-shell shell] sed -i '2a gaodafei' user_pwd.txt #第二行的后面加个gaodafei
[root@gh-shell shell] head -5 user_pwd.txt
gf1 01a8589953
gf2 76088a94e6
gaodafei
gf3 e09f493732
gf4 d07af864bb
[root@gh-shell shell]#
[root@gh-shell shell] sed -i '/gf3/a gaodafei123' user_pwd.txt
[root@gh-shell shell] head -5 user_pwd.txt
gf1 01a8589953
gf2 76088a94e6
gaodafei
gf3 e09f493732
gaodafei123
[root@gh-shell shell]#
sed的i命令:根据行号插入
[root@gh-shell shell] sed -i '$i gaohui' user_pwd.txt #在匹配的那个前一行加入
[root@gh-shell shell] tail user_pwd.txt
gaodafei123
gf4 d07af864bb
gf5 57196a836a
gf6 2fce65efeb
gf7 2f41fdfa48
gf8 d610f5fcf3
gf9 e5003d62d1
gf10 7e8c0ffca3
gaohui
gaohui
[root@gh-shell shell]#
[root@gh-shell shell] sed -i '/li/i shenmj' user_pwd.txt 匹配的前一行加
[root@gh-shell shell] tail user_pwd.txt
gf5 57196a836a
gf6 2fce65efeb
gf7 2f41fdfa48
gf8 d610f5fcf3
gf9 e5003d62d1
gf10 7e8c0ffca3
gaohui
gaohui
shenmj
lidaliu
[root@gh-shell shell]#
sed的c命令:整行替换

sed的r命令
'读入操作'
sed '$r /etc/hosts' /etc/fstab
在fstab文件的末尾后面读入hosts文件的内容
sed的s命令:替换



[root@gh-shell 1-13] cat grade.txt
name chinese math english
cali 80 91 82 cali feng cali feng
tom 90 80 99
lucy 99 70 75
jack 60 89 99
[root@gh-shell 1-13] sed -i 's/cali/fengdeyong/' grade.txt #默认替换第一个
[root@gh-shell 1-13] cat grade.txt
name chinese math english
fengdeyong 80 91 82 cali feng cali feng
tom 90 80 99
lucy 99 70 75
jack 60 89 99
[root@gh-shell 1-13] sed -i 's/cali/fengdeyong/2' grade.txt #替换第二个
[root@gh-shell 1-13] cat grade.txt
name chinese math english
fengdeyong 80 91 82 cali feng fengdeyong feng
tom 90 80 99
lucy 99 70 75
jack 60 89 99
[root@gh-shell 1-13] sed -i 's/cali/fengdeyong/g' grade.txt #全局替换
[root@gh-shell 1-13] cat grade.txt
name chinese math english
fengdeyong 80 91 82 fengdeyong feng fengdeyong feng
tom 90 80 99
lucy 99 70 75
jack 60 89 99
[root@gh-shell 1-13]#
换分隔符
[root@gh-shell 1-13] sed -i 's#sbin/nologin#fengdeyong#' passwd
[root@gh-shell 1-13] tail passwd
liu1:x:1023:1023::/home/liu1:fengdeyong
liu2:x:1024:1024::/home/liu2:fengdeyong
liu3:x:1025:1025::/home/liu3:fengdeyong
gaodingjiang:x:1026:1026::/home/gaodingjiang:fengdeyong
gaodingjiang1:x:1027:1027::/home/gaodingjiang1:fengdeyong
gaodingjiang12:x:1028:1028::/home/gaodingjiang12:fengdeyong
gaodingjiang123:x:1029:1029::/home/gaodingjiang123:fengdeyong
gh123:x:1030:1030::/home/gh123:fengdeyong
gaofei123:x:1031:1031::/home/gaofei123:fengdeyong
gaofei12:x:1032:1032::/home/gaofei12:fengdeyong
[root@gh-shell 1-13]#
练习:
[root@gh-shell 1-13] sed '/^SELINUX=/ s/disabled/enforcing/' /etc/selinux/config # This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted [root@gh-shell 1-13]#
尝试删除grade.txt中lucy的行:
[root@gh-shell 1-13] sed -i 's/lucy//g' grade.txt
[root@gh-shell 1-13] cat grade.txt
name chinese math english
fengdeyong 80 91 82 fengdeyong feng fengdeyong feng
tom 90 80 9999 70 75
jack 60 89 99
[root@gh-shell 1-13]#
sed的d命令:删除

'一般不建议删,而是加个注释'
[root@gh-shell 1-13] sed -i '/^jack/ s/^/#/' grade.txt
[root@gh-shell 1-13] cat grade.txt
name chinese math english
fengdeyong 80 91 82 fengdeyong feng fengdeyong feng
tom 90 80 9999 70 75
#jack 60 89 99
[root@gh-shell 1-13]#
sed中的&符号
$代替sed在模式匹配里找到的内容
[root@gh-shell 1-24] vim cat.txt
[root@gh-shell 1-24] sed -i 's/.at/"&"/g' cat.txt
[root@gh-shell 1-24] cat cat.txt
i have a "fat" "cat"
i have a "fat" "cat"
i have a "fat" "cat"
i have a "fat" "cat"
i have a "fat" "cat"
[root@gh-shell 1-24]#
7.怎么替换有深度的文件
'先用find找,再用sed替换'
[root@gh-shell 1-22] mkdir aa/bb/cc -p
[root@gh-shell 1-22] cp ip.txt aa/
[root@gh-shell 1-22] cp ip.txt aa/bb/
[root@gh-shell 1-22] cp ip.txt aa/bb/cc/
[root@gh-shell 1-22] find . -name ip.txt
./ip.txt
./aa/bb/cc/ip.txt
./aa/bb/ip.txt
./aa/ip.txt
[root@gh-shell 1-22]#
写个脚本去改:
[root@gh-shell 1-22] cat sc_sed.sh
#!/bin/bashfor i in $(find /shell -name ip.txt)
doecho $ised -i 's/192.168.1.8/8.8.8.8/g' $iecho "#####################"cat $i
done
[root@gh-shell 1-22] bash sc_sed.sh
/shell/1-22/ip.txt
#####################
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
/shell/1-22/aa/bb/cc/ip.txt
#####################
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
/shell/1-22/aa/bb/ip.txt
#####################
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
/shell/1-22/aa/ip.txt
#####################
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
sanchaung 8.8.8.8 gao 8.8.8.8
[root@gh-shell 1-22]#
8. 练习(要是awk更好,就用awk)
19/Jan/2024:09:51:12 到 19/Jan/2024:10:41:48 日志,显示出来–>sed或者awk
'sed:'
sed -n '/19\/Jan\/2024:09:51:12/,/19\/Jan\/2024:10:41:48/p' access.log'awk:'
awk '/19\/Jan\/2024:09:51:12/,/19\/Jan\/2024:10:41:48/' access.log
例子:

复制/etc/hosts文件到当前目录下,然后进行操作在每行前面加一个字符串sanchuang
[root@gh-shell 1-22] sed -i 's/^/sanchuang/' hosts
[root@gh-shell 1-22] cat hosts
sanchuang127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
sanchuang::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@gh-shell 1-22]#
自己编辑一个文件test.txt,内容如下:
0.0.0.0
1.1.1.1
2.2.2.2
使用sed或者awk或者编写脚本(shell,python,go等)实现输出以下形式:
0.0.0.0:80,1.1.1.1:80,2.2.2.2:80
[root@gh-shell 1-22] sed -i.bak 'N;N;s/\n/:80,/g;s/$/:80/' test.txt
0.0.0.0:80,1.1.1.1:80,2.2.2.2:80[root@gh-shell 1-22] cat test.txt.bak|sed -n 's/$/:80/;H;${x;s/\n/,/2g;p}'
0.0.0.0:80,1.1.1.1:80,2.2.2.2:80
[root@gh-shell 1-22]# 'awk最简单'
[root@gh-shell 1-22] cat test.txt.bak.a |xargs |awk '{print $1":80,"$2":80,"$3":90"}'
0.0.0.0:80,1.1.1.1:80,2.2.2.2:90
[root@gh-shell 1-22]#
x Exchange the contents of the hold and pattern spaces.
h H Copy/append pattern space to hold space.
g G Copy/append hold space to pattern space.
n N Read/append the next line of input into the pattern space.

**sed -i.bak ‘N;N;s/\n/:80,/g;s/$/:80/’ test.txt **
这是一个使用
sed命令在文本文件中进行替换的命令。让我逐步解释这个命令:
sed: 流编辑器,用于处理和转换文本流。
-i.bak:-i表示直接在文件中进行修改(in-place),后面的.bak是一个备份文件的扩展名。这样在修改文件的同时会在同级目录下生成一个备份文件,以便于回滚。
'N;N;s/\n/:80,/g;s/$/:80/': 这是sed的编辑脚本,它包含了多个命令。
N;N: 连续两次使用N命令,将下一行添加到当前行的末尾,形成两行的模式空间。
s/\n/:80,/g: 将模式空间中的所有换行符\n替换为:80,。这样,每两行之间的换行符都被替换为:80,,形成了IP地址和端口号的组合。
s/$/:80/: 将模式空间中的行尾$替换为:80,给每一行的最后添加:80。总体来说,这个命令的作用是将每两行之间的换行符替换为
:80,,并在每行的末尾添加:80。最终结果是将test.txt文件中每两行IP地址间的换行符替换为:80,,并在每行的末尾添加:80。
cat test.txt.bak|sed -n 's/$/:80/;H;${x;s/\n/,/2g;p}'这是一个用于处理文本的 LInix 命令行管道,涉及到
sed和cat命令。让我逐步解释这个命令:
cat test.txt.bak:cat命令用于将文件的内容输出到标准输出。这里是将test.txt.bak文件的内容输出。
|:管道符号,将前一个命令的输出传递给下一个命令作为输入。
sed -n 's/$/:80/;H;${x;s/\n/,/2g;p}':sed是一种流编辑器,用于文本流的处理。
-n参数表示关闭默认输出,只输出经过编辑的行。s/$/:80/:正则表达式替换,将每一行的行尾$替换为:80。这样,每个IP地址后都会添加:80。H:将模式空间中的行追加到保持空间(hold space)的末尾。${x;s/\n/,/2g;p}:
${}:表示在最后一行执行的命令。 花括号里边是只对最后一行操作!x:交换模式空间和保持空间的内容。s/\n/,/2g:将保持空间中的所有换行符替换为逗号。这样,每个IP地址与端口号的组合之间的换行符都会被替换为逗号。p:打印最终结果。综合起来,这个命令的作用是将
test.txt.bak文件中的每个IP地址后面添加:80,然后将每个IP地址与端口号的组合之间的换行符替换为逗号,最终输出一行带有IP地址和端口号的字符串。
先复制/etc/passwd文件到当前目录下,然后对当前目录下的passwd进行操作
1.取出passwd文件的第一列
[root@gh-shell 1-22] cat passwd|awk -F: '{print $1}'
2.sed将PATH环境变量中的冒号换成 ->可以将PATH变量的内容重定向到一个文件里,例如path.txt
echo $PATH|sed
[root@gh-shell 1-22]# echo $PATH
/shell/1-13/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@gh-shell 1-22]# echo $PATH >>gh.txt
[root@gh-shell 1-22]# cat gh.txt
/shell/1-13/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@gh-shell 1-22]# sed -i 's/:/->/g' gh.txt
[root@gh-shell 1-22]# cat gh.txt
/shell/1-13/->/usr/local/sbin->/usr/local/bin->/usr/sbin->/usr/bin->/root/bin
[root@gh-shell 1-22]#
3.sed将PATH环境变量斜杠/换成斜杠\
[root@gh-shell 1-22] sed -i 's/\//\\/g' gh.txt
[root@gh-shell 1-22] cat gh.txt
\shell\1-13\:\usr\local\sbin:\usr\local\bin:\usr\sbin:\usr\bin:\root\bin
4.sed修改SELINUX配置文件从开启(enforcing)变成禁用(disabled)
/etc/sysconfig/selinux
sed '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config
5.去掉passwd文件中第二个字段的x
[root@gh-shell 1-22] cat passwd |awk -F: '{print $1":"$3":"$4":"$5":"$6":"$7}'
[root@gh-shell 1-22] sed -i -r '/([0-Z]+)(:X:)(.*)/\1:\3' passwd
6.只显示ip add的ip地址
[root@gh-shell 1-22] ip add|awk -F"[/ ]+" '/ens33$/{print $3}'
192.168.153.166
[root@gh-shell 1-22]#
7.复制/etc/ssh/sshd config到当前目录下,修改里面的端口号修改为8899
将#Port 22 配置修改为Port 8899 要求去掉前面的#号,将22修改为8899
[root@gh-shell 1-22] sed -i '/^#Port/c Port 8899' ssh_config
9.小知识点
判断进程是否起来 pidof
[root@gh-shell 1-22] pidof nginx
[root@gh-shell 1-22] echo $?
1
[root@gh-shell 1-22] pidof sshd
2740 2719 2133 2109 1109 #获得正在运行的进程号
[root@gh-shell 1-22]#
或者直接统计有多少行,一行就是没有启动
[root@gh-shell 1-22] ps -ef|grep nginx
root 3412 2744 0 15:52 pts/3 00:00:00 grep --color=auto nginx
[root@gh-shell 1-22] ps -ef|grep nginx|wc -l
1
[root@gh-shell 1-22] ps -ef|grep sshd
root 1109 1 0 12:28 ? 00:00:00 /usr/sbin/sshd -D
root 2109 1109 0 12:31 ? 00:00:00 sshd: root@pts/0
root 2133 1109 0 12:31 ? 00:00:00 sshd: root@pts/1
root 2719 1109 0 14:52 ? 00:00:00 sshd: root@pts/2
root 2740 1109 0 14:52 ? 00:00:00 sshd: root@pts/3
root 3426 2744 0 15:52 pts/3 00:00:00 grep --color=auto sshd
[root@gh-shell 1-22] ps -ef|grep sshd|wc -l
6
[root@gh-shell 1-22]#
正则中的 \w 和 \W \s和\b
- \w表示字母,数组,下划线
- \W表示特殊符号
- \s表示匹配的空白
- \b。。。\b表示单词界定
\1
[root@halou-gf lianxi]echo aaafdfd bbb ccc |sed -nr 's/([a-z]+) ([a+z]+) ([a-z]+)/\3 \2 \1/p'
在 sed 中,
\3,\2,\1属于后向引用,用于引用正则表达式中括号分组的匹配结果。在你提供的示例中,正则表达式
([a-z]+) ([a+z]+) ([a-z]+)匹配了三个部分,每个部分都用括号分组起来了。其中:
([a-z]+): 第一个括号分组,匹配一个或多个小写字母。([a+z]+): 第二个括号分组,匹配一个或多个小写字母和加号+。([a-z]+): 第三个括号分组,匹配一个或多个小写字母。而
\3,\2,\1分别对应这三个括号分组的匹配结果。通过将
\3 \2 \1作为替换字符串,sed 命令会将匹配到的文本进行替换。具体来说,\3将会被匹配到的第三个括号分组的内容替换,\2将会被匹配到的第二个括号分组的内容替换,\1将会被匹配到的第一个括号分组的内容替换。所以,在你的示例中,sed 命令会将匹配到的字符串
aaafdfd bbb ccc进行替换,将括号分组的内容按照\3 \2 \1的顺序重新排列。最终输出结果为ccc bbb aaafdfd。
输出文本的第一行和最后一行
'方式1:'
[root@gh-shell 1-22] head -1 passwd;tail -1 passwd
root:x:0:0:root:/root:/bin/bash
gaofei12:x:1032:1032::/home/gaofei12:/bin/bash'方式2:'
[root@gh-shell 1-22] sed -rn '1p;$p' passwd
root:x:0:0:root:/root:/bin/bash
gaofei12:x:1032:1032::/home/gaofei12:/bin/bash
编写一个一键更换ip地址,并检测ip地址是否合法的脚本。
[root@gh-shell 1-22] cat modify_ip.sh
#!/bin/bash#接受第一个位置变量
new_ip=$1
# 检测IP地址是否合法
ip_regex='^((1[0-9][0-9]|2[0-4][0-9]|25[0-5]\.)([01][0-9]|2[0-4]|25[0-5]\.){2}([01][0-9]|2[0-4]|25[0-5]))$'
if ! [[ $new_ip =~ $ip_regex ]]; thenecho "无效的IP地址"exit 1
fi
#备份
mkdir /backup_network -p
cp /etc/sysconfig/network-scripts/ifcfg-ens33 /backup_network
#修改ip地址为第一个位置变量的内容
sed -i "/IPADDR/c IPADDR=$new_ip/" /etc/sysconfig/network-scripts/ifcfg-ens33
#重启网络服务
service network restart
#获取ip地址
sc_ip=$(ip add|awk '/inet.*ens33$/{print $2}')
#通过ip add命令去查看本机的ip地址,然后截取出来统计ip内容的长度,如果长度大于1就表示有ip,如果长度为小于1就表示没有ip
if ((${#sc_ip}>1));thenecho "modify ip successfully"
else#回滚cp /backup_network/ifcfg-ens33 /etc/sysconfig/network-scripts/ -fservice network restart
fi[root@gh-shell 1-22]#
传个错误的ip会失败
正确的会成功
一键更换ip升级版
编写一个修改ip地址的脚本
提醒用户输入ip、子网掩码、网关、dns服务器地址
直接修改网卡配置文件,然后刷新服务让输入的ip地址信息生效
需要检查输入ip地址是否合法,不能输入空的内容或者字母,如果ip地址不合法或者输入空内容、字母都给予提醒
提醒:空的内容包括回车和空格,可以是多个空格 在我刚才脚本的基础上改
#!/bin/bash# 提醒用户输入IP地址
read -p "请输入新的IP地址: " new_ip# 检查IP地址是否为空或包含非数字字符
if [[ -z "$new_ip" || ! "$new_ip" =~ ^[0-9.]+$ ]]; thenecho "无效的IP地址"exit 1
fi# 提醒用户输入子网掩码
read -p "请输入子网掩码: " subnet_mask# 检查子网掩码是否为空或包含非数字字符
if [[ -z "$subnet_mask" || ! "$subnet_mask" =~ ^[0-9.]+$ ]]; thenecho "无效的子网掩码"exit 1
fi# 提醒用户输入网关
read -p "请输入网关地址: " gateway# 检查网关地址是否为空或包含非数字字符
if [[ -z "$gateway" || ! "$gateway" =~ ^[0-9.]+$ ]]; thenecho "无效的网关地址"exit 1
fi# 提醒用户输入DNS服务器地址
read -p "请输入DNS服务器地址: " dns_server# 检查DNS服务器地址是否为空或包含非数字字符
if [[ -z "$dns_server" || ! "$dns_server" =~ ^[0-9.]+$ ]]; thenecho "无效的DNS服务器地址"exit 1
fi# 备份网络配置
mkdir -p /backup_network
cp /etc/sysconfig/network-scripts/ifcfg-ens33 /backup_network# 修改网卡配置文件
sed -i "/IPADDR/c IPADDR=$new_ip/" /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "/NETMASK/c NETMASK=$subnet_mask/" /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "/GATEWAY/c GATEWAY=$gateway/" /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "/DNS1/c DNS1=$dns_server/" /etc/sysconfig/network-scripts/ifcfg-ens33# 重启网络服务
service network restart# 获取修改后的IP地址
sc_ip=$(ip add | awk '/inet.*ens33$/{print $2}')# 检查是否成功修改IP地址
if [ ${#sc_ip} -gt 1 ]; thenecho "成功修改IP地址"
else# 回滚cp -f /backup_network/ifcfg-ens33 /etc/sysconfig/network-scripts/service network restartecho "无法修改IP地址。回滚操作。"
fi相关文章:
shell编程-7
shell学习第7天 sed的学习1.sed是什么2.sed有两个空间pattern hold3.sed的语法4. sed里单引号和双引号的区别:5.sed的查找方式6.sed的命令sed的标签用法sed的a命令:追加sed的i命令:根据行号插入sed的c命令:整行替换sed的r命令sed的s命令:替换sed的d命令:删除sed中的&符号 7…...
工业智能网关储能物联网应用实现能源的高效利用及远程管理
储能电力物联网是指利用物联网技术和储能技术相结合,实现对电力系统中各种储能设备的智能管理和优化控制。随着可再生能源的不断发展和应用,电力系统面临着越来越大的电力调度和储能需求而储能电力物联网的出现可以有效解决这一问题,提高电力…...
虹科数字化与AR部门升级为安宝特AR子公司
致关心虹科AR的朋友们: 感谢您一直以来对虹科数字化与AR的支持和信任,为了更好地满足市场需求和公司发展的需要,虹科数字化与AR部门现已升级为虹科旗下独立子公司,并正式更名为“安宝特AR”。 ”虹科数字化与AR“自成立以来&…...
服务器是什么?(四种服务器类型)
服务器 服务器定义广义: 专门给其他机器提供服务的计算机。狭义:一台高性能的计算机,通过网络提供外部计算机一些业务服务 个人PC内存大概8G,服务器内存128G起步 服务器是什么 服务器指的是 网络中能对其他机器提供某些服务的计算机系统 ,相对…...
09-微服务Sentinel整合GateWay
一、概述 在微服务系统中,网关提供了微服务系统的统一入口,所以我们在做限流的时候,肯定是要在网关层面做一个流量的控制,Sentinel 支持对 Spring Cloud Gateway、Zuul 等主流的 API Gateway 进行限流。 1.1 总览 Sentinel 1.6.…...
python基础学习-03 安装
python3 可应用于多平台包括 Windows、Linux 和 Mac OS X。 Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX, 等等。)Win 9x/NT/2000Macintosh (Intel, PPC, 68K)OS/2DOS (多个DOS版本)PalmOSNokia 移动手机Windows CEAcorn/RISC OSBeOSAmigaVMS/OpenVMSQNXVxWorksP…...
HTML — 区块元素
HTML 通过各种标签将元素组合起来。 一. 区块元素 大多数 HTML 元素被定义为块级元素或内联元素。块级元素在浏览器显示时,通常会以新的行开始。例如:<div>、<h1>、<p>、<ul>等。 它们在使用时会独自占据一行,称为块…...
《PCI Express体系结构导读》随记 —— 第I篇 第3章 PCI总线的数据交换(4)
接前一篇文章:《PCI Express体系结构导读》随记 —— 第I篇 第3章 PCI总线的数据交换(3) 3.2 PCI设备的数据传递 PCI设备的数据传递使用地址译码方式,当一个存储器读写总线事务到达PCI总线时,在这条总线上的所有PCI设…...
力扣0083——删除排序链表中的重复元素
删除排序链表中的重复元素 难度:简单 题目描述 给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。 示例1 输入:head [1,1,2] 输出:[1,2]示例2 输入:…...
MySQL数据库的一些缩写含义
DDL Data Definition Language,数据定义语言,用来定义数据库对象(数据库,表,字段) DML DML英文全称是Data Manipulation Language(数据操作语言),用来对数据库中表的数据记录进 行增、删、改操作。 添加数据&#x…...
解决 ssh: connect to host github.com port 22: Connection timed out
问题 今天使用git克隆github上的代码时,一直报错 原以为是公钥过期了,就尝试修改配置公钥,但是尝试了几次都不行,最终在博客上找到了解决方案,在次记录一下,以备不时之需 解决ssh-connect-to-host-github…...
【iOS ARKit】同时开启前后摄像头BlendShapes
在上一节中已经了解了 iOS ARkit 进行BlendShapes的基本操作,这一小节继续实践同时开启前后摄像头进行人脸捕捉和世界追踪。 iOS设备配备了前后两个摄像头,在运行AR 应用时,需要选择使用哪个摄像头作为图像输人。最常见的AR 体验使用设备后置…...
Vue3动态插入组件
一、使用<component>is实现动态组件插入 <component>:一个用于渲染动态组件或元素的“元组件”。 :is : 要渲染的实际组件,当 is 是字符串,它既可以是 HTML 标签名也可以是组件的注册名。 <script> import Foo from ./F…...
介绍一下OpenCV中常用的图像处理函数
OpenCV中常用的图像处理函数有很多,以下是其中一些函数的介绍: - cvLoadImage():读入图像函数。 - imshow():显示图像函数。 - imwrite():保存图像函数。 - Mat srcImage imread():读入图像函数。 - …...
vscode vim 快捷键汇总
需满足操作: 上下移动按照 word 移动选中增删改查找字符/变量移动、增加、复制、删除 行选中多个相同的变量/字符屏幕移动增加多个光标快速注释 上下左右移动 CommandDescription🔢 hleft (also: CTRL-H, BS, or Left key)🔢 lright (also…...
npm官方注册表和淘宝镜像切换
1.切换到淘宝镜像 加快npm包的下载速度, //已失效 //npm config set registry https://registry.npm.taobao.org/ npm config set registry https://registry.npmmirror.com这会将npm的注册表设置为淘宝镜像 查看: npm config get registry如果返回的…...
LFU算法
LFU算法 Least Frequently Used(最不频繁使用) Leetcode有原题,之前手写过LRU,数据结构还是习惯于用java实现,实现是copy的评论题解。 题解注释写的很清楚 大致就是说LFUCache类维护一个存放node的map,同…...
JVM系列-7内存调优
👏作者简介:大家好,我是爱吃芝士的土豆倪,24届校招生Java选手,很高兴认识大家📕系列专栏:Spring原理、JUC原理、Kafka原理、分布式技术原理、数据库技术、JVM原理🔥如果感觉博主的文…...
[UI5 常用控件] 01.Text
文章目录 前言1. 普通文本2. 长文本:3. 设置最大显示行数 ( maxLines3 )4. 单行显示 ( wrappingfalse )5. 显示空白符 ( renderWhitespacetrue )6. 使用 - 连接单词:只适用于英文 ( wrappingTypeHyphenated )7. 空白时使用 - 代替 ( emptyIndicatorModeOn )8. JSON数…...
C语言之指针的地址和指向的内容总结(八十四)
简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长! 优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀 优质专栏:多媒…...
变量 varablie 声明- Rust 变量 let mut 声明与 C/C++ 变量声明对比分析
一、变量声明设计:let 与 mut 的哲学解析 Rust 采用 let 声明变量并通过 mut 显式标记可变性,这种设计体现了语言的核心哲学。以下是深度解析: 1.1 设计理念剖析 安全优先原则:默认不可变强制开发者明确声明意图 let x 5; …...
QMC5883L的驱动
简介 本篇文章的代码已经上传到了github上面,开源代码 作为一个电子罗盘模块,我们可以通过I2C从中获取偏航角yaw,相对于六轴陀螺仪的yaw,qmc5883l几乎不会零飘并且成本较低。 参考资料 QMC5883L磁场传感器驱动 QMC5883L磁力计…...
什么是库存周转?如何用进销存系统提高库存周转率?
你可能听说过这样一句话: “利润不是赚出来的,是管出来的。” 尤其是在制造业、批发零售、电商这类“货堆成山”的行业,很多企业看着销售不错,账上却没钱、利润也不见了,一翻库存才发现: 一堆卖不动的旧货…...
css3笔记 (1) 自用
outline: none 用于移除元素获得焦点时默认的轮廓线 broder:0 用于移除边框 font-size:0 用于设置字体不显示 list-style: none 消除<li> 标签默认样式 margin: xx auto 版心居中 width:100% 通栏 vertical-align 作用于行内元素 / 表格单元格ÿ…...
七、数据库的完整性
七、数据库的完整性 主要内容 7.1 数据库的完整性概述 7.2 实体完整性 7.3 参照完整性 7.4 用户定义的完整性 7.5 触发器 7.6 SQL Server中数据库完整性的实现 7.7 小结 7.1 数据库的完整性概述 数据库完整性的含义 正确性 指数据的合法性 有效性 指数据是否属于所定…...
在Mathematica中实现Newton-Raphson迭代的收敛时间算法(一般三次多项式)
考察一般的三次多项式,以r为参数: p[z_, r_] : z^3 (r - 1) z - r; roots[r_] : z /. Solve[p[z, r] 0, z]; 此多项式的根为: 尽管看起来这个多项式是特殊的,其实一般的三次多项式都是可以通过线性变换化为这个形式…...
群晖NAS如何在虚拟机创建飞牛NAS
套件中心下载安装Virtual Machine Manager 创建虚拟机 配置虚拟机 飞牛官网下载 https://iso.liveupdate.fnnas.com/x86_64/trim/fnos-0.9.2-863.iso 群晖NAS如何在虚拟机创建飞牛NAS - 个人信息分享...
Vue 3 + WebSocket 实战:公司通知实时推送功能详解
📢 Vue 3 WebSocket 实战:公司通知实时推送功能详解 📌 收藏 点赞 关注,项目中要用到推送功能时就不怕找不到了! 实时通知是企业系统中常见的功能,比如:管理员发布通知后,所有用户…...
React核心概念:State是什么?如何用useState管理组件自己的数据?
系列回顾: 在上一篇《React入门第一步》中,我们已经成功创建并运行了第一个React项目。我们学会了用Vite初始化项目,并修改了App.jsx组件,让页面显示出我们想要的文字。但是,那个页面是“死”的,它只是静态…...
从实验室到产业:IndexTTS 在六大核心场景的落地实践
一、内容创作:重构数字内容生产范式 在短视频创作领域,IndexTTS 的语音克隆技术彻底改变了配音流程。B 站 UP 主通过 5 秒参考音频即可克隆出郭老师音色,生成的 “各位吴彦祖们大家好” 语音相似度达 97%,单条视频播放量突破百万…...
