Matthewl 发表于 2018-8-27 09:42:05

shell计算器脚本

  1 #!/bin/bash
  2 read -t 30 -p "Please enter the first value:" VA1
  3 read -p "Please enter operator:" OP1
  4 read -p "Please enter the second value:" VA2
  5 if [ -n "$VA1" -a -n "$VA2" ]
  6   then
  7         test1=`echo "$VA1" | sed 's///g'`
  8         test3=`echo "$VA2" | sed 's///g'`
  9      if [ -z "$test1" -a -z "$test3" ]
  10         then
  11            if [ "$OP1" == '+' ]
  12               then
  13                     SUM=$(($VA1+$VA2))
  14         elif[ "$OP1" == '-' ]
  15               then
  16                     SUM=$(($VA1-$VA2))
  17         elif[ "$OP1" == '*' ]
  18                  then
  19                     SUM=$(($VA1*$VA2))
  20         elif[ "$OP1" == '/' ]
  21                  then
  22                     SUM=$(($VA1/$VA2))
  23         else
  24                echo "please enter the corret operator!"
  25                exit 406
  26         fi
  27      else
  28         echo "please enter the corret number!"
  29         exit 405
  30    fi
  31 else
  32      echo "please enter thenumber!"
  33      exit 404
  34 fi
  35      echo "$VA1$OP1$VA2=$SUM"
  1 #!/bin/bash
  2 read -t 30 -p "Please enter the first value:" VA1
  3 read -p "Please enter operator:" OP1
  4 read -p "Please enter the second value:" VA2
  5 if [ -z "$VA1" -o -z "$VA2" -o -z "$OP1" ]
  6   then
  7         echo "Please enter the correct value!"
  8         exit 2
  9 fi
  10 if [ -n "$VA1" -a -n "$VA2" -a -n "$OP1" ]
  11   then
  12         test1=`echo "$VA1" | sed 's///g'`
  13         test2=`echo "$OP1" | sed -e 's/-//g;s/+//g;s/*//g;s/[/]//g;s/[%]//g'    `
  14         test3=`echo "$VA2" | sed 's///g'`
  15 fi
  16 if [ -n "$test1" -o -n "$test2" -o -n "$test3" ]
  17      then
  18         echo    "Please enter a correct numeric or operator value!"
  19         exit 22
  20 fi
  21 if [ -z "$test1" -a -z "$test2" -a -z "$test3" ]
  22      then
  23         echo "Calculations are performed when!"
  24   if [ "$OP1" == '+' ]
  25       then
  26            echo "$VA1$OP1$VA2=$(($VA1$OP1$VA2))"
  27 elif[ "$OP1" == '-' ]
  28                  then
  29                        echo "$VA1$OP1$VA2=$(($VA1$OP1$VA2))"
  30 elif[ "$OP1" == '*' ]
  31                  then
  32                        echo "$VA1$OP1$VA2=$(($VA1$OP1$VA2))"
  33 elif[ "$OP1" == '/' ]
  34                  then
  35                        echo "$VA1$OP1$VA2=$(($VA1$OP1$VA2))"
  36 else
  37                        echo "$VA1$OP1$VA2=$(($VA1$OP1$VA2))"
  38   fi
  39 fi

页: [1]
查看完整版本: shell计算器脚本