首页 > 系统相关 >shell整数计算器

shell整数计算器

时间:2023-09-22 18:00:13浏览次数:28  
标签:arr shell last fuhao grep 整数 echo 计算器 fi

#! /bin/bash

checkInt() {
  arr=$1
  for i in "${arr[@]}" ; do
    temp=`echo $i | sed 's/[0-9]//g' | sed 's/[ ]*//g'`
    if [ -n "$temp" ]; then
      echo "$i must be integer"
      return 1
    fi
  done
  return 0
}

fuhao="" #判断符号
getFuhao() {
  f=$(echo $1 | grep +)
  if [ -n "$f" ]; then
    fuhao="+"
  fi
  f=$(echo $1 | grep -)
  if [ -n "$f" ]; then
    fuhao="-"
  fi
  f=$(echo $1 | grep x)
  if [ -n "$f" ]; then
    fuhao="x"
  fi
  f=$(echo $1 | grep /)
  if [ -n "$f" ]; then
    fuhao="/"
  fi
  #echo "fuhao=$fuhao"
}

getLast() {
  arr=$1
  last=0;
  case $fuhao in
  "+")
    last=$((${arr[0]} + ${arr[1]}))
    ;;
  "-")
    last=$((${arr[0]} - ${arr[1]}))
    ;;
  "x")
    last=$((${arr[0]} * ${arr[1]}))
    ;;
  "/")
    last=$((${arr[0]} / ${arr[1]}))
    ;;
  esac
  echo "${arr[0]} $fuhao ${arr[1]} = $last"
}

r="" #输入
checkinputAndCompute() {
  while true; do
      echo "please input two num to plus, eg: a +-x/ b"
      read r
      if [ -z "$r" ]; then
        echo "cannot be null"
      else
        r=`echo "$r" | sed 's/\\*/x/'`
        getFuhao $r
        arr=(${r//$fuhao/ })
        if [ ${#arr[@]} -ne 2 ]; then
          echo "error input, eg: a +-x/ b"
        else
          if checkInt $arr; then
            getLast $arr
          fi
        fi
      fi
  done
}

checkinputAndCompute

  

#! /bin/bash

checkInt() {
arr=$1
for i in "${arr[@]}" ; do
temp=`echo $i | sed 's/[0-9]//g' | sed 's/[ ]*//g'`
if [ -n "$temp" ]; then
echo "$i must be integer"
return 1
fi
done
return 0
}

fuhao="" #判断符号
getFuhao() {
f=$(echo $1 | grep +)
if [ -n "$f" ]; then
fuhao="+"
fi
f=$(echo $1 | grep -)
if [ -n "$f" ]; then
fuhao="-"
fi
f=$(echo $1 | grep x)
if [ -n "$f" ]; then
fuhao="x"
fi
f=$(echo $1 | grep /)
if [ -n "$f" ]; then
fuhao="/"
fi
#echo "fuhao=$fuhao"
}

getLast() {
arr=$1
last=0;
case $fuhao in
"+")
last=$((${arr[0]} + ${arr[1]}))
;;
"-")
last=$((${arr[0]} - ${arr[1]}))
;;
"x")
last=$((${arr[0]} * ${arr[1]}))
;;
"/")
last=$((${arr[0]} / ${arr[1]}))
;;
esac
echo "${arr[0]} $fuhao ${arr[1]} = $last"
}

r="" #输入
checkinputAndCompute() {
while true; do
echo "please input two num to plus, eg: a +-x/ b"
read r
if [ -z "$r" ]; then
echo "cannot be null"
else
r=`echo "$r" | sed 's/\\*/x/'`
getFuhao $r
arr=(${r//$fuhao/ })
if [ ${#arr[@]} -ne 2 ]; then
echo "error input, eg: a +-x/ b"
else
if checkInt $arr; then
getLast $arr
fi
fi
fi
done
}

checkinputAndCompute



标签:arr,shell,last,fuhao,grep,整数,echo,计算器,fi
From: https://www.cnblogs.com/trump2/p/17723060.html

相关文章

  • 字符'1'和整数1的区别
    字符'1'和整数1的区别━━━━━━━━━━━━━━━━━━━━━━字符'1'是一个符号,在内存中以ASCII码对应的二进制00110001存放;整数1是一个数字,在内存中以数字1的二进制的补码00000001存放。......
  • How to use a shell script to check whether a command had been installed in the L
    HowtouseashellscripttocheckwhetheracommandhadbeeninstalledintheLinuxserverAllInOneerrors❌shellscripterror[::需要整数表达式shellscripterror[:-eq:需要一元表达式shellscripterror[:==:需要一元表达式#!/usr/bin/envbashif[[$(c......
  • HBASE shell学习
    一、基本命令打开HbaseShell:#hbaseshell1.1获取帮助#获取帮助help#获取命令的详细信息help'status'1.2查看服务器状态status1.3查看版本信息version关于表的操作2.1查看所有表list2.2创建表命令格式:create'表名称','列族名称1','列族名称2','列名称N'#创建......
  • Shell中如何分割字符串
    使用字符替换来分割字符串tr或者类似实现字符串替换的工具,如sed。echo"go:python:rust:js"|tr":""\n"#使用tr将分隔符:替换成换行符\n使用tr将分隔符:替换成换行符\n。使用cut分割字符串echo"go:python:rust:js"|cut-d":"-f1echo"go:pyth......
  • xshell终端中文显示乱码
    1.原因:xshell本地编码与远程服务器编码不一致,两边编码保持一致即可。2.处理方式:2.1调整本地编码(推荐)   通过命令 locale 查看服务器编码,LANG即为服务器编码。    在xshell中打开文件-属性-终端-编码,设置与服务器编码一致即可:   2.1调整服务器端编码......
  • shell批量执行命令与文件传输脚本
    shell批量执行命令与文件传输脚本需求:对未进行主机信任操作的服务器进行批量操作实现:由于ssh只能在交互模式中输入服务器密码进行登录登操作,不便于进行大批量服务器进行巡检或日志采集。sshpass恰好又解决了这个问题,使用ssh-ppasswd可以实现命令行输入密码操作,便于进行规模......
  • shell函数
    shell函数shell中允许将一组命令集合或语句形成一段可用代码,这些代码块称为shell函数。给这段代码起个名字称为函数名,后续可以直接调用该段代码的功能。1.函数的定义函数名(){函数体(一堆命令的集合,来实现某个功能)}function函数名(){函数体(一堆命令的集合,来实现......
  • How to print a string with a variable by using the echo command in the shell scr
    HowtoprintastringwithavariablebyusingtheechocommandintheshellscriptAllInOneNode.js&nvmquestionIdefinedashellvariableintheterminalandthenusedtheechocommandtooutputastringwiththatvariableanditworkedfine......
  • SHELL编程开发:如何轻松一键部署Nginx脚本
    实现思路下载Nginx源码包解压源码包进入解压后的目录配置编译选项编译并安装Nginx启动Nginx服务代码实现以下是脚本内容及注释:#!/bin/bash#一键部署Nginx脚本#下载Nginx源码包wgethttp://nginx.org/download/nginx-1.20.1.tar.gz#解压源码包tar-zxvfnginx-1.20.1.tar.......
  • 《Linux命令行与shell脚本编程大全.第3版》电子书PDF+源代码
    精通Linux命令行与shell脚本编程,尽在本书中本书是关于Linux命令行和shell命令的全面参考资料,涵盖详尽的动手教程和实际应用指南,并提供相关参考信息和背景资料,带你从Linux命令行基础入手,直到写出自己的shell。时隔四年后的这一版本,针对Linux的新特性和实践,进行了全面更新:使用......