Why am I getting the unary operator error?

I am writing a shell script to optimize the development workflow.

An argument arises as to which theme folder I will work in and starts the grunt watch in this directory.

If I call the script without the required argument, I am currently printing a warning that the topic should be specified as a command line argument.

I want to print a list of available options, for example. subject catalogs

This is what I still have ...

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

Ideally, I would replace the output of the echo string with the ls parent directory with themes, for example:

 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 
+7
bash shell
source share
3 answers

You need quotes around $THEME here:

 if [ $THEME == '' ] 

Otherwise, when you do not specify a topic, $THEME will not change to anything, and the shell will see this syntax error:

 if [ == '' ] 

With added quotes:

 if [ "$THEME" == '' ] 

extending the empty $THEME instead gives the correct comparison:

 if [ "" == '' ] 

This ability for runtime syntax errors can be surprising for those whose background is in more traditional programming languages, but command shells (at least those used in the Bourne tradition) are somewhat different from each other. In many contexts, shell parameters behave more like macros than variables; this behavior provides flexibility, but also creates traps for the reckless.

Since you noted this bash question, it is worth noting that the phrase is not executed as a result of expanding the parameter inside the "new" test syntax available in bash (and ksh / zsh), namely [[ ... ]] . So you can also do this:

 if [[ $THEME == '' ]] 

Places you can leave without quotes are listed here . But it’s a good habit to always indicate parameter expansions anyway, unless you explicitly want word breaks (and even then see if arrays will solve your problem instead).

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

 if [ -z "$THEME" ] 

Technically, you don't need quotes in this simple case; [ -z ] is true. But if you have a more complex expression, the parser will be confused, so it's best to use quotation marks. Of course, [[ ... ]] does not require quotes here:

 if [[ -z $THEME ]] 

But [[ ... ]] not part of the POSIX standard; in this respect == does not exist. Therefore, if you care about strict compatibility with other POSIX shells, stick to the quotation mark and use either -z or one = .

+24
source share

[ "$THEME" ] will be evaluated as false if $THEME is undefined or an empty string and true otherwise. See http://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html#Bash-Conditional-Expressions . You can modify the if statement to use this behavior and have an even simpler conditional:

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

"$THEME" must be in double quotes if its value contains spaces.

+2
source share

Please correct the syntax with double quotes.

  if [ "$THEME" == "" ] then echo 'Need to specify theme from the following' ls workspace/aws/ghost 
0
source share

All Articles