最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

對(duì)用戶輸入的判斷的shell實(shí)現(xiàn)代碼

 更新時(shí)間:2016年08月14日 11:20:21   投稿:mdxy-dxy  
今天的案例是將 對(duì)用戶輸入的判斷的shell腳本,需要的朋友可以參考下

今天的案例是將 對(duì)用戶輸入的判斷的

#!/bin/sh
# validint -- Validates integer input, allowing negative ints too.

function validint
{
 # Validate first field. Then test against min value $2 and/or
 # max value $3 if they are supplied. If they are not supplied, skip these tests.

 number="$1";   min="$2";   max="$3"

 if [ -z $number ] ; then
  echo "You didn't enter anything. Unacceptable." >&2 ; return 1
 fi

 if [ "${number%${number#?}}" = "-" ] ; then # is first char a '-' sign?
testvalue="${number#?}"   # all but first character
 else
  testvalue="$number"
 fi

 nodigits="$(echo $testvalue | sed 's/[[:digit:]]//g')"

 if [ ! -z $nodigits ] ; then
  echo "Invalid number format! Only digits, no commas, spaces, etc." >&2
  return 1
 fi

 if [ ! -z $min ] ; then
  if [ "$number" -lt "$min" ] ; then
    echo "Your value is too small: smallest acceptable value is $min" >&2
    return 1
  fi
 fi
 if [ ! -z $max ] ; then
   if [ "$number" -gt "$max" ] ; then
    echo "Your value is too big: largest acceptable value is $max" >&2
    return 1
   fi
 fi
 return 0
}


if validint "$1" "$2" "$3" ; then
 echo "That input is a valid integer value within your constraints"
fi

解析腳本:
1) number="$1"; min="$2"; max="$3" 指用戶的3個(gè)輸入;
2)nodigits="$(echo $testvalue | sed 's/[[:digit:]]//g')" 為后面測試用戶輸入的是否全為數(shù)字做準(zhǔn)備
3)if validint "$1" "$2" "$3" ; then 注意 "$1" "$2" "$3"要加引號(hào)。
4)testvalue變量是為了過濾負(fù)數(shù)后測試輸入是否全為數(shù)字的。
5)感覺想得挺周全的。

相關(guān)文章

最新評(píng)論

凤阳县| 鄂尔多斯市| 武鸣县| 祁门县| 北安市| 明水县| 灵宝市| 寻乌县| 夏津县| 德化县| 闽清县| 沁阳市| 多伦县| 涪陵区| 嵊州市| 彭阳县| 博爱县| 深水埗区| 疏勒县| 比如县| 安阳县| 浙江省| 秦皇岛市| 会东县| 阿尔山市| 长武县| 浦城县| 商洛市| 莱州市| 会理县| 北京市| 永川市| 达日县| 乌海市| 双柏县| 图木舒克市| 桦南县| 漳州市| 虞城县| 米泉市| 永兴县|