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

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.

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

Formatting an if statement in bash [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
OK i am sure this is an easy question but I am having trouble finding the answer. All I am trying to do is have an if statement that says if $stla is greater than -25 or $stla less than -16. I am pretty sure it is just a syntax thing but I am new to bash and havn't had much luck Googling the answer.
I attached my whole script, the variable assignment seems to be working, so you should need to worry about that. If I disable the if statement and just have echo $stlo it will print every $stlo, so I think the variables are correct.
Right now I just have if ["$stla" -lt "-25"] which returns "line 10: [33.63: command not found" So it is treating $stla as a command and not a number. How do I correct that? And once that is corrected how can I make a or condition to include greater than -25?
Sorry for the simple question, let me know if I need to clarify anything.
#!/bin/bash
for file in *.SAC; do
stla="$(saclst stla f $file)"
stla=(`echo $stla | awk '{print $2}'`)
stlo="$(saclst stlo f $file)"
stlo=(`echo $stlo | awk '{print $2}'`)
if ["$stla" -gt "-25"]
then
echo $stlo
fi
done
This works for me
stla=-24
if [[ "$stla" -gt "-25" ]]
then
echo $stla
fi
Please note spaces in if statement, and fixed a typo in echo $stla
If You want to you float:
stla=-24.5
if [[ $(echo "$stla > -25"| bc) -eq 1 ]]
then
echo $stla
fi
You can use printf and tr to enable fixed-point comparisons:
if [ $(printf "%0.2f" "$stla"|tr -d .) -gt -2500 ]
then
echo "$stlo"
fi
This works by first converting $stla into an integer. Effectively, it multiplies $stla by 100 by printing it with two decimal places and then stripping the decimal point. You manually multiply the other operand to make the comparison maintain its original meaning.

Bash- if clause doesn't work correctly [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 5 years ago.
I want to make a shutdown script and it doesn't work as intended. This is what I wrote.
echo "Wanna shutdown (y/n)?"
read ANSWER
if [ $ANSWER=="y" ]
then
sudo shutdown -P now
else
printf "Something...."
whatever i press it just shuts down. Why?
You need to put spaces around the == operator. Otherwise the test expression is a single word, and all non-empty words test successfully.
In addition, if you wish to be portable, you should use = instead of ==. And it is always wise to double quote variable expansions because [ won't do that for you.
if [ "$ANSWER" = y ]; then
On the other hand, if you are using bash (or ksh or zsh) you could use the more forgiving [[ conditional expression:
if [[ $ANSWER = y ]]; then

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 ]

Bash if statement syntax error [duplicate]

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 ] ;

Resources