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