简体   繁体   English

Linux bash '[: -ge: 一元运算符预期' 错误

[英]Linux bash '[: -ge: unary operator expected' error

#!/bin/bash


SubValue()
{


  romanvalue="${romanvalue}$2"
  decvalue=$(( $decvalue - $1 ))

}

if [ $decvalue -ge 1000 ] ; then
  SubValue 1000 "M"
elif [ $decvalue -ge 900 ] ; then
  SubValue 900 "CM"
elif [ $decvalue -ge 500 ] ; then
  SubValue 500 "D"
elif [ $decvalue -ge 400 ] ; then
  SubValue 400 "CD"
elif [ $decvalue -ge 100 ] ; then
  SubValue 100 "C"
elif [ $decvalue -ge 90 ] ; then
  SubValue 90 "XC"
elif [ $decvalue -ge 50 ] ; then
  SubValue 50 "L"
elif [ $decvalue -ge 40 ] ; then
  SubValue 40 "XL"
elif [ $decvalue -ge 10 ] ; then
  SubValue 10 "X"
elif [ $decvalue -ge 9 ] ; then
  SubValue 9 "IX"
elif [ $decvalue -ge 5 ] ; then
  SubValue 5 "V"
elif [ $decvalue -ge 4 ] ; then
  SubValue 4 "IV"
elif [ $decvalue -ge 1 ] ; then
  SubValue 1 "I"
fi

I tried this code and it gives many errors that我尝试了这段代码,它给出了许多错误

dectoroma.sh: line 13: [: -ge: unary operator expected dectoroma.sh: line 15: [: -ge: unary 
operator expected
dectoroma.sh: line 17: [: -ge: unary operator expected
dectoroma.sh: line 19: [: -ge: unary operator expected
dectoroma.sh: line 21: [: -ge: unary operator expected
dectoroma.sh: line 23: [: -ge: unary operator expected
dectoroma.sh: line 25: [: -ge: unary operator expected
dectoroma.sh: line 27: [: -ge: unary operator expected
dectoroma.sh: line 29: [: -ge: unary operator expected
dectoroma.sh: line 31: [: -ge: unary operator expected
dectoroma.sh: line 33: [: -ge: unary operator expected
dectoroma.sh: line 35: [: -ge: unary operator expected
dectoroma.sh: line 37: [: -ge: unary operator expected

could anyone help me to fix this problem please.谁能帮我解决这个问题。

The variable decvalue is not set.变量decvalue未设置。 You are using variables outside of double quotes then all the commands [ $decvalue -ge... ] become [ -ge... ] , thus the error.您在双引号之外使用变量,然后所有命令[ $decvalue -ge... ]变为[ -ge... ] ,因此出现错误。

You can debug your script using the -x (debug) and/or -v (verbose) option to the line #!/bin/bash -xv .您可以使用#!/bin/bash -xv行的 -x(调试)和/或 -v(详细)选项来调试脚本。 You'll then see and understand what happens.然后你会看到并理解会发生什么。 Instead, you can also call set -xv to enable debug locally then set +xv after the lines you want to debug.相反,您也可以调用set -xv在本地启用调试,然后在要调试的行之后set +xv

Using double quotes, the error message would be a bit more explicit:使用双引号,错误消息会更明确一点:

bash: [: : integer expression expected

The source of the problem reported in the question is an empty decvalue as syme has already stated.问题中报告的问题的根源是syme已经说明的空decvalue

Considering the purpose of the script, it seems you attempted to write a recursive function.考虑到脚本的目的,您似乎试图编写递归 function。 For this reason, i suggest you to follow the comment under your question by jww regarding debugging!出于这个原因,我建议您关注 jww 在您的问题下关于调试的评论!

Moreover, you are encouraged to search for questions on recursive functions in bash on SO.此外,我们鼓励您在 SO 上 的 bash 中搜索有关递归函数的问题。 There are excellent answers for factorial computations in bash. bash 中有很好的阶乘计算答案。

Think about which quantities are "transported" and which are returned/compiled in the end.想想哪些数量是“运输”的,哪些是最终返回/编译的。


If you are really stuck, you find 90% of the solution below.如果你真的被卡住了,你会在下面找到 90% 的解决方案。

#!/bin/bash
romanLetters(){
        local decvalue=$1
        local roman
        declare -i decvalue

        if [ $decvalue -ge 1000 ]; then
                romanvalue="M$(romanLetters $((decvalue - 1000)))"
                echo $romanvalue
        elif [ $decvalue -ge 900 ]; then
                romanvalue="CM$(romanLetters $((decvalue - 900)))"
                echo $romanvalue
        # ... and so on ...
        fi
}

echo "1900: $(romanLetters 1900)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM