Bash script - syntax error near unexpected token `then' - bash

I have this simple bash script that keeps failing with the following messages:
./servo.sh: line 7: syntax error near unexpected token then'
./servo.sh: line 7: if[ "$level" -eq 1 ]; then'
And my bash script:
#!/bin/bash
level=1
while :
do
if[ $level -eq 1 ]; then
echo "hello"
else
echo "else"
fi
done
What am I doing wrong?

add a space between the if and the [

Related

how to execute if statement line by line in bash

how to execute if statement line by line in bash??
for i in $(cat $install_script)
do
if [ "$i" = "$check" ]; then
count=$((count+1))
echo whiptail --title "Install SmartBoard" --yesno "${scripts_name[$count]}" 7 $((${#scripts_name[$count]} + 5))
if (( $? == 0 )); then
skip=false
else
skip=true
fi
fi
if [ "$i" == "$check wifi" ]; then
break
fi
if [ $skip == true ]; then
continue
else
sh -c $i ## process line by line
fi
done
error !!
sh: 1: Syntax error: end of file unexpected (expecting "then")
sh: 1: Syntax error: "then" unexpected
sh: 1: Syntax error: "else" unexpected
sh: 1: Syntax error: "fi" unexpected
i'm not good english so if my posts word be rude Sorry that. :(

Bash script greater than operator gives syntax error

I googled a lot and found it because of the spaces within the if condition but then even after resolving that, it still gives me some errors as I've pasted below. Just beginning to learn bash scripts any help would be useful. Thanks.
#!/bin/bash
msg=3;
if[ $msg -gt 0 ]
then
echo $msg;
fi
ERROR
line 3: if[ 3 -gt 0 ]: command not found
line 4: syntax error near unexpected token `then'
line 4: `then'
You missed a space after if:
if [ $msg -gt 0 ]
To better format your code:
msg=3
if [ $msg -gt 0 ]; then
echo $msg;
fi

Getting an error when I nest a for-loop inside an if-else statement

I am trying to write a simple bash script with a for-loop inside an if-else statement. The following code works:
if [ $# -le 0 ]; then
echo 'No arguments provided.'
else
echo "Arguments detected."
fi
But as soon as I replace the second echo with a for-loop, it breaks:
if [ $# -le 0 ]; then
echo 'No arguments provided.'
else
for i in "$#"
do
echo "Arguments detected: $i"
done
fi
I've tried to execute only the for-loop and I'm given an error, so I think the for-loop is wrong somehow, but I don't know how. The error is:
bash: C:/Users/Angleton/.bashrc: line 45: syntax error near unexpected token `fi'
bash: C:/Users/Angleton/.bashrc: line 45: ` fi'

bash script error unexpected token error

if [ $USER=root ]; then echo "hi" else echo "bye" fi
it is giving me the following error
line 5: syntax error near unexpected token `fi'
line 5: `fi'
The correct code:
if [ $USER == "root" ]; then echo "hi; else echo "bye"; fi

bash: syntax error near unexpected token `done'

Can anyone point out the error in the following bash script
for i in {0..127} ; do echo -n [$i] ; if [$i -eq 7]; then echo "\n" ; mytool-c "read 0x1540:0xa0:$i*$j" ; done
You are missing spaces in the if line. It needs to be [ $i -eq 7 ]. ([ is a command.)
But your error is that you are missing the closing fi for the if.

Resources