Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
应为bash一元运算符,can';I don’我看不出if语句有什么错_Bash - Fatal编程技术网

应为bash一元运算符,can';I don’我看不出if语句有什么错

应为bash一元运算符,can';I don’我看不出if语句有什么错,bash,Bash,我有一些bash代码,但它似乎不起作用,我不知道为什么。if语句有问题,但我无法理解 function param_err_non { echo "error: parameter missing" return 1 } function param_err_many { echo "error: too many parameters" return 1 } function param_err_invl {

我有一些bash代码,但它似乎不起作用,我不知道为什么。if语句有问题,但我无法理解

function param_err_non {
        echo "error: parameter missing"
        return 1
}

function param_err_many {
        echo "error: too many parameters"
        return 1
}

function param_err_invl {
        echo "error: invalid parameter"
        return 1
}

dbcmd=""
dbusr=""
dbste="" #this isn't so obvious, it's the site name
dbkey=""
dbval=""
nopar=0

if [ "$1" = "" ]; then param_err_non; fi

case "$1" in
        get)
                dbcmd=$1
                ;;                                                                                      
        put)                                                                                            
                dbcmd=$1                                                                                
                ;;                                                                                      
        del)                                                                                            
                dbcmd=$1                                                                                
                ;;                                                                                      
        new)                                                                                            
                dbcmd=$1                                                                                
                ;;                                                                                      
        list)                                                                                           
                dbcmd=$1                                                                                
                nopar=1                                                                                 
                ;;
        *)
                param_err_invl
esac

echo "running command $dbcmd"

if [ "$dbcmd" = "" ]; then param_err_non; fi

if [ "$2" = "" ]; then param_err_non; else dbusr=$2; fi

if [ $nopar -eq 0 ]; then
        if [ "$3" = "" ]; then param_err_non; else dbste=$3; fi
fi

counter=
for param in "$@"
do

        let counter=counter+1

        if [ "$dbcmd" = "del" ]; then
                echo "entering delete param check $counter"
                if [ "$3" = "" ]; then deleteall=1; else
                         if [ $counter -eq 4 ]; then param_err_many; fi
                fi
        fi

        if [ "$dbcmd" = "get" ]; then
                if [ $counter -eq 4 ]; then dbkey="$4"; fi
                if [ $counter -ge 5 ]; then param_err_many; fi
        fi

        if [ "$dbcmd" = "put" ]; then
                if [ $counter -eq 4 ]; then dbkey="$4"; fi
                if [ $counter -eq 5 ]; then dbval="$5"; fi
                if [ $counter -ge 6 ]; then param_err_many; fi
        fi

        if [ "$dbcmd" = "new" ]; then
                if [ $counter -eq 4 ]; then param_err_many; fi
        fi

        if [ "$dbcmd" = "list" ]; then
                if [ $counter -eq 4 ]; then param_err_many; fi
        fi
done
我得到的输出如下

$ source db-scripts/sitedb.bash del john thirdtest
running command del
entering delete param check 1
entering delete param check 2
entering delete param check 3
bash: [: =: unary operator expected
error: parameter missing

我真的需要一双额外的眼睛,因为问题中的if语句似乎一切都很好。

有一个中等的可能性,这个问题是许多类似于:

if [ $counter -eq 4 ]; then param_err_many; fi
修复方法是将变量用双引号括起来-
“$counter”
,或者将计数器初始化为0而不是空字符串

如果这不能解决问题,那么您应该在问题中出现错误的位置附近包含运行
bash-xyourscript.sh的调试输出。事实上,在将来,如果shell中出现错误,首先要做的就是使用
bash-x
运行它,看看哪里出了问题