What is a binary operator

5

I made a code in bash:

#!/bin/bash

echo "Digite o arquivo: "
read ARQUIVO
PROCURA=$(find /home/gabriel -name $ARQUIVO)
test $PROCURA -e & echo "O arquivo '$ARQUIVO' não foi encontrado"

And when you run it you get the following error message mentioning binary operators:

prompt$./testa-arquivos.sh 

Digite o arquivo: 
teste
O aruqivo 'teste' não foi encontrado
./testa-arquivos.sh: linha 6: test: Script/teste: esperado operador binário

How can I resolve this error, and I would also like to understand what binary operators are and what they are for.

    
asked by anonymous 16.05.2017 / 05:40

2 answers

6

Binary operator is one that receives two operands. And this can be a pain in Bash.

Something I usually do is check if one number is greater than or equal to another. For this, I use test with the gt operator:

if [ "$numero" -gt 2 ]; then
    ...
fi

Where can the main bottlenecks with Bash operators appear? Well, in addition to syntax with well-non-standard behaviors (from those who come from a more behaved world like C, Java, or Pascal), we have such variables expansions.

What are these expansions? Well, did you notice that I put quotation marks around $numero ? I did this with a single goal: if it does not have value in $numero , put the string empty.

Imagine a scenario where $numero has no value; when doing the variable expansion, my code would look like this:

if [ "" -gt 2 ]; then
    ...
fi

Now imagine that I had forgotten the quotes. My code would be expanded to the following:

if [  -gt 2 ]; then
    ...
fi

Note that in this second case, there is no operator on the left side of -gt , which causes a comparison error!

The other point I mentioned was about nonstandard syntax. If you are not careful, a short-circuit of commands may end up being called a routine in parallel or opening a pipeline.

For example, the shortcuts operator && indicates that I only execute the command on the right if the command on the left returns true (in the shell world, exit code 0) . The OR % shortcuts operator indicates that I only execute the command on the right if the command on the left returns || (in the shell world, exit code other than 0). >

So let's go to the parallel processing and pipeline operators. The false operator indicates that the command that proceeds must be executed in parallel; realize that a simple typo might turn AND & into this operator. The && operator will create a pipeline between the command on the left and the command on the right; note that it is easy to miss typing OR | and falling into the pipe.

Note that in your code, the || command is running on another thread because it is followed by test .

For more readings, check out the Shell Kilt Swiss Knife by Aurélio Verde

    
16.05.2017 / 13:22
1

Answering your question, in a simpler way.

Change the line:

test $PROCURA -e & echo "O aruqivo '$ARQUIVO' não foi encontrado"

By the section below:

if [  -e $PROCURA ]; then
echo 'O arquivo foi encontrado.'
else
echo 'O arquivo não foi encontrado'
fi

Well now let's talk about why the error message on the line, on your test line the -e (binary operator) came after the variable, it should come before, but even then your line would not work way you were, because you would need a framework that validates the test. The test just tests, it has no action, you need if to get the test result and make it a true or false condition.

Now comes the long explanation, but first let's speak command test. did you wonder why we are using [ and not writing test ?

Well, they are the same thing. In fact, there is 3 test command, a bash native, /usr/bin/test and /usr/bin/[ . But there are differences between them related to POSIX. It's pretty complex to explain these differences, but for now if your script gives you a problem with [ you'll have to use it in double-bracket form [[ .

Binary operators are those you determine at test time, such as -e (exist), -f (file), -d (directory), and so on. see man page to see all.

Now there's an extra hint about logical operators:

Use -a to "and" logico and -o to "or" logico, perhaps because of POSIX parameters the && and || operators are not interpreted by bash. But you can still use that double brace option [[ .

I hope it has become clear to you.

    
12.08.2017 / 21:01