Linux sh unexpected operator

Can’t Find Unexpected Operator (Bash Error)

Always protect your bash variables with double quotes in test expressions:

Unexpected operator error

You need to use = instead of == in the [ $? == 0 ] line.

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 you use [[ ]], that could apply and you wouldn’t need to quote the first argument:

ShellScript 1: [: [: unexpected operator

Don’t use the brackets at all.

if cmp -s 1.fsa 1_.fsa; then 
echo "Files are identical!"
else
echo "Files are different!"
fi

[ ] runs a command called test . If you read man test , you’ll see that it doesn’t understand «returns any» to mean anything at all; neither does it expect to see [ passed to it.

So, the literal error you got:

means that the [ command (aka test) is complaining that it doesn’t expect to be passed [ as an argument.

Unexpected operator from [ $var -eq 1 ]

This is one of those matter where quotes are important; when $USE_TEST is not defined, your statement expands to:

. though that would also imply switching to a string comparison, because -eq will break with an empty string:

But you may also consider using [[ (which is a bash builtin) or $ (which specifies a default value).

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

Читайте также:  Arch linux после установки

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.)

Amazon CLI bash script on Ubuntu 16.04, unexpected operator on if statements (syntax looks correct)

but I suspect it’s just a typo as it would prevent your error message from appearing.

You are probably running this as follows:

which means that the #!/bin/bash line is ignored, and either Bash in POSIX mode or another shell altogether is run. == is not POSIX, so you have to change

if [ $# -lt 1 ] && [ "$dayOfWeek" == "$weekly_backup_day" ]
if [ $# -lt 1 ] && [ "$dayOfWeek" = "$weekly_backup_day" ]

$ sh -c ‘[ 1 == 1 ] && echo Yes’ # Can’t have «= why quote», the summary is «because of word splitting and glob expansion». The Wooledge wiki has a good article about it.

Источник

/bin/sh: Odd string comparison error ‘unexpected operator’ [duplicate]

Found this error to be quite weird because previously my script was working and but after I moved it from the server I was working on to my local machine, it stopped working and just gave me an ‘unexpected operator’ error.

# Else if the script is being run in the arrayscripts directory, add /output/ . elif [ $basePath == "arrayscripts" ]; then echo "$dscr has started to run." cpuPath="`pwd`/output/cpu.binary" txtPath="`pwd`/output/cpu.txt" csvPath="`pwd`/output/cpu.csv" 

You are likely running this using a POSIX shell, probably dash (default sh in Ubuntu) which doesn’t support == . See Bashisms

If this shell is bash, the obvious problem is missing quotes. «$basePath» is not the same as $basePath — unquoted, the latter can expand to any number of arguments, making what it puts in the operator position completely unpredictable.

3 Answers 3

If your actual shell is /bin/sh [contrary to the initial question, but as discussion commentary has made clear], use = rather than == in your test expression:

elif [ "$basePath" = arrayscripts ] 

Note that the right-hand side doesn’t need to be quoted in this case, since it contains no expansions and no syntactically-sensitive characters.

Alternately, if this issue is reproducible when using bash, the obvious problem is missing quotes.

[ "$basePath" = arrayscripts ] # this is POSIX compatible 
[[ $basePath = arrayscripts ]] # this works only with bash 

Otherwise, the number of arguments $basePath expands into is undefined — it may expand into zero arguments, making the statement

. which would try to use = as a unary operator, which it isn’t.

or if $basePath contained, say, «true -o bar mt24″>

For completeness, I thought I’d mention that quoting won’t prevent [ = arrayscripts ] situations. What seems to be recommended is using [ «.$basePath» = .arrayscripts ] so that if basePath is undefined you’re left with [ . = .arrayscripts ].

@bobpaul, that claim is incorrect: [ «» = arrayscripts ] — which [ «$emptyvar» = arrayscripts ] evaluates to — is not a syntax error; in any POSIX-compliant shell, it correctly compares an empty string on the left to a non-empty string on the right.

Читайте также:  Arch linux build package

@bobpaul, . you can test this yourself: Run [ «$emptyvar» = arrayscripts ] , and note the lack of any syntax error reported to stderr.

This is an error you’d get if you were executing the script with a POSIX shell like dash . dash is the default /bin/sh on some platforms such as Ubuntu and Debian.

== is specific to bash ( Bashism ) and is not compatible with POSIX shells like dash , which uses only = to test string equality.

In the context of single brackets, == and = are treated as the same operator in bash , so either can be used.

Thanks for letting me know, updated the tags so I included Ubuntu which was the operating system I was using

Note that although bash supports == inside [ . ] , there’s no reason to actually do so. Use [[ . == . ]] instead.

I managed to get my script working by changing the comparison function from ‘==’ to ‘=’ as suggested by ‘alister’ in the unix and linux forums ( http://www.unix.com/shell-programming-and-scripting/141856-how-avoid-unexpected-operator-error-when-comparing-2-strings.html ) and so my script looked like this

# Else if the script is being run in the arrayscripts directory, add /output/ . elif [ "$basePath" = "arrayscripts" ]; then echo "$dscr has started to run." cpuPath="`pwd`/output/cpu.binary" txtPath="`pwd`/output/cpu.txt" csvPath="`pwd`/output/cpu.csv" 

Hope that if anyone gets this same error as I did, that this answer will help them. .

Источник

[ :Unexpected operator in shell programming [duplicate]

Cygwin has most likely aliased sh to bash . Some distributions don’t offer a true sh anymore. Although some will argue (and I tend to agree) that if you’re writing a script to be portable, write it in sh instead of bash .

Two mistakes: 1. use «=» not «==» for /bin/sh 2. doesn’t handle the empty string. Do $BLAH == yBLAH to fix that. So also this is technically not a duplicate question.

@personal_cloud, please don’t recommend the $BLAH approach — it’s much better to just quote, with «$choose» ; constant prefixes/suffixes haven’t been needed since the 1970s (as long as features marked obsolescent in the current POSIX test standard, like -a or -o , are avoided).

7 Answers 7

There is no mistake in your bash script. But you are executing it with sh which has a less extensive syntax

So, you’ll need run bash ./choose.sh instead, or convert the script to use POSIX compliant sh commands only, such as = between strings instead of == .

bash syntax is a superset of sh syntax — the /bin/sh executable on your system may provide only standard sh functionality, in which [] -style tests are not included.

Yes. They are completely different shells. Although, bash was based on and is largely backwards-compatable with sh, and they might actually be the same program on your system, but will still behave differently depending on which name you use. You can have the script run with bash automatically by changing the first line to #!/bin/bash and making the file executable, and just running ./choose.sh .

Читайте также:  Install linux binary file

This answer is partially wrong. The fully correct one is the now top-voted by Nietzche-jou. sh supports [ . == works in Bash built-ins [[ , test and [ while a single = is required by POSIX version of [ and test . (Bash manual says the same, BTW.) Requiring Bash because of this is needless harm to portability. Command name is [ or test , ] is just non-necessary, unused last parameter. Many shells implement these as built-ins, as Wikipedia says.

@Palec: while the answer is not as extensive as Nietzche-jou’s answer, I do not see what would be wrong about it? I never mentioned anything about brackets 🙂 +1 for your comment regardless!

@Wolph By writing partially wrong I meant two things. First, the problem is not in Bash vs sh syntax, both can call [ command correctly. It is in syntax of the [ command parameters, which is not sh’s business. Second, Bash is overkill for such a job, especially when a much simpler solution exists. This is not really wrong as it solves the problem too, but I think it is generally bad advice. This leads beginners to false conclusion that Bash solves their problems. It has many unportable extensions over POSIX-required features. I believe we should lead beginners to writing portable programs.

Источник

Unknown error in someone else’s code

Looking online, I know something is wrong with that first line. But none of the changes people have suggested fix it. Anyone have any ideas?

What «shebang» does the configure script have? == isn’t a POSIX supported comparison operator for the [ . ] test construct — it’s possible that the script was developed on a system where /bin/sh was a symbolic link to bash

So I’m not a coder or computer programmer or anything but «$USENETCDFPAR» is a string if I understand correctly. IF that string has nothing no value assigned to it won’t it break? :echo $USENETCDFPAR :(Nothing)

I don’t see why it would break anything — it’s perfectly valid to compare an empty string «» against a non-empty string «1» (the result will be false of course)

1 Answer 1

The «unexpected operator» here is really ==

In a POSIX shell, the test operator for string equality is = :

$ sh -c '[ "a" = "a" ] && echo equal' equal 
$ sh -c '[ "a" == "a" ] && echo equal' sh: 1: [: a: unexpected operator 

== is a provided as a synonym for = in the bash shell. From the CONDITIONAL EXPRESSIONS section of man bash :

 string1 == string2 string1 = string2 True if the strings are equal. = should be used with the test command for POSIX conformance. 

so probably the script’s developer worked on a system whose /bin/sh was a symbolic link to bash whereas on Ubuntu it is a symbolic link to the POSIX compliant dash shell.

You can either replace == by = inside the [ . ] test brackets, or change the script’s shebang line to #!/bin/bash

Источник

Оцените статью
Adblock
detector