If Statement Error in Bash

Bash if statement syntax error

The reason that this is a syntax error is that [ isn't part of the shell syntax; it's actually a command. Originally it was just a symlink to the test command. It still is, but it's also a built-in command in bash and other Bourne-derived shells.

if is a shell keyword, but the shell sees if[, not if. Because it didn't see an if, it doesn't know what to do when it sees then. (Actually, it knows exactly what to do: print a syntax error message.)

...

A bit of experimentation shows that it's not quite as simple as I thought it was. I tried creating a command called if[ and putting it in a directory in my $PATH. When I type just if[ at the prompt, the shell asks for more input. I actually don't know what it's looking for, but apparently the [ character is specially treated by the shell. The shell just doesn't split if[ into the if keyword and the [ command (as you might reasonably expect based on how other languages work). (If I really wanted to execute that command, I could type \if[ or "if[" -- or give it a sane name in the first place.

In any case, that last part probably doesn't matter; adding a space character will fix the problem.

if statement error in bash

Use if [ "$i" = "0" ]

In bash, you need spaces around [ and ] in if conditions

bash if statement gives syntax error in console, but works properly

You are attempting to test two numbers for equality using bash's arithmetic context. Let's simplify and observe error messages:

$ ((2==)) && echo yes
bash: ((: 2==: syntax error: operand expected (error token is "==")
$ ((==2)) && echo yes
bash: ((: ==2: syntax error: operand expected (error token is "==2")

The above match fairly closely the error messages that you observe.

The following, of course, works as expected:

$ ((2==2)) && echo yes
yes

It appears that, depending on which message you observed, that either the value of ${passes212[$i]} or ${passes256[$i]} is empty.

Let's try this again but using variables with or without assigned values:

$ x=2; y=""; (($x==$y)) && echo yes
bash: ((: 2==: syntax error: operand expected (error token is "==")
$ x=""; y=2; (($x==$y)) && echo yes
bash: ((: ==2: syntax error: operand expected (error token is "==2")

If the variable's value is empty, we get the same error messages as above when no variable was present at all. This would seem to confirm our diagnosis.

The solution is to make sure both arrays have assigned values.

Bash error on if statement

If $test is empty, the if becomes:

if [ "test" ==   ]; then

which normally raises the error:

-bash: [: test: unary operator expected

You need to enclose your variable in quotes in order for there to be a value when $test is empty:

if [ "test" == "$test"  ]; then

EDIT: There are two reasons why $test is "empty". The first reason is because | creates a subprocess, as Biffen correctly states. When the subprocess exits, all the variables that were set within it go away. In your case, it's not that $test is empty; it doesn't even exist. Not at the time you try to use it in if.

If you avoid the subprocess (which can be done in several ways), you might still have empty $test. The trivial way to avoid it is to write it not as pipe, but as process redirect:

while read test
do
echo $test
done < <(tail -1 demo.txt)

This will set the local variable test to the first line, it will display it, then it will set it to next line, but there is no next line; read fails, but not before it nukes $test (so $test ends up empty). That it really works as I say, you can check by running this:

while read test
do
realtest="$test"
echo $test
done < <(tail -1 demo.txt)

echo REALTEST: $realtest

and see that $realtest now does have the last line of the file (it would not have, if you tried the redirection approach).

However, if you just want to read a single line, why the while loop?

read test < <(tail -1 demo.txt)

Bash script IF and || creates syntax error

The problem you are dealing with here is a classic example of SC1083 - This {/} is literal. Check expression (missing ;/\n?) or quote it.

} is literal because it's not at the start of an expression. We fix it by adding a ; before it.

so add a ; just before } to indicate command-termination and double-quote all your variables as,

command-one || { echo "Something went wrong with Command-one at file  ${f}  !" >> ../corrupted.txt; } || error=1
command-two || { echo "Something went wrong with Command-two at file ${f} !" >> ../corrupted.txt; } || error=1
command-three || { echo "Something went wrong with Command-three at file ${f} !" >> ../corrupted.txt; } || error=1

Another would be to fix the comparison operator to

if [ $error -eq 0 ];

Error in Bash Shell Script IF Statement

You could use quotes and write "$(launchctl list ...)", but it's probably cleaner to do:

#!/bin/bash

if ! launchctl list | grep -q '^\d.*RemoteDesktop.*'; then
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -users admin -access -on -privs -all
else
exit 0
fi


Related Topics



Leave a reply



Submit