Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I encountered "unary operator expected" in a Bash script

In my Bash script, I have a function to return 0 or 1 (true or false) for the later main function's condition.

function1 () {
    if [[ "${1}" =~ "^ ...some regexp... $" ]] ; then
        return 1
    else
        return 0
    fi
}

Then in my main function:

main () {
    for arg in ${@} ; do
        if [ function1 ${arg} ] ; then
            ...
        elif [ ... ] ; then
            ...
        fi
    done
}

However, when I ran this script it always gave me an error message:

[: function1: unary operator expected

How can I fix this?

like image 759
GJ. Avatar asked May 14 '12 15:05

GJ.


People also ask

What is unary operator in bash script?

The word unary is basically synonymous with “single.” In the context of mathematics, this could be a single number or other component of an equation. So, when Bash says that it is expecting a unary operator, it is just saying that you are missing a number in the script.

Which unary operator is expected?

What is the Bash Unary Operator Expected error? It's an error that occurs when Bash identifies a line in your script that contains a binary operator that is not applied to two arguments.

How do bash scripts work?

A bash script is a series of commands written in a file. These are read and executed by the bash program. The program executes line by line. For example, you can navigate to a certain path, create a folder and spawn a process inside it using the command line.

How do you check if a variable is defined in bash?

To find out if a bash variable is defined: Return true if a bash variable is unset or set to the empty string: if [ -z ${my_variable+x} ]; Also try: [ -z ${my_bash_var+y} ] && echo "\$my_bash_var not defined"


1 Answers

You are making the common mistake of assuming that [ is part of the if command's syntax. It is not; the syntax of if is simply

if command; then
    ... things which should happen if command's result code was 0
else
    ... things which should happen otherwise
fi

One of the common commands we use is [ which is an alias for the command test. It is a simple command for comparing strings, numbers, and files. It accepts a fairly narrow combination of arguments, and tends to generate confusing and misleading error messages if you don't pass it the expected arguments. (Or rather, the error messages are adequate and helpful once you get used to it, but they are easily misunderstood if you're not used.)

In your main function, the call to [ appears misplaced.  You probably mean

if function "$arg"; then
    ...
elif ... ; then ...

By the way, for good measure, you should also always quote your strings. Use "$1" not $1, and "$arg" instead of $arg.

The historical reasons for test as a general kitchen sink of stuff the authors didn't want to make part of the syntax of if is one of the less attractive designs of the original Bourne shell. Bash and zsh offer alternatives which are less unwieldy (like the [[ double brackets in bash, which you use in your function1 definition), and of course, POSIX test is a lot more well-tempered than the original creation from Bell Labs.

As an additional clarification, your function can be simplified to just

function1 () {
    ! [[ "$1" =~ "^ ...some regexp... $" ]]
}

That is, perform the test with [[ and reverse its result code. (The "normal" case would be to return 0 for success, but maybe you are attempting to verify that the string doesn't match?)

like image 179
tripleee Avatar answered Sep 30 '22 12:09

tripleee