Shell 脚本 - Shell 变量不保留值

2024-01-11

#!/bin/bash

while true
do
 if [[ $# -eq 0 ]] ; then

    result=$((operand1+operand2))
    result=$((operand1-operand2))
    result=$((operand1*operand2))

     if [[ $result ]] ; then 
        operand1=$result 
        echo "operand 1 is: $result" 
        echo "" 
     else 
        echo Enter operand1 value: 
        read operand1 

     fi

     # Offer choices
     echo 1. Addition
     echo 2. Subtraction
     echo 3. Multiplication
     echo 4. Division
     echo 5. Exit


     echo Enter your choice:
     read choice


     if [[ $choice != 1 && $choice != 2 && $choice != 3 && $choice != 4 && $choice != 5 ]] ; then
         echo Sorry not a valid operator - please try again 
         echo Enter your choice:
         read choice

     fi

     echo Enter operand2 value:
     read operand2

     # get operands and start computing based on the user's choice
     if [[ $choice -eq 1 ]] ; then

         echo ----------------------------------------
         echo Addition of $operand1 and $operand2 is $((operand1+operand2))
         echo ----------------------------------------
         echo

     elif [[ $choice -eq 2 ]] ; then

         echo ----------------------------------------
         echo Subtraction of $operand1 and $operand2 is $((operand1-operand2))
         echo ----------------------------------------
         echo
     elif [[ $choice -eq 3 ]] ; then

         echo ----------------------------------------
         echo Multiplication of $operand1 and $operand2 is $((operand1*operand2))
         echo ----------------------------------------
         echo
     elif [[ $choice -eq 4 && operand2 -eq 0 ]] ; then
         echo Can not divide by 0 please try again 
         echo Please enter operand2
         read operand2
         echo ----------------------------------------
         echo Division of $operand1 and $operand2 is $((operand1/operand2))
         echo ----------------------------------------
         echo

      elif [[ $choice -eq 4 && operand2 -ne 0 ]] ; then
         echo ----------------------------------------
         echo Division of $operand1 and $operand2 is $((operand1/operand2))
         echo ----------------------------------------
         echo

     elif [[ $choice -eq 5 ]] ; then
         exit

     else
         echo ----------------------------------------
         echo Invalid choice.. Please try again
         echo ----------------------------------------
         echo
     fi

   else
         echo ----------------------------------------
         echo You either passed too many parameters or too less
         echo than the optimum requirement.
         echo
         echo This program accepts a maximum of 2 arguments or no
         echo argument at all in order to run successfully.
         echo ----------------------------------------
   fi
done

我正在专门研究这段代码的顶部部分。我试图拥有它,以便用户的结果将延续到下一个操作数1。每次计算后我得到 operand1 = 0 。知道如何解决这个问题吗? 操作数 1 为:0

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice:
1
Enter operand2 value:
1
----------------------------------------
Addition of 0 and 1 is 1
----------------------------------------

operand 1 is: 0

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice:


这是减少重复代码的方法。注意使用select展示菜单。

#!/bin/bash

PS3="Enter the operation: "     # for the select statement

operand() {
    read -r -p "Enter operand${1} value: "
    echo "$REPLY"
}

operand1=$(operand 1)

while true; do
    select operation in Addition Subtraction Multiplication Division Exit; do
        case $operation in
            Exit) exit ;;
            Addition)       op="+"; break ;;
            Subtraction)    op="-"; break ;;
            Multiplication) op="*"; break ;;
            Division)       op="/"; break ;;
        esac
    done

    while true; do
        operand2=$(operand 2)
        if [[ $operation == "Division" ]] && (( operand2 == 0 )); then
            echo Can not divide by 0 please try again 
        else
            break
        fi
    done

    (( result = operand1 $op operand2 ))

    echo "----------------------------------------"
    echo "$operation of $operand1 and $operand2 is $result"
    echo "----------------------------------------"

    echo "operand 1 is: $result" 
    operand1=$result
done
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Shell 脚本 - Shell 变量不保留值 的相关文章

  • 使用 awk 处理多个文件

    我必须使用 awk 处理大量 txt 文件 每个文件 1600 万行 我必须阅读例如十个文件 File 1 en sample 1 200 en n sample 2 10 en sample 3 10 File 2 en sample 1
  • 为什么我的信号处理程序只执行一次?

    我正在 UNIX 和 C 中处理信号 并遇到了这个问题 我正在尝试编写一个计数到 10 的程序 每秒一个数字 当用户尝试使用 SIGINT 如 CTRL C 中断它时 它会打印一条消息 告诉它无论如何都会继续计数 到目前为止 我得到了这个
  • 从 Jenkins Pipeline 捕获 shell 脚本输出

    我正在尝试提取 git 分支并在 Jenkinsfile 中提交信息 如下所示 def commit sh returnStdout true script git rev parse HEAD trim def branch sh ret
  • 如何使用 netcat 发送文件并保持连接?

    我发出命令 netcat serveraddress myport lt MY FILE 问题是 一旦文件发送完毕 netcat 就会发送一条消息来关闭连接 发送该文件后 我需要从控制台写入消息 我记得对标准输入做了一些事情 是这样的吗 n
  • 为什么符号链接的权限默认为所有许可?

    当我使用创建硬链接时ln ln testfile txt testfile2 txt 文件硬链接 通过 inode 表中两个别名的相同 inode 编号进行确认ls li 对硬链接文件具有与原始名称文件相同的初始权限 1222285 rw
  • PHP 中是否有相当于 subprocess 的东西?

    在 Java 和 Python 中 你有ProcessBuilder or 子流程 https docs python org 2 library subprocess html可让您使用未转义字符串轻松启动进程的模块 例如 ls some
  • linux + ksh + 向下舍入或向上舍入 - 浮点数

    在我的 ksh 脚本中 我只需要计算整数 有时我会得到浮点数 例如 3 49 或 4 8 等 所以我需要根据以下规则将浮点数转换为整数 示例 3 49 will be 3 2 9 will be 3 4 1 will be 4 23 51
  • 如何使用 echo 命令写入并附加到文件

    我正在尝试编写一个脚本 它将使用 echo 并写入 附加到文件 但我的语法中已经有了 字符串 说 echo I am Finding difficult to write this to file gt file txt echo I ca
  • 拼写检查 shell 脚本

    我有一些疑问 我对一个应该是简单拼写检查器的脚本有疑问 它的目的是 当发现错误的单词时 它会提示用户输入该单词的正确拼写 如果用户输入正确的拼写 则会显示更正的单词以及错误的单词 下面 在读完所有单词之后 但是 如果用户只是按 Enter
  • 如何同时正确使用管道和信号?

    我有 2 个孩子 我想将信号从孩子发送到父母 并将答案 随机数 为什么 为什么不 命名管道从父母发送到每个孩子 我有这个代码 include
  • Ansible - 当至少一项在循环中失败时跳过任务

    我正在使用 sqlplus 运行一些 SQL 脚本 在运行之前 我从该目录获取所有 sql 文件列表并将其存储在sql out如下所示 问题是 如果其中一个 sql 脚本失败 其余 sql 脚本仍然会执行 如果任何一个脚本失败 我想完全跳过
  • 基于 Unix ASCII 的命令行图表/绘图工具

    有没有好的命令行 UNIX 图表 绘图 绘图工具 我正在寻找能够在 ASCII 图表上绘制 xy 点的东西 澄清一下 我正在寻找能够以 ASCII 格式输出图形 如 ascii art 风格 的东西 这样我就可以在交互式 shell 会话中
  • 有哪些基于对象的 shell?

    我打算写一个面向对象的shell 基于Python 我已经有很多想法了 但在实现它之前 我想通过一些现有的 shell 来激发我的灵感 我所说的面向对象的基本意思是 参数不仅仅是字符串数组 而且是对象数组 返回值也是一个对象 不仅有 std
  • 如何使用 git hook pre-merge-commit 获取原始合并分支名称

    我正在尝试使用新的 git hook pre merge commit 创建一个特定的脚本 但它没有参数 有什么解决方法可以让我获得正在合并的分支的名称吗 例子 在分支 myBranch 上 我调用 git merge testingBra
  • 类似 jq 中的 sql join

    我有以下 json id 1 type folder title folder 1 id 2 type folder title folder 2 id 3 type item title item 1 folder 1 id 4 type
  • 将 JSON 导出到环境变量

    如果我有这样的 JSON hello1 world1 testk testv 我想将每个键值对导出为环境变量 如何通过 shell 脚本来做到这一点 例如 当我在终端上写时 echo hello1 world1应该打印其他键值对吗 注意 上
  • 如何在 Python 中包含 PHP 脚本?

    我有一个 PHP 脚本 news generator php 当我包含它时 它会抓取一堆新闻项并打印它们 现在 我在我的网站 CGI 中使用 Python 当我使用 PHP 时 我在 新闻 页面上使用了这样的内容 为了简单起见 我删掉了这个
  • 构建自动化和 MySQL Workbench 脚本:正向工程师 SQL CREATE SCRIPT

    我目前正在研究自动化软件构建过程 其中包括 MySQL Workbench 中定义的数据库架构 使用 Workbench 的脚本编写功能 我想打开一个 Workbench 文档并将其架构导出为 SQL CREATE 脚本 我想知道是否有一个
  • 运行具有外部依赖项的 Scala 脚本

    我在 Users joe scala lib 下有以下 jar commons codec 1 4 jar httpclient 4 1 1 jar httpcore 4 1 jar commons logging 1 1 1 jar ht
  • 执行命令而不将其保留在历史记录中[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 在进行软件开发时 经常需要在命令行命令中包含机密信息 典型示例是将项目部署到服务器的凭据设置为环境变量 当我不想将某些命令存储在命令历史记

随机推荐