“unary operator expected” error in Bash if condition

前端 未结 4 1973
无人及你
无人及你 2020-11-27 09:23

I\'ve been trying to figure out whats wrong with this but just can\'t figure it out..

This is the part seems to be getting an error..

elif [ $operati         


        
4条回答
  •  广开言路
    2020-11-27 10:02

    You can also set a default value for the variable, so you don't need to use two "[", which amounts to two processes ("[" is actually a program) instead of one.

    It goes by this syntax: ${VARIABLE:-default}.

    The whole thing has to be thought in such a way that this "default" value is something distinct from a "valid" value/content.

    If that's not possible for some reason you probably need to add a step like checking if there's a value at all, along the lines of "if [ -z $VARIABLE ] ; then echo "the variable needs to be filled"", or "if [ ! -z $VARIABLE ] ; then #everything is fine, proceed with the rest of the script".

提交回复
热议问题