• 合格linux运维人员必会的30道shell编程实践题及讲解-11


    企业实践题11:

    开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。以屏幕输出的方式提醒用户比较结果。

    注意:一共是开发2个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数做判断。 

    我的脚本=========================

    #read读入方式
    #!/bin/bash read -p "pls input num1: " num1 read -p "pls input num2: " num2 [ -z $num1 -a -z $num2 ] && { echo "USAGE: pls input two num {num1 num2}" exit 1 } [ "`echo "$num1"|sed -r ' s#[^0-9]##g'`" = "$num1" ] || { echo "first arg must be int." exit 2 } [ "`echo "$num2"|sed -r ' s#[^0-9]##g'`" = "$num2" ] || { echo "second arg must be int." exit 3 } [ $num1 -lt $num2 ] && { echo "$num1 < $num2" } [ $num1 -gt $num2 ] && { echo "$num1 > $num2" } [ $num1 -eq $num2 ] && { echo "$num1 = $num2" }
    #脚本传参方式
    #!/bin/bash
    
    [ $# -ne 2 ]&&{
      echo "USAGE: num1 num2"
      exit 1
    }
    
    [ "`echo "$1"|sed -r ' s#[^0-9]##g'`" = "$1" ]||{
      echo "first arg must be int."
      exit 2
    }
    
    [ "`echo "$2"|sed -r ' s#[^0-9]##g'`" = "$2" ]||{
      echo "second arg must be int."
      exit 2
    }
    
    [ $1 -lt $2 ]&&{
      echo "$1<$2"
      exit 0
    }
    
    [ $1 -eq $2 ]&&{
      echo "$1=$2"
      exit 0
    }
    
    [ $1 -gt $2 ]&&{
      echo "$1>$2"
      exit 0
    }
  • 相关阅读:
    C++面试考点
    C++面试考点
    C++11 引用叠加规则和模板参数类型推导规则
    C++11 引用叠加规则和模板参数类型推导规则
    C++11 auto和decltype推导规则
    C++11 auto和decltype推导规则
    RVO和NRVO
    RVO和NRVO
    Strange Bank(找零问题)
    eli和字符串 (牛客假期训练)
  • 原文地址:https://www.cnblogs.com/oliver-blogs/p/7723559.html
Copyright © 2020-2023  润新知