Bash:如何从数字中提取数字
发布时间:2020-12-15 21:24:30 所属栏目:安全 来源:网络整理
导读:所以我在我的脚本中引入一个数字,就像这样: ./script 795 我想从该数字中提取每个数字并检查它是否小于7.如下所示: if [ 7 -le 7 ]then echo "The first digit is smaller than 7" 我想知道如何提取每个数字. 解决方法 您可以使用子字符串来提取脚本的第一
所以我在我的脚本中引入一个数字,就像这样:
./script 795 我想从该数字中提取每个数字并检查它是否小于7.如下所示: if [ 7 -le 7 ] then echo "The first digit is smaller than 7" 我想知道如何提取每个数字. 解决方法
您可以使用子字符串来提取脚本的第一个参数的第一个字符:
if [ ${1:0:1} -lt 7 ]; then echo "The first digit is smaller than 7" fi 要为每个角色执行此操作,您可以使用循环: for (( i = 0; i < ${#1}; ++i )); do if [ ${1:$i:1} -lt 7 ]; then echo "Character $i is smaller than 7" fi done 请注意,我已将-le(小于或等于)更改为-lt(小于)以使您的消息正确无误. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |