如果否则判断魏德错误

发布于 2025-02-06 19:29:52 字数 954 浏览 0 评论 0原文

无法运行代码,错误“ [[add:“找不到)。


#!/bin/bash

add() {
    first="${1}"
    second="${2}"
    result=`expr $first + $second`
    echo  "The sum of two numbers are $result"  
    return $result
}


subtract() {
        first="${1}"
        second="${2}"
        result=`expr $first - $second`
        echo  "The sum of two numbers are $result"
    return $result 
}


echo "[0] Add"
echo  "[1] Subtract"

read operation

echo "Enter Number One"
read first
echo "Enter Number Two"
read second

if [["$operation" == "Add"]]
then
    add $first $second
else
    subtract $first $second
fi

我正在尝试运行此代码,如果其他比较不起作用。每次我执行代码时,在第32行中都会发生错误并执行。我正在尝试创建一个加法/减法bash sh文件。

root@Kumaraswamy:~# ./maths.sh
[0] Add
[1] Subtract
Add
Enter Number One
7
Enter Number Two
3
./maths.sh: line 32: [[Add: command not found
The sum of two numbers are 4
root@Kumaraswamy:~#

Unable to run code, error "[[Add:" not found.


#!/bin/bash

add() {
    first="${1}"
    second="${2}"
    result=`expr $first + $second`
    echo  "The sum of two numbers are $result"  
    return $result
}


subtract() {
        first="${1}"
        second="${2}"
        result=`expr $first - $second`
        echo  "The sum of two numbers are $result"
    return $result 
}


echo "[0] Add"
echo  "[1] Subtract"

read operation

echo "Enter Number One"
read first
echo "Enter Number Two"
read second

if [["$operation" == "Add"]]
then
    add $first $second
else
    subtract $first $second
fi

I am trying to run this code, the if else comparison does not work. Each time i execute the code, at line 32 an error is occurred and else is executed. I am trying to create an addition/subtraction bash sh file.

root@Kumaraswamy:~# ./maths.sh
[0] Add
[1] Subtract
Add
Enter Number One
7
Enter Number Two
3
./maths.sh: line 32: [[Add: command not found
The sum of two numbers are 4
root@Kumaraswamy:~#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2025-02-13 19:29:52

之后留出空间[[[。这应该起作用,因为这些是在环境变量$ path下列出的目录之一中找到的真实可执行文件。

Leave a space after [ or [[. That should work since these are real executables found in one of the directories listed under environment variable $PATH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文