国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Linux > 正文

對用戶輸入的判斷的shell實現代碼

2019-10-26 18:48:49
字體:
來源:轉載
供稿:網友

今天的案例是將 對用戶輸入的判斷的

#!/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個輸入;
2)nodigits="$(echo $testvalue | sed 's/[[:digit:]]//g')" 為后面測試用戶輸入的是否全為數字做準備
3)if validint "$1" "$2" "$3" ; then 注意 "$1" "$2" "$3"要加引號。
4)testvalue變量是為了過濾負數后測試輸入是否全為數字的。
5)感覺想得挺周全的。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 囊谦县| 昌都县| 明水县| 洛南县| 昌乐县| 浙江省| 紫云| 琼结县| 黑河市| 清水县| 永安市| 无锡市| 浪卡子县| 宁阳县| 平遥县| 万载县| 定结县| 芒康县| 旅游| 于田县| 伊金霍洛旗| 井冈山市| 嵊州市| 淮北市| 双城市| 黄骅市| 焉耆| 大兴区| 新巴尔虎右旗| 长治市| 扎囊县| 闽侯县| 柯坪县| 库伦旗| 溆浦县| 湟中县| 叶城县| 汉阴县| 五华县| 浠水县| 阜宁县|