shell实例:判断用户输入的是否为数字

#!/bin/bash

# judge input

while read -p "Pls input Max lines:" MAX
do
   expr $MAX + 0 &>/dev/null
   if [ $? -eq 0 ];then
     break
   fi
done

echo "Success:$MAX"

 

说明:循环等待输入,直到输入的为整数

方法2: 字符串切分

#!/bin/bash
# jedge int

read -p "input:" str

if [ -z "${str//[0-9]/}" ];then
    echo "$str is num"
else
    echo "$str is not num"
fi

 

posted @ 2022-08-21 11:06  Richie`  阅读(210)  评论(0编辑  收藏  举报