加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 如何检查字符串是否只包含数字/数字字符

发布时间:2020-12-16 01:06:59 所属栏目:安全 来源:网络整理
导读:如何检查MyVar是否仅包含带有BASH的if语句的数字.数字我指的是0-9. 即: if [[ $MyVar does contain digits ]] -- How can I check if MyVar is just contains numbersthen do some maths with $MyVarelse do a different thingfi 这里是: #!/bin/bashif [[
如何检查MyVar是否仅包含带有BASH的if语句的数字.数字我指的是0-9.

即:

if [[ $MyVar does contain digits ]]  <-- How can I check if MyVar is just contains numbers
then
 do some maths with $MyVar
else
 do a different thing
fi
这里是:
#!/bin/bash
if [[ $1 =~ ^[0-9]+$]]
then
    echo "ok"
else
    echo "no"
fi

如果第一个参数只包含数字,则打印确定,否则不打印.你可以用:./ yourFileName.sh inputValue来调用它

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读