In case that you have this error during execution of your bash script, there is issue in your if statement.
Lets see how this works on example. We have simple bash script called 123.sh.
server~# cat 123.sh
#!/bin/bash
a=`ls|wc -l`
if [ $a == 0 ] then
echo 0
else
echo 1
fi
server:~# ./123.sh
./123.sh: line 5: syntax error near unexpected token `else'
./123.sh: line 5: `else'
As you can see, bash is telling as that we have syntax error in line 5. But if you look in line 5 there are just else command and there is no way this is wrongly written. So problem has to be somewhere above.
For this particular error problem is in location of then. In line where is if statement we have then statement and this is reason why this don't work. Syntax should be like this
if [ conditions ]
then
.
.
else
.
.
fi
So if we change that like, script will work!
server:~# cat 123.sh
#!/bin/bash
a=`ls|wc -l`
if [ $a == 0 ]
then
echo 0
else
echo 1
fi
server:~# ./123.sh
1
Lets see how this works on example. We have simple bash script called 123.sh.
server~# cat 123.sh
#!/bin/bash
a=`ls|wc -l`
if [ $a == 0 ] then
echo 0
else
echo 1
fi
server:~# ./123.sh
./123.sh: line 5: syntax error near unexpected token `else'
./123.sh: line 5: `else'
As you can see, bash is telling as that we have syntax error in line 5. But if you look in line 5 there are just else command and there is no way this is wrongly written. So problem has to be somewhere above.
For this particular error problem is in location of then. In line where is if statement we have then statement and this is reason why this don't work. Syntax should be like this
if [ conditions ]
then
.
.
else
.
.
fi
So if we change that like, script will work!
server:~# cat 123.sh
#!/bin/bash
a=`ls|wc -l`
if [ $a == 0 ]
then
echo 0
else
echo 1
fi
server:~# ./123.sh
1
No comments:
Post a Comment