1.统计字符串的长度
[root@test3 ~]# echo 'wwww'|wc -L4
统计文件中最长行的长度
[root@test3 ~]# cat 1.txt
aa
aaaaa
a
[root@test3 ~]# cat 1.txt |wc -L5[root@test3 ~]# echo 'wwww'|awk '{print length}'4
前面加个#就是统计长度信息[root@test3 ~]# name=oldboy[root@test3 ~]# echo ${name}
oldboy
[root@test3 ~]# echo ${#name}6
笔试题,小于三的输出
[root@test3 ~]# echo I am lihua I am 18
I am lihua I am 18[root@test3 ~]# for i in `echo I am lihua I am 18` ;do if [[ `echo $i |wc -L` < 3 ]];then echo $i;fi;done
I
am
I
am
18
awk按行去统计
[root@test3 ~]# cat 1.txt
I
am
I
aaaaa
am
18[root@test3 ~]# cat 1.txt |awk '{print length}'121522[root@test3 ~]# cat 1.txt |awk '{if(length($1)<3)print $1}'
I
am
I
am
18[root@test3 ~]# echo I am lihua I am 18|xargs -n1
I
am
lihua
I
am
18
awk按列去统计 NF最后一列的列号
[root@test3 ~]# echo I am lihua I am 18|awk '{for(i=1;i<=NF;i++)if(length($i)<3)print $i}'
I
am
I
am
18
变量子串的删除
不想要前面的www.
[root@test3 ~]# url=www.baidu.com[root@test3 ~]# echo $url
www.baidu.com
可以用sed替换
[root@test3 ~]# echo $url|sed 's/www.//g'
baidu.com
可以用# #在前面表示统计字符串长度,在后面表示要删除的内容[root@test3 ~]# echo ${url#www.}
baidu.com
也可以用正则来表示
[root@test3 ~]# echo ${url#*.}
baidu.com
%表示从后面往前删
[root@test3 ~]# echo ${url%.com}
www.baidu
[root@test3 ~]# a=10.5[root@test3 ~]# [ $a -eq 10 ]
-bash: [: 10.5: integer expression expected
[root@test3 ~]# [ ${a%.*} -eq 10 ]
表示替换
[root@test3 ~]# echo $url
www.baidu.com
[root@test3 ~]# echo ${url/baidu/sina}
www.sina.com
语法结构
语法1; test-f /etc/hosts # 判断hosts文件是否存在,存在则为真
语法2: [-f /etc/hosts ]# 判断hosts文件是否存在,存在则为真-f#判断文件是否存在-d#判断目录是否存在-e#存在即为真,不管是啥,在就行-r#可读为真,一般测试的是属主-w#可写为真,一般测试的是属主-x#可执行为真,一般测试的是属主-n#长度不为0-z#长度为0
并且 &&
或者 ||
如果写在if判断里面 -a 是并且 -o是或者
if[[-f /etc/hosts -a-d /etc ]]thenecho ok
elseecho no
使用man test可以查看这些参数
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and read permission is granted
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set-w FILE
FILE exists and write permission is granted
-x FILE
FILE exists and execute (or search) permission is granted
传参的时候会用
[root@test3 ~]# xx=root[root@test3 ~]# [ -n $xx ]&& echo 1 || echo 21[root@test3 ~]# [ -z $xx ]&& echo 1 || echo 22
判断你输入的是否为整数
#!/bin/bashread-p'please input age: ' age
expr1 + $age> /dev/null 2>&1[$?-ne0]&&echo'必须输入整数'
jumpserver服务段 test3 192.168.23.103
客户端 test2 192.168.23.102
客户端 test2 192.168.23.101
[root@test3 ~]# cat 1.sh #!/bin/bashIP='192.168.23.102'nfs='192.168.23.101'echo-e"\t\033[34m1.$IP\033[0m"echo-e"\t\033[34m2.$nfs\033[0m"read-p'Please input number you wish to connect to.: ' num1
case$num1in1)ssh$IP;;2)ssh$nfs;;
*)echo"Usage $0 [1|2]"esac[root@test3 ~]# bash [root@test3 ~]# bash 1.sh 1.192.168.23.102
2.192.168.23.101
Please input number you wish to connect to.: 1
Last login: Sat Jan 421:36:21 2025 from 192.168.23.1
[root@test2 ~]# exit
此时退出他就直接退回到原本的机器上了,既然能退回到原本的住机器上,那我为什么不能直接ssh连呢,这将是致命的
框架,
#!/bin/bashIP='192.168.23.102'nfs='192.168.23.101'fun(){echo-e"\t\033[34m1.$IP\033[0m"echo-e"\t\033[34m2.$nfs\033[0m"echo-e"\t\033[34m3.meau\033[0m"}
fun
whiletruedoread-p'Please input number you wish to connect to.(3 is help): ' num1
case$num1in1)ssh$IP;;2)ssh$nfs;;3)
fun
;;
*)echo"Usage $0 [1|2]"esacdone
----------------------------------------------------------------------------
完善jumpserver跳板机
1.运维和开发是不是属于不同的账号
2.运维和开发登录跳板机输入密码
3.开发的菜单和运维的不同
主菜单
1.运维
2.开发
子菜单
1.显示运维可以连接的服务器信息
2.显示开发可以连接的服务器的信息
=====================菜单的框架============================================[root@test3 ~]# cat 1.sh #!/bin/bashIP='192.168.23.102'nfs='192.168.23.101'fun1(){echo-e"\t\033[34m1.ops\033[0m"echo-e"\t\033[34m2.dev\033[0m"}ops(){echo-e"\t\033[34m1.$IP\033[0m"echo-e"\t\033[34m2.$nfs\033[0m"echo-e"\t\033[34m3.meau\033[0m"}dev(){echo-e"\t\033[34m1.$nfs\033[0m"echo-e"\t\033[34m2.meau\033[0m"}whiletruedo
fun1
read-p'请选择你的角色编号:' num1
case$num1in1)
ops
whiletruedoread-p'请输入你要连接的主机编号:' num2
if[[$num2-eq1]]thenssh$IPelif[[$num2-eq2]]thenssh$nfselif[[$num2-eq3]]then
ops
elseecho'只能输入[1|2|3]'breakfidone;;2)
dev
whiletruedoread-p'请输入你要连接的主机编号:' num3
if[[$num3-eq1]]thenssh$nfselif[[$num3-eq2]]then
dev
elseecho'请输入[1|2] 'breakfidone;;
*)echo"Usage $0 [1|2]"esacdone===========================================================================================
通过邮箱找回密码(难道和简单的两种)
简单的可以把密码写入到一个文件中,通过找提取密码来对比,也可以写死
[root@test3 ~]# cat pass.txt
oldboy 123456[root@test3 ~]# grep -w oldboy pass.txt |awk '{print $2}'123456
写死
[root@test3 ~]# cat 1.sh#!/bin/bashIP='192.168.23.102'nfs='192.168.23.101'fun1(){echo-e"\t\033[34m1.ops\033[0m"echo-e"\t\033[34m2.dev\033[0m"}ops(){echo-e"\t\033[34m1.$IP\033[0m"echo-e"\t\033[34m2.$nfs\033[0m"echo-e"\t\033[34m3.meau\033[0m"}dev(){echo-e"\t\033[34m1.$nfs\033[0m"echo-e"\t\033[34m2.meau\033[0m"}whiletruedo
fun1
read-p'请选择你的角色编号:' num1
case$num1in1)i=0whiletruedoread-s-p'请输入密码:' pass
let i++
if[[$i=5]]thenecho'你已经输错五次了,请30s后尝试'# breaksleep30unset i
# exitfiif[["$pass"='123456']]thenbreakelseecho-e"\t"echo'请输入正确的密码'# break#continuefidoneecho-e"\t"
ops
whiletruedoread-p'请输入你要连接的主机编号:' num2
if[[$num2-eq1]]thenssh$IPelif[[$num2-eq2]]thenssh$nfselif[[$num2-eq3]]then
ops
elseecho'只能输入[1|2|3]'breakfidone;;2)read-p'请输入密码:' pass
错误四次发邮箱验证码
if[$i=4];thenread-p"尝试次数过多,请输入邮箱地址找回密码: " MA
if[$MA="2334537366@qq.com"];thenecho$RANDOM|md5sum|cut -c1-8>p.txt
mail -s'验证码'"2334537366@qq.com"<p.txt
read-p"请输入验证码: " a
[$a=`cat p.txt`]&& mail -s'账号密码请记牢固'"2334537366@qq.com"<pass.txt
fifi
不能ctrl + c,
在我们执行ctrl + c 或者 +z +q等等的时候,他都是通过 trap 给系统发送终止信号
trap"" HUP TSTP INT 禁止ctrl + c ctrl + z ctrl + q,就是不允许你在脚本里面退出
trap"echo 别乱按,小心爆炸" HUP TSTP INT
while循环
while语法
while 条件表达式 #为真才执行do
执行的命令集合
done
列
[root@test3 ~]# cat 2.sh #!/bin/bashwhile[1-eq1] 或者true
doecho hehe
sleep1done[root@test3 ~]# bash 2.sh
hehe
hehe
使用while从1加到100
[root@test3 ~]# bash 2.sh 5050[root@test3 ~]# cat 2.sh #!/bin/bashi=1sum=0while[$i-le100]dosum=$[$sum+$i]let i++
doneecho$sum[root@test3 ~]# bash 2.sh 5050
read是读
line是行 所以说while是按照一行一行的读取文件,for循环读取文件是按照空格来读取
line是自定义变量,叫啥都行
whileread line
dodone< 文件路径
[root@test3 ~]# cat 1.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@test3 ~]# vim 2.sh[root@test3 ~]# cat 2.sh#!/bin/bashwhileread line
doecho$line
done</root/1.txt
[root@test3 ~]# bash 2.sh
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
分别用while和for来创建用户 已经设置对应的密码
[root@test3 ~]# cat 1.txt
zs oldboy123
xp oldboy456
sh oldboy789
for循环实例
[root@test3 ~]# cat 2.sh #!/bin/bashforiin`cat1.txt |awk'{print $1}'`dopass=`cat1.txt|grep $i|awk'{print $2}'`useradd$iecho$pass|passwd--stdin$idone[root@test3 ~]# bash 2.sh
Changing password for user zs.
passwd: all authentication tokens updated successfully.
Changing password for user xp.
passwd: all authentication tokens updated successfully.
Changing password for user sh.
passwd: all authentication tokens updated successfully.
while循环取列
[root@test3 ~]# cat 2.sh #!/bin/bashwhileread a b
doecho$a
done<1.txt
[root@test3 ~]# bash 2.sh
zs
xp
sh[root@test3 ~]# bash 2.sh
Changing password for user zs.
passwd: all authentication tokens updated successfully.
Changing password for user xp.
passwd: all authentication tokens updated successfully.
Changing password for user sh.
passwd: all authentication tokens updated successfully.
[root@test3 ~]# cat 2.sh #!/bin/bashwhileread a b
douseradd$aecho$b|passwd--stdin$a
done<1.txt
whilereaddodone< 也支持命令
[root@test3 ~]# cat 2.sh #!/bin/bashwhileread a b
doecho$adone<<(cat1.txt )[root@test3 ~]# bash 2.sh
zs
xp
sh
shell命令行来执行循环
[root@test3 ~]# for i in `echo {1..5}`;do echo $i;done12345
创建用户
[root@test3 ~]# for i in `echo {1..5}`;do useradd test$i;done[root@test3 ~]# tail -n5 /etc/passwd
test1:x:1004:1004::/home/test1:/bin/bash
test2:x:1005:1005::/home/test2:/bin/bash
test3:x:1006:1006::/home/test3:/bin/bash
test4:x:1007:1007::/home/test4:/bin/bash
test5:x:1008:1008::/home/test5:/bin/bash
删除用户
[root@test3 ~]# for i in `echo {1..5}`;do userdel -r test$i;done
sed是一个流编辑器,非交互式的编辑器,它一次处理一行内容.
处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(patternspace)
sed命令格式
sed[options]'command' file(s)-e 允许多项编辑
-n 取消默认的输出
-i 直接修改对应文件
-r 支持扩展元字符
sed`内置命令参数
a 在当前行后添加一行或多行
c 在当前行进行替换修改
d 在当前行进行删除操作
i 在当前行之前插入文本
p 打印匹配的行或指定行
n 读入下一输入行,从下一条命令进行处理
! 对所选行以外的所有行应用命令
h 把模式空间里的内容重定向到暂存缓冲区
H 把模式空间里的内容追加到暂存缓冲区
g 取出暂存缓冲区的内容,将其复制到模式空间,覆盖该处原有内容
G 取出暂存缓冲区的内容,将其复制到模式空间,追加在原有内容后面
注释2-6行
sed'2,6s/^/#/' /etc/passwd