gpt4 book ai didi

shell - 在 fish shell 中如何检查变量是否为数字?

转载 作者:行者123 更新时间:2023-12-05 00:48:10 25 4
gpt4 key购买 nike

如何检查一个变量是否是 fish shell 中的数字?寻找一种简单的方法。

最佳答案

一种可能的解决方案是使用 string match,如下所示:

for arg in 9 -42 0 3.14159 stack overflow hyphenated-word 922-99999 0...0 555.555.555
if string match -qr '^-?[0-9]+(\.?[0-9]*)?$' -- "$arg"
echo $arg is a number
end
end

9 is a number
-42 is a number
0 is a number
3.14159 is a number

如果 num 不是数字,则不会回显任何内容。

这里是 string match 来自 fish shell 文档 (https://fishshell.com/docs/current/commands.html#string) 的概要:

string match [(-a | --all)] [((-e | --entire)] [(-i | --ignore-case)] [(-r | --regex)]
[(-n | --index)] [(-q | --quiet)] [(-v | --invert)] PATTERN [STRING...]

关于shell - 在 fish shell 中如何检查变量是否为数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52798129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com