Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
970 views
in Technique[技术] by (71.8m points)

bash - Why am I getting a 'unary operator expected' error?

I'm writing a shell script to streamline my development workflow.

It takes an argument as to which theme folder I'm going to be working in and starts grunt watch on that directory.

If I call the script without the necessary argument I'm currently printing a warning that a theme needs to be specified as a command line argument.

I'd like to print a list of the available options, e.g. theme directories

This is what I have so far...

THEME=$1

if [ $THEME == '' ]
then
    echo 'Need to specify theme'
else
    cd 'workspace/aws/ghost/'$THEME'/'
    grunt watch
fi

Ideally I'd replace the output of the echo line with an ls of the themes parent directory like so

THEME=$1

if [ $THEME == '' ]
then
    echo 'Need to specify theme from the following'
    ls workspace/aws/ghost
else
    cd 'workspace/aws/ghost/'$THEME'/'
    grunt watch
fi

However this gives me the following error

./ghost_dev.sh: line 3: [: ==: unary operator expected
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need quotes around $THEME here:

if [ $THEME == '' ]

Otherwise, when you don't specify a theme, $THEME expands to nothing, and the shell sees this syntax error:

if [ == '' ]

With quotes added, like so:

if [ "$THEME" == '' ]

the expansion of an empty $THEMEyields this valid comparison instead:

if [ "" == '' ]

This capacity for runtime syntax errors can be surprising to those whose background is in more traditional programming languages, but command shells (at least those in the Bourne tradition) parse code somewhat differently. In many contexts, shell parameters behave more like macros than variables; this behavior provides flexibility, but also creates traps for the unwary.

Since you tagged this question bash, it's worth noting that there is no word-splitting performed on the result of parameter expansion inside the "new" test syntax available in bash (and ksh/zsh), namely [[...]]. So you can also do this:

if [[ $THEME == '' ]]

The places you can get away without quotes are listed here. But it's a fine habit to always quote parameter expansions anyway except when you explicitly want word-splitting (and even then, look to see if arrays will solve your problem instead).

It would be more idiomatic to use the -z test operator instead of equality with the empty string:

if [ -z "$THEME" ]

You technically don't need the quotation marks in this simple case; [ -z ] evaluates to true. But if you have a more complicated expression, the parser will get confused, so it's better to just always use the quotes. Of course, [[...]] doesn't require any here, either:

if [[ -z $THEME ]]

But [[...]] is not part of the POSIX standard; for that matter, neither is ==. So if you care about strict compatibility with other POSIX shells, stick to the quoting solution and use either -z or a single =.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...