zwd 发表于 2018-8-24 11:34:42

Shell 实现简单计算器功能

# cat jisuan.sh  
#!/bin/bash
  
print_usage(){
  
    printf $"USAGE:$0 NUM1 {+|-|*|/} NUM2\n"
  
    exit 1
  
}
  

  
#判断传入的参数是不是3个
  
if [ $# -ne 3 ]
  
then
  
    print_usage
  
fi
  

  
firstnum=$1
  
secondnum=$3
  
op=$2
  

  
#对传入的参数进行判断看是不是合理
  
if [ -n "`echo $firstnum|sed 's///g'`" ];then
  
    print_usage
  
fi
  

  
if [ "$op" != "+" ]&&[ "$op" != "-" ]&&[ "$op" != "*" ]&&[ "$op" != "/" ];then
  
   print_usage
  
fi
  

  
if [ -n "`echo $secondnum|sed 's///g'`" ];then
  
    print_usage
  
fi
  

  
echo "${firstnum}${op}${secondnum}=$((${firstnum}${op}${secondnum}))"


页: [1]
查看完整版本: Shell 实现简单计算器功能