shell脚本判断是否为纯数字
[root@host ]# cat test.sh
#!/bin/bash
test=$1
echo "$test"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ] #纯数字返回值为0,不是纯数字返回值不等于0
if [ "$?" = "0" ];then #判断返回值是否为0
echo "$test is number" #返回值等于0则执行
else
echo "$test not number" #反之返回值不等于0则执行
fi
[root@host ]# sh test.sh 1234
1234 is number
[root@host ]# sh test.sh 12345
12345 is number
[root@host ]# sh test.sh 12345d
12345d not number
[root@host ]# sh test.sh 12345a
12345a not number
[root@host ]# sh test.sh 12345a123
12345a123 not number
判断是否为数字、字母或者其他字符
[root@host ]# cat test.sh
#!/bin/bash
number=$1
if [[ $number =~ ^[a-zA-Z]+$ ]];then #正则匹配多个输入的字符
echo "$number 是字母"
elif [[ $number =~ ^[0-9]+$ ]];then #匹配多个数字
echo "$number 是数字"
else
echo "Input $number error!"
fi
[root@host ]# sh test.sh 34567
34567 是数字
[root@host ]# sh test.sh 3456d
Input 3456d error!
[root@host ]# sh test.sh aaacccfff
aaacccfff 是字母