Linux108 shell:.bashrc 正则表达式:. * .* ^ $ ^$ [ ] [^] ^[] ^[^ ] \< \>
[code@samba ~]$ cat jump.sh
#!/bin/bash
menu1()
{
cat<<-EOF
欢迎使用jump-server,请选择主机
1.DBI-Master
2.DB2-Slave
3.Web1
4.Web2
h.help
q.exitEOF
}
menu2()
{
cat<<-EOF
欢迎使用Web-server,请选择操作系统
1.清理日志
2.启动Apache
3.重启Apache
q.退出
h.helpEOF
}
while true
do
menu1
read -p "pl choose the host:" host
case $host in1)ssh root@192.168.235.3;;2)ssh root@192.168.235.100;;3)clearmenu2read -p "pl input actions:" actionscase $actions in1)ssh root@192.168.235.3 echo "日志正在清理中";;2)ssh root@192.168.235.3 service apache start;;3)ssh root@192.168.235.3 service apache stop;;h)clearmenu2;;*)echo "pl choose the operation ";;esac;;h)clearmenu1;;q)exit;;
esac
done
source 函数是什么意思
[code@samba ~]$ cat fun1.sh
#!/bin/bash
fun1()
{
echo hello world
hostname
}function fun2()
{A=helloif [ -z "$A" ];thenecho "变量为空"elseecho $Afi
}
[code@samba ~]$ bash -x fun1.sh
[code@samba ~]$ chmod +x fun1.sh
[code@samba ~]$ ./fun1.sh
[code@samba ~]$ fun1
bash: fun1: 未找到命令...
[code@samba ~]$ source fun1.sh
[code@samba ~]$ fun1
hello world
samba.web.cn
执行前:当前Shell环境
┌──────────────┐
│ Shell会话 │
└──────────────┘
source执行时:直接注入
┌──────────────┐
│ Shell会话 │←───(注入fun1/fun2函数定义)
│ + fun1() │
│ + fun2() │
└──────────────┘
执行后:函数常驻内存
┌──────────────┐
│ Shell会话 │
│ fun1() {…} │ ← 可随时调用
│ fun2() {…} │
└──────────────┘
[root@66 ~]# grep '[0-9]\{1,3\}'.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\} 1.txt
[root@66 ~]# cat 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogletaobao.com
hello world
helloworld
hhhh world
123213123
21312e 12312 31
23123172./g.2
1./g.21
172./g.12193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# cat 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogletaobao.com
hello world
helloworld
hhhh world
123213123
21312e 12312 31
23123172./g.2
1./g.21
172./g.12193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}}' 1.txt
[root@66 ~]#
[root@66 ~]# grep -E '([0-9]{1,3}\.){3} [0-9]{1,3}' 1.txt
[root@66 ~]# grep -E '([0-9]{1,3}\.){3} [0-9]{1,3}' 1.txt
[root@66 ~]#
shell
.bashrc
bash jump.sh
┌──────────────────────────────────────────────────────────────────────┐│ • MobaXterm Personal Edition v23.2 • ││ (SSH client, X server and network tools) ││ ││ ⮞ SSH session to code@192.168.235.10 ││ • Direct SSH : ✓ ││ • SSH compression : ✓ ││ • SSH-browser : ✓ ││ • X11-forwarding : ✓ (remote display is forwarded through SSH) ││ ││ ⮞ For more info, ctrl+click on help or visit our website. │└──────────────────────────────────────────────────────────────────────┘Last login: Wed Oct 8 14:35:26 2025 from 192.168.235.1
欢迎使用jump-server,请选择主机
1.DBI-Master
2.DB2-Slave
3.Web1
4.Web2
h.help
q.exit
pl choose the host:2
Last login: Wed Oct 8 15:17:03 2025 from 192.168.235.10
[root@backup ~]# exit
登出
Connection to 192.168.235.100 closed.
欢迎使用jump-server,请选择主机
1.DBI-Master
2.DB2-Slave
3.Web1
4.Web2
h.help
q.exit
pl choose the host:4
欢迎使用jump-server,请选择主机
1.DBI-Master
2.DB2-Slave
3.Web1
4.Web2
h.help
q.exit
pl choose the host:
正则表达式:第一类正则
* grep ‘go*’ 1.txt 前导字符0次或连续多次
[root@66 ~]# grep 'go*' 1.txt
ggle
gogle
google
gooooogle
gooogle
. greo ‘go.’ 1.txt 任意单字符
[root@66 ~]# grep 'go.' 1.txt
gogle
google
gooooogle
gooogle
.* 任意长字符
[root@66 ~]# grep 'goo.*' 1.txt
google
gooooogle
gooogle
^$ 空行 grep -n ‘^$’ 1.txt
[root@66 ~]# grep -n '^$' 1.txt
[root@66 ~]# vim 1.txt
[root@66 ~]# cat 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogletaobao.com[root@66 ~]# grep -n '^$' 1.txt
11:
12:
13:
15:
^ 行开头
[root@66 ~]# grep '^goo*' 1.txt
gogle
google
gooooogle
gooogle
$ 行结尾 grep ‘.*com$’
[root@66 ~]# grep '.*com$' 1.txt
taobao.com
taobao.com
taobaot.com
a.com
ab.com
taobao.com
[ ] [ abc ] 组内任意单个字符
[root@66 ~]# grep '[abc]' 1.txt
taobao.com
taobao.com
taobaot.com
a.com
ab.com
taobao.com
[ ^] 不在字符组内任一字符
[root@66 ~]# grep '[^abc]' 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogle
taobao.com
1 任一字符开头
[root@66 ~]# grep '^[abc]' 1.txt
a.com
ab.com
^ [^]不以任一字符开头
[root@66 ~]# grep '^[^abc]' 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
gooogle
taobao.com
[root@66 ~]# grep '[0-9].' 1.txt
< 取单词头 grep ‘<hello’
[root@66 ~]# grep '\<hello' 1.txt
hello world
helloworld
> grep ‘w>’ 1.txt 取单词尾
[root@66 ~]# grep 'w\>' 1.txt
[root@66 ~]# grep 'hello\>' 1.txt
hello world
<> 精确匹配 grep ‘<hello>’
[root@66 ~]# grep '\<hello\>' 1.txt
hello world
{n} 前导字符至少出现n次
[root@66 ~]# grep 'go\{2\}' 1.txt
google
gooooogle
gooogle
{n,m} 前导字符n-m次
[root@66 ~]# grep 'go\{2,3\}' 1.txt
google
gooooogle
gooogle
{} 保持被匹配字符
普通正则
[root@66 ~]# grep '[0-9]' 1.txt
123213123
21312e 12312 31
23123
172./g.2
1./g.21
172./g.12
193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep '[a-zA-Z0-9.]' 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogle
taobao.com
hello world
helloworld
hhhh world
123213123
21312e 12312 31
23123
172./g.2
1./g.21
172./g.12
193.168.235.2
193.168.235.23
193.168.235.123
扩展正则 \d 匹配数字 grep -P ‘\d’
[root@66 ~]# grep '\d' 1.txt
hello world
helloworld
hhhh world
[root@66 ~]# grep -P '\d' 1.txt
123213123
21312e 12312 31
23123
172./g.2
1./g.21
172./g.12
193.168.235.2
193.168.235.23
193.168.235.123
\w 字母数字下划线
[root@66 ~]# grep -P '\w' 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogle
taobao.com
hello world
helloworld
hhhh world
123213123
21312e 12312 31
23123
172./g.2
1./g.21
172./g.12
193.168.235.2
193.168.235.23
193.168.235.123
扩展正则
+ 一次或多次
[root@66 ~]# grep 'go+' 1.txt
[root@66 ~]# grep -E 'go?' 1.txt
[root@66 ~]# grep 'tao|j' 1.txt
[root@66 ~]# grep -E '(taobao|j).com'
^C^C
[root@66 ~]# grep -E '(taobao|j).com' 1.txt
taobao.com
taobao.com
taobao.com
? 0次或多次 a|b
() 组字符
正则匹配IP地址
[root@66 ~]# grep -E 'go{2}' 1.txt
google
gooooogle
gooogle
[root@66 ~]# grep '[0-9]\{1,3\}'\.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]{1,3\}' 1.txt
> ^C
[root@66 ~]# grep '[0-9]\{1,3\}'.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\} 1.txt
[root@66 ~]# cat 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogletaobao.com
hello world
helloworld
hhhh world
123213123
21312e 12312 31
23123172./g.2
1./g.21
172./g.12193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}}' 1.txt
[root@66 ~]# grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' 1.txt
193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep -P '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' 1.txt
193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep -E '([0-9]{1,3}\.){3}' [0-9]{1,3}' 1.txt
> ^C
[root@66 ~]# grep -E '([0-9]{1,3}\.){3} [0-9]{1,3}' 1.txt
[root@66 ~]# grep -E '([0-9]{1,3}\.){3} [0-9]{1,3}' 1.txt
[root@66 ~]# grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' 1.txt
193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep
记录
Last login: Tue Oct 7 18:57:15 2025 from 192.168.235.1
[code@samba ~]$ vim ~/.bashrc
[code@samba ~]$ cat jump.sh
#!/bin/bash
menu1()
{
cat<<-EOF
欢迎使用jump-server,请选择主机
1.DBI-Master
2.DB2-Slave
3.Web1
4.Web2
h.help
q.exitEOF
}
menu2()
{
cat<<-EOF
欢迎使用Web-server,请选择操作系统
1.清理日志
2.启动Apache
3.重启Apache
q.退出
h.helpEOF
}
while true
do
menu1
read -p "pl choose the host:" host
case $host in1)ssh root@192.168.235.3;;2)ssh root@192.168.235.100;;3)clearmenu2read -p "pl input actions:" actionscase $actions in1)ssh root@192.168.235.3 echo "日志正在清理中";;2)ssh root@192.168.235.3 service apache start;;3)ssh root@192.168.235.3 service apache stop;;h)clearmenu2;;*)echo "pl choose the operation ";;esac;;h)clearmenu1;;q)exit;;
esac
done
[code@samba ~]$ vim ~/.bashrc
[code@samba ~]$ vim .bashrc
[code@samba ~]$ cat .bashrc
# .bashrc# Source global definitions
if [ -f /etc/bashrc ]; then. /etc/bashrc
fi# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=# User specific aliases and functions
bash jumper.sh
[code@samba ~]$ vim .bashrc
[code@samba ~]$ cat .bashrc
# .bashrc# Source global definitions
if [ -f /etc/bashrc ]; then. /etc/bashrc
fi# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=# User specific aliases and functions
bash jump.sh
[code@samba ~]$ jump.sh
bash: jump.sh: 未找到命令...
[code@samba ~]$ cat jump.sh
#!/bin/bash
menu1()
{
cat<<-EOF
欢迎使用jump-server,请选择主机
1.DBI-Master
2.DB2-Slave
3.Web1
4.Web2
h.help
q.exitEOF
}
menu2()
{
cat<<-EOF
欢迎使用Web-server,请选择操作系统
1.清理日志
2.启动Apache
3.重启Apache
q.退出
h.helpEOF
}
while true
do
menu1
read -p "pl choose the host:" host
case $host in1)ssh root@192.168.235.3;;2)ssh root@192.168.235.100;;3)clearmenu2read -p "pl input actions:" actionscase $actions in1)ssh root@192.168.235.3 echo "日志正在清理中";;2)ssh root@192.168.235.3 service apache start;;3)ssh root@192.168.235.3 service apache stop;;h)clearmenu2;;*)echo "pl choose the operation ";;esac;;h)clearmenu1;;q)exit;;
esac
done
[code@samba ~]$ ./jump.sh
欢迎使用jump-server,请选择主机
1.DBI-Master
2.DB2-Slave
3.Web1
4.Web2
h.help
q.exit
pl choose the host:2
Last login: Tue Oct 7 19:05:53 2025 from 192.168.235.1
[root@backup ~]#
┌──────────────────────────────────────────────────────────────────────┐│ • MobaXterm Personal Edition v23.2 • ││ (SSH client, X server and network tools) ││ ││ ⮞ SSH session to code@192.168.235.10 ││ • Direct SSH : ✓ ││ • SSH compression : ✓ ││ • SSH-browser : ✓ ││ • X11-forwarding : ✓ (remote display is forwarded through SSH) ││ ││ ⮞ For more info, ctrl+click on help or visit our website. │└──────────────────────────────────────────────────────────────────────┘Last login: Wed Oct 8 14:35:26 2025 from 192.168.235.1
欢迎使用jump-server,请选择主机
1.DBI-Master
2.DB2-Slave
3.Web1
4.Web2
h.help
q.exit
pl choose the host:2
Last login: Wed Oct 8 15:17:03 2025 from 192.168.235.10
[root@backup ~]# exit
登出
Connection to 192.168.235.100 closed.
欢迎使用jump-server,请选择主机
1.DBI-Master
2.DB2-Slave
3.Web1
4.Web2
h.help
q.exit
pl choose the host:4
欢迎使用jump-server,请选择主机
1.DBI-Master
2.DB2-Slave
3.Web1
4.Web2
h.help
q.exit
pl choose the host:
login as: root
root@192.168.235.133's password:┌────────────────────────────────────────────────────────────────────┐│ • MobaXterm 20.0 • ││ (SSH client, X-server and networking tools) ││ ││ ➤ SSH session to root@192.168.235.133 ││ • SSH compression : ✘ ││ • SSH-browser : ✔ ││ • X11-forwarding : ✔ (remote display is forwarded through SSH) ││ • DISPLAY : ✔ (automatically set on remote server) ││ ││ ➤ For more info, ctrl+click on help or visit our website │└────────────────────────────────────────────────────────────────────┘Activate the web console with: systemctl enable --now cockpit.socketThis system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --registerLast login: Sun Sep 28 21:29:18 2025 from 192.168.235.1
[root@66 ~]# cat 1.txt
cat: 1.txt: 没有那个文件或目录
[root@66 ~]# ls 1.txt
ls: 无法访问'1.txt': 没有那个文件或目录
[root@66 ~]# ls
aaa expect2.sh id_rsa.pub pub2.sh scp.sh user1 useradd.sh
anaconda-ks.cfg file1 initial-setup-ks.cfg rsa.sh sshpass-1.06 user2
expect1.sh file3 ip.txt scp1.sh sshpass-1.06.tar.gz useradd2.sh
[root@66 ~]# vim 1.txt
[root@66 ~]# cat 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogle
[root@66 ~]# grep 'go*' |txt
bash: txt: 未找到命令...
^C
[root@66 ~]# grep 'go*' 1.txt
ggle
gogle
google
gooooogle
gooogle
[root@66 ~]# grep 'go.' 1.txt
gogle
google
gooooogle
gooogle
[root@66 ~]# grep 'goo.*' 1.txt
google
gooooogle
gooogle
[root@66 ~]# grep -n '^$' 1.txt
[root@66 ~]# vim 1.txt
[root@66 ~]# cat 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogletaobao.com[root@66 ~]# grep -n '^$' 1.txt
11:
12:
13:
15:
[root@66 ~]# grep '^goo*' 1.txt
gogle
google
gooooogle
gooogle
[root@66 ~]# grep '.*com$' 1.txt
taobao.com
taobao.com
taobaot.com
a.com
ab.com
taobao.com
[root@66 ~]# grep '[abc]' 1.txt
taobao.com
taobao.com
taobaot.com
a.com
ab.com
taobao.com
[root@66 ~]# grep '[^abc]' 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogle
taobao.com
[root@66 ~]# grep '^[abc]' 1.txt
a.com
ab.com
[root@66 ~]# grep '^[^abc]' 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
gooogle
taobao.com
[root@66 ~]# grep '[0-9].' 1.txt
[root@66 ~]# grep -w hello 1.txt
[root@66 ~]# grep '\<hello' 1.txt
[root@66 ~]# grep 'w\>' 1.txt
[root@66 ~]# vim 1.txt
[root@66 ~]# vim 1.txt
[root@66 ~]# grep -w hello 1.txt
hello world
[root@66 ~]# grep '\<hello' 1.txt
hello world
helloworld
[root@66 ~]# grep 'w\>' 1.txt
[root@66 ~]# grep '\<hello\>' 1.txt
hello world
[root@66 ~]# grep 'hello\>' 1.txt
hello world
[root@66 ~]# grep 'go\{2}' 1.txt
grep: 不匹配的 \{
[root@66 ~]# grep 'go\{2\}' 1.txt
google
gooooogle
gooogle
[root@66 ~]# grep 'go\{2,3\}' 1.txt
google
gooooogle
gooogle
[root@66 ~]# vim 1.txt
[root@66 ~]# grep '[0-9]' 1.txt
123213123
21312e 12312 31
23123
172./g.2
1./g.21
172./g.12
193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep '\d' 1.txt
hello world
helloworld
hhhh world
[root@66 ~]# grep -P '\d' 1.txt
123213123
21312e 12312 31
23123
172./g.2
1./g.21
172./g.12
193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep '[a-zA-Z0-9.]' 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogle
taobao.com
hello world
helloworld
hhhh world
123213123
21312e 12312 31
23123
172./g.2
1./g.21
172./g.12
193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep -P '\w' 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogle
taobao.com
hello world
helloworld
hhhh world
123213123
21312e 12312 31
23123
172./g.2
1./g.21
172./g.12
193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep 'go+' 1.txt
[root@66 ~]# grep -E 'go?' 1.txt
[root@66 ~]# grep 'tao|j' 1.txt
[root@66 ~]# grep -E '(taobao|j).com'
^C^C
[root@66 ~]# grep -E '(taobao|j).com' 1.txt
taobao.com
taobao.com
taobao.com
[root@66 ~]# grep -E 'go{2}' 1.txt
google
gooooogle
gooogle
[root@66 ~]# grep '[0-9]\{1,3\}'\.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]{1,3\}' 1.txt
> ^C
[root@66 ~]# grep '[0-9]\{1,3\}'.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\} 1.txt
[root@66 ~]# cat 1.txt
ggle
gogle
google
gooooogle
taobao.com
taobao.com
taobaot.com
a.com
ab.com
gooogletaobao.com
hello world
helloworld
hhhh world
123213123
21312e 12312 31
23123172./g.2
1./g.21
172./g.12193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}}' 1.txt
[root@66 ~]# grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' 1.txt
193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep -P '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' 1.txt
193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep -E '([0-9]{1,3}\.){3}' [0-9]{1,3}' 1.txt
> ^C
[root@66 ~]# grep -E '([0-9]{1,3}\.){3} [0-9]{1,3}' 1.txt
[root@66 ~]# grep -E '([0-9]{1,3}\.){3} [0-9]{1,3}' 1.txt
[root@66 ~]# grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' 1.txt
193.168.235.2
193.168.235.23
193.168.235.123
[root@66 ~]# grep
↩︎