Bash if statement syntax error [duplicate] - bash

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 7 years ago.
I'm at a loss as to why this is giving a syntax error. Any thoughts?
#!/bin/bash
if [ `date +%H` -lt 11 ] ; then exit 0;
fi
if [ `date +%H` -gt 14 ] ; then
if[ `date +%H` -lt 20 ] ; then # <--- this line is the culprit, it seems
exit 0;
fi
fi
When run, I get:
./get.sh: line 7: syntax error near unexpected token `then'
./get.sh: line 7: ` if[ `date +%H` -lt 20 ] ; then '

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.

Add space before [
if [ `date +%H` -lt 20 ]

if[ `date +%H` -lt 20 ] ;
you need to place a space after if
if [ `date +%H` -lt 20 ] ;

Related

bash if statement command not found [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 months ago.
I have the simplest issue, but I can't get this to work, and it's driving me crazy. So I am running a script called execute.sh that has a help function. I swear my syntax is correct below; I have tried just one equal sign and now trying two. I have tried the double brackets and continue to get the error "[-h: command not found" . To execute the script I run the following the command.
sh execute.sh -h
Where -h is the argument I am passing int that forces the bash script to call the help function and then exist. But it keeps erroring on the IF Statement. Not sure what else to do, any suggestions?
arg=$1
if ["$arg" == "-h"]; then
helpFunction
exit1;
fi
In
["$arg" == "-h"]
line, you should separate the [ from " also " from the ] with a space. Because [ is actually a command, so it should have a space after it, and the syntax of [ command arguments require a space before ].
arg=$1
if [ "$arg" == "-h" ]; then
helpFunction
exit 1;
fi

bash while loop gives syntax error saying missing '[' [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 1 year ago.
I'm new to bash scripting and am having trouble trying to get this to work
local attempt=1
local success=false
while [[ "$attempt" -le "$retryAttempt" && "$success" -eq "false"]]; do
if ! [[ some condition here]];
then
echo true
return
fi
done
I'm getting an error on the while condition saying
line 10: [: missing ]
I cannot figure out what is wrong here, suggestions anyone?
Short: insert spaces around braces
Long: I wouldn't consider myself a bash pro (no idea what local is), but in a minimal syntax example (which is actually an infinity loop).
while [ $b -le $a] ; do
b=$a
done
gives me the same error. Changing the first line to
while [ $b -le $a ] ; do
works.
Depending on the complexity of the script, you might want to consider python or perl. In my opinion bash syntax can be a real pain in the a**. Especially when passing arguments with spaces through more than one level.

cygwin bash: unexpected EOF [duplicate]

This question already has an answer here:
Why is a shell script giving syntax errors when the same code works elsewhere? [duplicate]
(1 answer)
Closed 4 years ago.
What is the problem with the following bash code ?
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 {file}"
fi
Generates an error of "Unexpected EOF".
Cygwin 2.10.0(0.325/5/3) in Windows 10.
You have DOS line endings in your file, which means the bash parser sees
if [ -z "$1" ]; then\r
echo "Usage: $0 {file}"\r
fi\r
Rather than a complete if statement, it sees the beginning of one, one whose condition consists of (so far) the commands [ -z "$1" ], then\r, echo "Usage: $0 {file}"\r, and fi\r. The parser is still looking for the then keyword to terminate the condition list, but finds the end of the file instead.
Save your script as a POSIX text file using \n as the line endings, not \r\n.

command not found error triggered from if [ -gt ] comparsion

I was working though the examples here http://tldp.org/LDP/abs/html/debugging.html and would like some help explaining the internals of why this script throws the error [37: command not found
#!/bin/bash
# ex74.sh
# This is a buggy script.
# Where, oh where is the error?
a=37
if [$a -gt 27 ]
then
echo $a
fi
exit $? # 0! Why?
if [ $a -gt 27 ]
^
|
add space here
The reason for the spaces after [ is because [ is also not syntax. It is a simple command. Usually a builtin of the shell. The shell executes the command [ with the rest as parameters, including the ] as mandatory last parameter. If you do not put a space after [ the shell will try to execute [whatever as command and fail.
The reason for space before the ] is similar. Because otherwise it will not be recognized as a parameter of its own.

Test bash command line arguments

I don't know bash well but this seems pretty basic, yet I'm stuck on it. I'm using the bash installed on Mac OS X. I'm simply trying to test 1 command line argument and this is what I have and it doesn't work.
if [$1 -eq 'clean']
then
echo "Your argument is 'clean'!"
fi
Every time I've tried it, bash gives me a command not found error.
I'm obviously doing something wrong, what is it?
Couple of issues here:
Spaces around [ and ] are required in shell
-eq is used for comparing integers not for strings
Try this instead:
if [[ "$1" == "clean" ]]; then
echo "Your argument is 'clean'!"
fi
If you are using bash then [[ and ]] are more efficient than [ and ]

Resources