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.
Related
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
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'
anothervar = 1
while [$anothervar -lt 1 ] do
read a
if [ 42 = $a ]; then
$anothervar = 2
else
echo $a
fi
done
get line 9: syntax error near unexpected token `done' error.
What did i do wrong ?
If you paste your shell script into ShellCheck you will see the following two shell script analysis messages for line 2 of your shell script:
You need a space after the [ and before the ].
Use semicolon or linefeed before 'do' (or quote to make it literal).
Your shell script after making the two corrections to line 2 suggested by the automated shell script analysis and changing the first line to anothervar=0 so that the commands inside the while loop can be executed is:
anothervar=0
while [ $anothervar -lt 1 ]; do # fixes 2 errors in this line
read a
if [ 42 = $a ]; then
anothervar=2
else
echo $a
fi
done
Alternatively:
anothervar=0
while [[ $anothervar -lt 1 ]]
do
read a
if [[ 42 = $a ]]
then
anothervar=2
else
echo $a
fi
done
Voila, no semicolons, and spaces in the variables don't bother you anymore. ;-)
I don't know where the problem is: when I execute this, I get an error:
./script.sh: line 4: if[ 7 -gt 5 ]: command not found
./script.sh: line 5: syntax error near unexpected token then'
./script.sh: line 5: `then'
#!/bin/bash
read a
read b
if[ $a -gt $b ]
then
echo "$a is greater than $b"
elif [ $a -lt $b ]
then
echo "$a is less than $b"
else
echo "$a is equal to $b"
fi
The syntax for if in bash follows:
if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
Note the space following if; it is mandatory. Note also that [ isn't any kind of special syntax; it's just a command, same as ls or grep. You can't type ifgrep, so you can't type if[ either.
Because if[ is not if, you weren't in an if block, so then was unexpected, thus your syntax error.
Thus:
if [ "$a" -gt "$b" ] # Correct
not
if ["$a" -gt "$b" ] # Wrong because of lack of space
or
if[ $a -gt $b ] # Wrong because of lack of space and lack of quotes
(Leaving the quotes out leaves you open to a completely separate set of bugs).
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 [