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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32107041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:03:57  来源:igfitidea点击:

How to check if a string only contains digits/numerical characters

bash

提问by 3kstc

How can I check if MyVarcontains onlydigits with an ifstatement with BASH. By digits I am referring to 0-9.

如何检查是否MyVar包含带有 BASH 语句的数字。通过数字,我指的是 0-9。if

ie:

IE:

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

回答by giliev

Here it is:

这里是:

#!/bin/bash
if [[  =~ ^[0-9]+$ ]]
then
    echo "ok"
else
    echo "no"
fi

It prints okif the first argument contains only digits and nootherwise. You could call it with: ./yourFileName.sh inputValue

它打印ok第一个参数是否只包含数字,no否则打印。你可以这样称呼它:./yourFileName.sh inputValue

回答by John1024

[[ $myvar =~ [^[:digit:]] ]] || echo All Digits

Or, if you like the if-thenform:

或者,如果您喜欢这种if-then形式:

if [[ $myvar =~ [^[:digit:]] ]]
then
    echo Has some nondigits
else
    echo all digits
fi

In olden times, we would have used [0-9]. Such forms are not unicode safe. The modern unicode-safe replacement is [:digit:].

在过去,我们会使用[0-9]. 此类表单不是 Unicode 安全的。现代 unicode 安全的替代品是[:digit:].

回答by David C. Rankin

If you would like to test in a POSIX compliant way, you can use either:

如果您想以符合 POSIX 的方式进行测试,您可以使用:

expr string : regex        ## returns length of string if both sides match

or

或者

expr match string regex    ## behaves the same

For example to test if $myvaris all digits:

例如测试是否$myvar是所有数字:

[ $(expr "x$myvar" : "x[0-9]*$") -gt 0 ] && echo "all digits"

Note:'x'prepended to the variable and expression to protect against test of empty-string throwing error. To use the lengthreturned by the test, don't forget to subtract 1which represents the 'x'.

注意:'x'前置到变量和表达式以防止空字符串抛出错误的测试。要使用length测试返回的 ,不要忘记减去1代表'x'.

In if-then-elseform, here is a short script that tests whether the first argument to the script contains all digits:

if-then-else表单中,这是一个简短的脚本,用于测试脚本的第一个参数是否包含所有数字:

#!/bin/sh

len=$(expr "x" : "x[0-9]*$")  ## test returns length if  all digits
let len=len-1                   ## subtract 1 to compensate for 'x'

if [ $len -gt 0 ]; then         ## test if $len -gt 0 - if so, all digits
    printf "\n '%s' : all digits, length: %d chars\n" "" $len
else
    printf "\n '%s' : containes characters other than [0-9]\n" ""
fi

Example Output

示例输出

$ sh testdigits.sh 265891

 '265891' : all digits, length: 6 chars

$ sh testdigits.sh 265891t

 '265891t' : contains characters other than [0-9]

The bash regular expression test [[ $var =~ ^[0-9]+$ ]]is fine, I use it, but it is a bashism(limited to the bash shell). If you are concerned with portability, the POSIX test will work in any POSIX compliant shell.

bash正则表达式测试没问题[[ $var =~ ^[0-9]+$ ]],我用了,但它是bashism(仅限于bash shell)。如果您关心可移植性,POSIX 测试将在任何 POSIX 兼容 shell 中工作。

回答by Abhishek J

Simple! just use expression option in grep The below solution echo's the variable and checks if it contains only digits

简单的!只需在 grep 中使用表达式选项 下面的解决方案 echo 是变量并检查它是否只包含数字

if [[ $(echo $var | grep -E "^[[:digit:]]{1,}$") ]]
then
    echo "var contains only digits"
else
    echo "var contains other characters apart from digits"
fi

回答by MrPotatoHead

Grep expression solution without [[ ]], for wider shell compatability:

没有 [[]] 的 Grep 表达式解决方案,以获得更广泛的 shell 兼容性:

if [ "$(echo $var | grep -E "^[0-9]{1,}$")" ]; then echo "digits only"; then
    echo "var contains digits only"
else
    echo "var contains digits and/or other characters"
fi