Shell脚本入门(五)--- 随机字符生成、变量判断、正则表达式、shell的特殊符号、sed语句

本文介绍了Shell脚本中如何生成随机数字和字符,包括数字、字母和组合字符串,并讲解了正则表达式的使用,包括元字符、扩展正则表达式以及在Shell中的应用。此外,还探讨了Shell中的特殊符号,如通配符、命令连接符和sed语句的实践操作。
摘要由CSDN通过智能技术生成

Shell脚本入门(五)

随机数字字符生成 [tr]

生成32 位数字
/dev/urandom tr -dc a-z|head -c ${1:-32} ; echo
生成10个大写字母
 /dev/urandom tr -dc A-Z|head -c ${1:-10};echo
生成10个数字
/dev/urandom tr -dc 0-9|head -c ${1:-10};echo
生成10个数字和大写字母的组合字符串
 /dev/urandom tr -dc 0-9-A-Z|head -c ${1:-10};echo
生成 数字字母符号 15个
tr -dc '_A-Z#\-+=a-z(0-9%^>)]{<|' </dev/urandom | head -c 15; echo

判断变量是不是数字

#!usr/bin/bash
read -p "请输入一个数值: " num
if [[ ! "$num" =~ ^[0-9]+$ ]];then
        echo "你输入的不是数字,程序退出!"
        exit
fi
echo "你输入的是数字 $num"

正则表达式RE

正则表达式元字符 ^ 匹配以xx开头的行
[root@localhost tmp]# grep '^root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
$ 匹配以xxx结尾的行
[root@localhost tmp]# grep 'bash$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
^r…t 匹配首尾
[root@localhost srv]# grep '^r..t' 1.txt 
root:x:0:0:root:/root:/bin/bash
rabt:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost srv]# grep '^r.t' 1.txt 
rot:x:1:1:bin:/bin:/sbin/nologin
[] 匹配指定范围内的一个字符
[root@localhost srv]# grep '[rx]oot' 1.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/xoot:/sbin/nologin
[ - ] 匹配指定范围内的一个字符
[a-z]oot 匹配 a-z 的oot
[root@localhost srv]# grep '[a-z]oot' 1.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/xoot:/sbin/nologin
[a-z0-9]oot 匹配a-z 0-z oot
[root@lo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值