Loading... 通过脚本判断输入值是数字执行后续命令。 ``` #!/bin/bash ## 方法1,有点问题 #if [[ "$1" =~ "^[[:digit:]]*$" ]];then #if [[ "$1" =~ "^[0-9]+$" ]];then # echo "$1 is number." #else # echo 'no.' #fi ## 方法2, 可以,不过不是bash实现的,是使用了grep的正则 #if grep '^[[:digit:]]*$' <<< "$1";then # echo "$1 is number." #else # echo 'no.' #fi ## 方法3 #if [ "$1" -gt 0 ] 2>/dev/null ;then # echo "$1 is number." #else # echo 'no.' #fi ## 方法4,case #case "$1" in # [1-9][0-9]*) # echo "$1 is number." # ;; # *) # ;; #esac ## 方法5,awk #echo $1| awk '{print($0~/^[-]?([0-9])+[.]?([0-9])+$/)?"number":"string"}' ## 方法5,awk #if [ -n "$(echo $1| sed -n "/^[0-9]\+$/p")" ];then # echo "$1 is number." #else # echo 'no.' #fi ## 方法6,expr expr $1 "+" 10 &> /dev/null if [ $? -eq 0 ];then echo "$1 is number" else echo "$1 not number" fi ``` 侵删转自:[https://www.iteye.com/blog/xiaohuafyle-1812437](https://www.iteye.com/blog/xiaohuafyle-1812437) 最后修改:2024 年 10 月 07 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏