bash 语法错误:预期的操作数(错误标记是“>”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33721744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:54:16  来源:igfitidea点击:

syntax error: operand expected (error token is "> "

bashunix

提问by luke davis

./ex6.bash: ? 10: ((: > : syntax error: operand expected (error token is "> ")

And this is my code:

这是我的代码:

#!/bin/bash

printf "Input first number => "
read num1
printf "Input second number => "
read num2

num1=$a1
num2=$a2
if (( $a1>$a2 ))
then
    while [ $a1==$a2 ];
    do
        let "a1 = $a1 - 1"
        let "a2 = $a2 + 1"
        if (( $a1==$a2 ))
        then
            printf " $num2 ~ $num1 mid point : $a1 \n"
            break
        elif (( $((a1 -1))==$a2 ))
        then
            printf " $num2 ~ $num1 mid point : $a1 \n"
            break
        fi
    done
else
    while [ $a1==$a2 ];
    do
        let "a1 = $a1 + 1"
        let "a2 = $a2 - 1"
        if (( $a1==$a2 ))
        then
            printf " $num1 ~ $num2 mid point : $a1 \n"
            break
        elif (( $((a1 -1))==$a2 ))
        then
            printf " $num1 ~ $num2 mid point : $a1 \n"
            break
        fi
    done
fi

What's wrong and how do I fix it? I don't know what to do.

出了什么问题,我该如何解决?我不知道该怎么办。

回答by chepner

You never set a value for a1, so the arithmetic statement (($a1>$a2))expands to ((>)). Perhaps you meant a1=$num1instead of num1=$a1, but you don't need a1at all; you can just use $num1. The same holds for a2and num2.

您从未为 设置值a1,因此算术语句(($a1>$a2))扩展为((>))。也许您的意思是a1=$num1代替num1=$a1,但您根本不需要a1;你可以使用$num1. 这同样适用于a2num2