简体   繁体   English

如何检查字符串的第一个字符是否为bash中的数字?

[英]How to check if the first character of a string is number in bash?

I am writing an xml element in bash. 我在bash中编写xml元素。 I need to replace some strings which will start with numbers into something that starts with an character, so it is a valid XML tag (remember xml tags cannot start with alpha numeric characters). 我需要将一些以数字开头的字符串替换为以字符开头的字符串,因此它是有效的XML标记(请记住xml标记不能以字母数字字符开头)。

For Example, 例如,

  str = 192.16.76.52,1001

which is not a valid xml tag, ie i cannot do 这不是有效的xml标记,即我无法执行

<192.16.76.52,1001>
.....
</192.16.76.52,1001>

So, I am planning to detect if it is a number and replace it with something like 因此,我打算检测它是否为数字并将其替换为

IP192.16.76.52

I know I can do it with IFS, but it might take long time and extra code. 我知道我可以使用IFS做到这一点,但可能需要很长时间和额外的代码。

Since you know the main problem, feel free to comment or offer suggestions. 既然您知道了主要问题,请随时发表评论或提出建议。

Thanks 谢谢

The RHS of = in [[ takes a glob pattern. [[=的RHS采用球形模式。

$ [[ 123 = [0-9]* ]] ; echo $?
0
$ [[ foo = [0-9]* ]] ; echo $?
1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM