shell - 在鱼壳中如何检查变量是否为数字?
问题描述
如何检查变量是否是鱼壳中的数字?寻找一种简单的方法。
解决方案
一种可能的解决方案是使用string match
如下:
set num 123
if string match -qr '^[0-9]+$' $num; echo 'it is a number!'; end
如果num
不是数字,则不会回显任何内容。
这是string match
鱼壳文档(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...]
推荐阅读
- ios - CollectionView Segue 在接收 ViewController 时发现 nil
- java - 将 ArrayNode 放入 ObjectNode
- ruby - 有人可以解释这个 ruby 片段中实例变量 @ 前缀的用法吗?
- python - 检查 os.system 在执行期间是否引发错误
- gradle - 如何找到 Gradle 任务的“类型”?
- appstore-approval - 如果我的二进制文件已经获得批准,我可以提交另一个更新吗
- angularjs - AngularJS 的 Apollo 客户端
- javascript - 可拖动元素行为
- python - 为字符串解析 ~4k 文件(复杂条件)
- python - 安装特定版本后 pip 不升级