怎么样判断一个变量是数字或字符 shell
3个回答
展开全部
#! /bin/bash
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"
}
}'
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"
}
}'
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
read input
tst=`echo $input | sed -n "/^[0-9]\+$/p"`
if [ "x$tt" == "x" ]; then
echo no
else
echo yes
fi
tst=`echo $input | sed -n "/^[0-9]\+$/p"`
if [ "x$tt" == "x" ]; then
echo no
else
echo yes
fi
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询