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工程师进阶系列【原创干货持续更新中……】🚀 优质专栏:多媒…...
在rocky linux 9.5上在线安装 docker
前面是指南,后面是日志 sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo sudo dnf install docker-ce docker-ce-cli containerd.io -y docker version sudo systemctl start docker sudo systemctl status docker …...
【算法训练营Day07】字符串part1
文章目录 反转字符串反转字符串II替换数字 反转字符串 题目链接:344. 反转字符串 双指针法,两个指针的元素直接调转即可 class Solution {public void reverseString(char[] s) {int head 0;int end s.length - 1;while(head < end) {char temp …...

QT: `long long` 类型转换为 `QString` 2025.6.5
在 Qt 中,将 long long 类型转换为 QString 可以通过以下两种常用方法实现: 方法 1:使用 QString::number() 直接调用 QString 的静态方法 number(),将数值转换为字符串: long long value 1234567890123456789LL; …...

2025季度云服务器排行榜
在全球云服务器市场,各厂商的排名和地位并非一成不变,而是由其独特的优势、战略布局和市场适应性共同决定的。以下是根据2025年市场趋势,对主要云服务器厂商在排行榜中占据重要位置的原因和优势进行深度分析: 一、全球“三巨头”…...

PHP 8.5 即将发布:管道操作符、强力调试
前不久,PHP宣布了即将在 2025 年 11 月 20 日 正式发布的 PHP 8.5!作为 PHP 语言的又一次重要迭代,PHP 8.5 承诺带来一系列旨在提升代码可读性、健壮性以及开发者效率的改进。而更令人兴奋的是,借助强大的本地开发环境 ServBay&am…...
【FTP】ftp文件传输会丢包吗?批量几百个文件传输,有一些文件没有传输完整,如何解决?
FTP(File Transfer Protocol)本身是一个基于 TCP 的协议,理论上不会丢包。但 FTP 文件传输过程中仍可能出现文件不完整、丢失或损坏的情况,主要原因包括: ✅ 一、FTP传输可能“丢包”或文件不完整的原因 原因描述网络…...
DAY 26 函数专题1
函数定义与参数知识点回顾:1. 函数的定义2. 变量作用域:局部变量和全局变量3. 函数的参数类型:位置参数、默认参数、不定参数4. 传递参数的手段:关键词参数5 题目1:计算圆的面积 任务: 编写一…...
React从基础入门到高级实战:React 实战项目 - 项目五:微前端与模块化架构
React 实战项目:微前端与模块化架构 欢迎来到 React 开发教程专栏 的第 30 篇!在前 29 篇文章中,我们从 React 的基础概念逐步深入到高级技巧,涵盖了组件设计、状态管理、路由配置、性能优化和企业级应用等核心内容。这一次&…...

C++--string的模拟实现
一,引言 string的模拟实现是只对string对象中给的主要功能经行模拟实现,其目的是加强对string的底层了解,以便于在以后的学习或者工作中更加熟练的使用string。本文中的代码仅供参考并不唯一。 二,默认成员函数 string主要有三个成员变量,…...
2.2.2 ASPICE的需求分析
ASPICE的需求分析是汽车软件开发过程中至关重要的一环,它涉及到对需求进行详细分析、验证和确认,以确保软件产品能够满足客户和用户的需求。在ASPICE中,需求分析的关键步骤包括: 需求细化:将从需求收集阶段获得的高层需…...