Bash script syntax error - bash

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. ;-)

Related

Comparing Numbers Two number in bash

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).

If condition is not working in bash

in the below code its directly going to else condition than if
#!/bin/bash
var=0
if [ "$var" -eq "0" ]
then
echo $var
else
echo $var
fi
This code demonstrably works exactly as given, with no changes whatsoever.
Putting it in a script called testscript, and running PS4=':$LINENO+' bash -x testscript (to print each command invoked preceded by the line number in the source file that it came from), one gets the following output:
:2+var=0
:3+'[' 0 -eq 0 ']'
:5+echo 0
0
Now, let's look at the line numbers in that trace, against the line numbers in the original source file:
#!/bin/bash # line 1
var=0 # line 2 -- :2+var=3
if [ "$var" -eq "0" ] # line 3 -- :3+'[' 0 -eq 0 ']'
then # line 4
echo $var # line 5 -- :5+echo 0
else # line 6
echo $var # line 7
fi
...and the fact that we went to line 2, line 3, and line 5 in that order means that we actually took the truth branch, not the else branch.
#!/bin/bash
var=0
if [ "$var" -eq "0" ]
then
echo $var
echo "Hello"
else
echo $var
echo "ELSE"
fi
It will print: 0 Hello
means it is going if loop only. Check once

if ["$i" -gt "$count"]; IS GIVING AN ERROR

I'm trying to put the 'f-$count'(f-1,f-2) name into the array.
Below is the code,
echo "Enter the count"
read count
echo $count
#arr=()
i=1
while true;
do
if ["$i" -gt "$count"]; then
exit 0
else
arr[$i]=f-$i
i=$((i+1))
fi
done
echo ${arr[#]}
I'm getting the error as 'script.sh: line 11: [3570: command not found
' continuously.
In shell programming, the brackets in the if MUST be delimited by spaces:
if ["$i" -gt "$count"]; then
MUST be:
if [ "$i" -gt "$count" ]; then
[EDIT] The left bracket ([) is actually a built-in shell command and so requires the space afterwards to delimit it from its parameters, as with any command.

Passing Argument in bash

I am having trouble to find the syntax error in the following script.
bash test.sh cat
#!/bin/bash
if [ $1 = "cat" ]; then
echo "valid"
else
echo "invalid"
fi
If you are not giving arguments, $1 will evaluate to a blank space and you are probably seeing line 2: [: =: unary operator expected. To fix, add quotes around $1:
#!/bin/bash
if [ "$1" = "cat" ]; then
echo "valid"
else
echo "invalid"
fi
This way, if you don't call with an argument it will still compare to an empty string.
In general, you should always put quotes around your variable expansions, otherwise you may see unexpected errors if the variable is empty (as you just saw) or if the variable has a space in it.
The arg $1 has no value. You could do something like this.
if [ -z $1 ]
then
echo "you forgot to give me an arg."
exit 1
fi
if [ $1 = "cat" ]; then
echo "valid"
else
echo "invalid"
fi
you can also do:
if [ $# -ne 1 ]; then
echo "Usage: ./script.sh <arg1>"
exit 1
fi

Shell script elif

I am new in shell script, trying to catch the return value of a program, and do something with it.
I have this script below
#!/bin/sh
if [ $# !=2 ] ; then
echo "Usage : param1 param2 "
exit 1;
elif [ $# -eq 2 ]; then
./callprogram
$out = $?
echo "$out"
fi
if [ $out==0 ]; then
echo "out ok"
fi
It keeps getting me error of
"[: 11: 0: unexpected operator
out ok
I have no clue why line 11 is wrong. if I remove "fi", it will promt that it needs "fi". Can anyone help with this matter?
Thank you
You need a space after the [ and you need to use -eq (equals) or -ne (not equals) to compare numbers in your if-statement.
To assign a variable use out=$?, not $out = $?. There should be no spaces on either side of the = sign.
Try this:
if [ $# -ne 2 ] ; then
echo "Usage : param1 param2 "
exit 1
elif [ $# -eq 2 ]; then
./callprogram
out=$?
echo "$out"
fi
if [ $out -eq 0 ]; then
echo "out ok"
fi
Change:
if [ $out==0 ]; then
to:
if [ $out = 0 ]; then
add spaces, and change '==' to '='. Note, that bash, executed as a bash accepts ==. But if you run is as a sh it will say "unexpected operator".
Why:
The [ is a command (or symlink to test binary, depending on your OS and shell). It expects $out and == and 0 and ] to be separate command arguments. If you miss the space around them, you have one argument $out==0.
BTW:
It's safer to always enquote the variables like that:
if [ "$var" ......
instead of
if [ $var
because when variable is empty, then you can get another error because of wrong number of arguments (no argument instead of empty string).
You have several problems. The one that is giving you the error is that you need a space after != on
if [ $# != 2 ]
(although -ne would be better than !=). It appears that you are calling the script with 11 arguments, and then calling [ with the arguments 11 !=2, and it does not know what to do with !=2 because you meant != 2 but forgot the space. Also, you want
out=$?
on the assignment (no $ on the LHS)
and
if [ $out = 0 ]
on the comparison. (Spaces around the operator, which is '=' instead of '=='. '==' will work on many shells, but '=' works in more shells.)
But your script would be better written without the explicit reference to $?
#!/bin/sh
if test $# != 2; then
echo "Usage: $0 param1 param2 " >&2 # Errors go to stderr, not stdout
exit 1;
fi
# you know $# is 2 here. No need to check
if ./callprogram; then
echo "out ok"
fi

Resources