网上搜到的几种方法,记载备用:
expr match "123456" "[0-9][0-9]*$"
返回值大于0,就说明全部都是数字
1) Bash >= 3.0
if [[ "${variable}" =~ "^[[:digit:]]*$" ]]; then
echo "Found digit string!"
fi
2) Bash >= 2.0
if grep '^[[:digit:]]*$' <<< "${variable}"; then
echo "Found digit string!"
fi
read input
tt=`echo $input | sed -n "/^[0-9]\+$/p"`
if [ "x$tt" == "x" ]; then
echo no
else
echo yes
fi
printf "Enter: "
read var
if [[ $var =~ ^[0-9]+$ ]]
then
echo "Number."
elif [[ $var =~ ^[A-Za-z]+$ ]]
then
echo "String."
else
echo "mixed number and string or others "
fi
或者 awk or gawk
#! /bin/bash
printf "Enter: "
read var
echo "$var" | gawk '{
if ($0 ~ /^[0-9]+$/){
print "Number "
} else if ($0 ~ /^[A-Za-z]+$/){
print "String "
} else {
print "Mixed number and string or others"
}
}'
阅读(668) | 评论(0) | 转发(0) |