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
How to check if a string only contains digits/numerical characters
提问by 3kstc
How can I check if MyVar
contains onlydigits with an if
statement 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 ok
if the first argument contains only digits and no
otherwise. 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-then
form:
或者,如果您喜欢这种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 $myvar
is 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 length
returned by the test, don't forget to subtract 1
which represents the 'x'
.
注意:'x'
前置到变量和表达式以防止空字符串抛出错误的测试。要使用length
测试返回的 ,不要忘记减去1
代表'x'
.
In if-then-else
form, 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