gpt4 book ai didi

Linux bash '[: -ge: unary operator expected' 错误

转载 作者:太空宇宙 更新时间:2023-11-04 04:50:47 32 4
gpt4 key购买 nike

#!/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

我尝试了这段代码,它给出了许多错误

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

谁能帮我解决这个问题。

最佳答案

问题中报告的问题根源是一个空的decvalue,如 syme已经说明了。

考虑到脚本的目的,您似乎试图编写一个递归函数。因此,我建议您关注 jww 的问题下有关调试的评论!

此外,我们鼓励您在 recursive functions in bash 上搜索问题就这样。 bash 中的阶乘计算有很好的答案。

想想哪些数量被“传输”,哪些数量最终被返回/编译。

<小时/>

如果您确实遇到困难,您可以找到下面 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)"

关于Linux bash '[: -ge: unary operator expected' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58467698/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com