[ :Unexpected Operator in Shell Programming

Unexpected Operator error in shell script

if [ "$req" = 1 ]

or even better

if [ "$req" -eq 1 ]

See the syntax and operators in man test.

Shell script if statement [: unexpected operator

First, you have this tagged as bash, and it's bourne. As sigmalha pointed out, you are using the wrong operator. Use = instead of ==.

In addition, you are failing (exit 1) on a success, and succeeding (exit 0) on your failure.

Why am i getting an unexpected operator error in bash string equality test?

You can't use == for single bracket comparisons ([ ]). Use single = instead. Also you must quote the variables to prevent expansion.

if [ "$bn" = README ]; then

If you use [[ ]], that could apply and you wouldn't need to quote the first argument:

if [[ $bn == README ]]; then

Unexpected operator in if statement

The script you’ve posted has various issues, which are highlighted by ShellCheck:

Line 1:
echo "\nContinue downloading? [y/n]"
^-- SC2028: echo may not expand escape sequences. Use printf.

Line 2:
read CONTINUE
^-- SC2162: read without -r will mangle backslashes.

Line 5:
if [ $CONTINUE != "y" ] && [ $CONTINUE != "n" ]; then
^-- SC2086: Double quote to prevent globbing and word splitting.
^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
if [ "$CONTINUE" != "y" ] && [ "$CONTINUE" != "n" ]; then

Line 7:
elif [ $CONTINUE = "n" ]; then
^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
elif [ "$CONTINUE" = "n" ]; then

Line 8:
echo "\nDonwload terminated!"
^-- SC2028: echo may not expand escape sequences. Use printf.

But despite these issues the script actually otherwise works as expected on Debian (Buster)’s default shell (which is dash). You might be running a non-default shell. The easiest way to solve your issue is therefore to

  • Declare a valid shebang line
  • Fix the issues highlighted above.

Which leaves us with this:

#!/bin/sh

printf "\nContinue downloading? [y/n] "
read -r CONTINUE

error() {
printf >&2 '%s\n' "$@"
exit 1
}

if [ "$CONTINUE" != y ] && [ "$CONTINUE" != n ]; then
error "Invalid argument"
elif [ "$CONTINUE" = n ]; then
printf "\nDownload terminated!\n"
exit
fi

(This also adds a definition for the undefined error call; substitute as appropriate.)

Bash scripting unexpected operator

if [ "a" == "a" ] should be if [ "a" = "a" ].

bash accepts == instead of =, but your /bin/sh probably isn't bash.

So either change the == to =, or your shebang to #!/bin/bash

Unexpected operator - shell script in a Bamboo plan

The problem with single brackets is that the wildcards are expanded through the filename expansion (globbing). Consider this:

$ touch test{1,2,3,4}
$ set -x
$ [ "$(git rev-parse --abbrev-ref HEAD)" == *"test"* ] || [ "$(git rev-parse --abbrev-ref HEAD)" == *"develop"* ] && echo "yes"
$ [ "$(git rev-parse --abbrev-ref HEAD)" == *"test"* ]
++ git rev-parse --abbrev-ref HEAD
+ '[' develop == test1 test2 test3 test4 ']'
-bash: [: too many arguments

If you have double brackets available in your shell, then you might replace single brackets with double brackets:

[[ "$(git rev-parse --abbrev-ref HEAD)" == *"test"* ]] \
|| [[ "$(git rev-parse --abbrev-ref HEAD)" == *"develop"* ]] && echo "yes"

As an alternative, you might use case as follows:

case "$(git rev-parse --abbrev-ref HEAD)" in
*test*|*develop*)
echo 'yes'
;;
esac

Note, single opening bracket ([) is just a synonym for the test builtin with the only exception that the single opening bracket requires the last argument to be ].



Related Topics



Leave a reply



Submit