han8809 发表于 2018-8-21 08:35:17

Shell脚本———比较整数大小经典案例

  方法1:传参
  #!/bin/bash
  ##############################################################
  # File Name: compare1.sh
  # Version: V7.4
  # Author: feng yu
  # Organization: http://blog.51cto.com/13520761
  # Created Time : 2018-03-26 17:22:20
  # Description:
  ##############################################################
  [ $# -ne 2 ] && echo "请输入2个参数" && exit 2      ---参数个数不等于2时,提示请输入2个参数,并退出
  expr $1 + 1 &>/dev/null
  [ $? -eq 2 ] && echo "请输入整数" && exit 2             ---判断第一个参数是否为整数,不是整数,提示请输入整数,并退出
  expr $2 + 1 &>/dev/null
  [ $? -eq 2 ] && echo "请输入整数" && exit 2            ---判断第二个参数是否为整数,不是整数,提示请输入整数,并退出
  [ $1 -eq $2 ] && echo "$1 = $2"
  [ $1 -lt $2 ] && echo &quot;$1 < $2&quot;
  [ $1 -gt $2 ] && echo &quot;$1 > $2&quot;
  方法2:传参(if表达式)
  #!/bin/bash
  ##############################################################
  # File Name: compare.sh
  # Version: V7.4
  # Author: feng yu
  # Organization: http://blog.51cto.com/13520761
  # Created Time : 2018-03-26 16:20:43
  # Description:
  ##############################################################
  if ! [ $# -eq 2 ];then
  echo &quot;请输入2个参数&quot;
  exit
  fi
  expr $1 + 1 &>/dev/null
  if [ $? -eq 2 ];then
  echo &quot;请输入整数&quot;
  exit
  fi
  expr $2 + 1 &>/dev/null
  if [ $? -eq 2 ];then
  echo &quot;请输入整数&quot;
  exit
  fi
  if [ $1 -eq $2 ];then
  echo &quot;$1 = $2&quot;
  elif [ $1 -lt $2 ];then
  echo &quot;$1 < $2&quot;
  elif [ $1 -gt $2 ];then
  echo &quot;$1 > $2&quot;
  fi
  方法3:read读入
  #!/bin/bash
  ##############################################################
  # File Name: compare2.sh
  # Version: V7.4
  # Author: feng yu
  # Organization: http://blog.51cto.com/13520761
  # Created Time : 2018-03-26 18:20:14
  # Description:
  ##############################################################
  read -p &quot;请输入整数:&quot; num1
  expr $num1 + 1 &> /dev/null
  [ $? -eq 2 ] && echo &quot;请输入整数&quot; && exit 2
  [ -z &quot;$num1&quot; ] && echo &quot;请输入一个整数&quot; && exit 2
  read -p &quot;请输入整数:&quot; num2
  expr $num2 + 1 &> /dev/null
  [ $? -eq 2 ] && echo &quot;请输入整数&quot; && exit 2
  [ -z &quot;$num2&quot; ] && echo &quot;请输入一个整数&quot; && exit 2
  [ $num1 -eq $num2 ] && echo &quot;$num1 = $num2&quot;
  [ $num1 -gt $num2 ] && echo &quot;$num1 > $num2&quot;
  [ $num1 -lt $num2 ] && echo &quot;$num1 < $num2&quot;
  方法4:read读入(if表达式)
  #!/bin/bash
  ##############################################################
  # File Name: compare2.sh
  # Version: V7.4
  # Author: feng yu
  # Organization: http://blog.51cto.com/13520761
  # Created Time : 2018-03-26 18:20:14
  # Description:
  ##############################################################
  read -p &quot;请输入整数:&quot; num1
  expr $num1 + 1 &> /dev/null
  if [ $? -eq 2 ];then
  echo &quot;请输入整数&quot;
  exit 2
  fi
  if [ -z &quot;$num1&quot; ];then
  echo &quot;请输入一个整数&quot;
  exit 2
  fi
  read -p &quot;请输入整数:&quot; num2
  expr $num2 + 1 &> /dev/null
  if [ $? -eq 2 ];then
  echo &quot;请输入整数&quot;
  exit 2
  fi
  if [ -z &quot;$num2&quot; ];then
  echo &quot;请输入一个整数&quot;
  exit 2
  fi
  if [ $num1 -eq $num2 ];then
  echo &quot;$num1 = $num2&quot;
  elif [ $num1 -gt $num2 ];then
  echo &quot;$num1 > $num2&quot;
  elif [ $num1 -lt $num2 ];then
  echo &quot;$num1 < $num2&quot;
  fi

页: [1]
查看完整版本: Shell脚本———比较整数大小经典案例