How to debug "syntax error in conditional expression" in Bash? - bash

I am writing a Bash script and I am new to it. When I run it I get this error:
./greet: line 14: syntax error in conditional expression
./greet: line 15: syntax error near `then'
./greet: line 15: ` then '
The lines of code its throwing on are these:
if [[ $hour -lt 0 || $hour -gt 23]]
then
echo "Please Enter a value between 0-23"
exit 1
fi
#
I have tried putting spaces after the "then" and rewriting it multiple times.

In the if statement, you need a space before ]]. The [[ and ]] have to be their own "words" as defined by the shell, so surrounded by whitespace.

Related

conditional binary operator expected error while runnng shell script

conditional binary operator expected error while running shell script.
//some code
# Parsing the input file
while IFS=' ' read -r f1 f2
do
key="$f1"
case $key in
-modules )
module="$f2"
;;
-full_run )
fullrun="$f2"
;;
-router )
router="$f2"
if [[ "$fullrun" == "yes" ]] && [[ $router" == "none" ]] ;
then
echo "If full_run is selected then router is mandatory"
exit 1
fi
;;
//some code
I am getting following error while running the above script
./firewall_scale_estimator_configure_PRexp.sh: line 80: conditional binary operator expected
./firewall_scale_estimator_configure_PRexp.sh: line 80: syntax error near `full_run'
./firewall_scale_estimator_configure_PRexp.sh: line 80: ` echo "If full_run is selected then router is mandatory
Can somebody explain what i am doing wrong here?

[: -!eq: binary operator expected

I have an issue with this Below Code
getting an error called :
[: -!eq: binary operator expected]
How do I fix this error
RC=$?
if [ $RC -eq 0 ]; then
echo File: $j Deletion Successfull >> $_Main/Status"_"$_date
else
echo File: $j Deletion Failed >> $_Main/Status"_"$_date
fi
The source of the error: [: –le: binary operator expected might be that you are using the unicode version of "–" instead of the regular "-".
Check your keyboard settings / mappings and try to use the normal "-".
You code is running perfectly well. However, if i change it like that :
#!/bin/sh
RC=$?
if [ $RC eq 0 ]; then
echo File:
else
echo File:
fi
I got the same error :
./test.sh: line 4: [: eq: binary operator expected
File:
This means you should check that you are using -eq and not anything else. The problem is clearly located into your condition.
In your case, seems that you tried to negate the equals condition with a !. This clearly won't work.

Bash and Functions

I am to write a basg=h script that displays a message if you input the correct number (i.e. 1 = function f1, 2 = function f2, and 3 = function f3)
My code is as follow
#!/bin/bash
function f1
{
echo "This message is from function 1"
}
function f2
{
echo "This messge is from function 2"
}
function f3
{
echo "This message is from function 3"
}
function=$(typeset -F)
declare -a myarr=(`echo "$function" [sed 's/declare[ ]-f / /g'`)
read -p "Enter a number (1, 2, or 3): " number
if ! [[ "$number" =~ ^[0-9]+$ ]]
then
echo "Not a valid number"
exit
fi
flag=0
for element in "${myarr[#]}
do
if echo "$element" | grep -q "$num"; then
$element
flag=1
fi
done
if [ "$flag" -eq 0 ]; then
echo "No function matches number $num"
fi
Now when I run the code I obtain the error
q6: line 43: unexpected EOF while looking for matching `"'
q6: line 45: syntax error: unexpected end of file
Any help sourcing the errors?
for element in "${myarr[#]}
Missing the end quote. You can catch errors like this by seeing where the syntax highlighting goes wonky. Notice how much of the script following this line is incorrectly colored red?
Even better, you can use ShellCheck:
Line 32:
for element in "${myarr[#]}
^-- SC1009: The mentioned syntax error was in this for loop.
^-- SC1078: Did you forget to close this double quoted string?
Fix that and you get:
Line 1:
#!/bin/bash
^-- SC1114: Remove leading spaces before the shebang.
Line 20:
declare -a myarr=(`echo "$function" [sed 's/declare[ ]-f / /g'`)
^-- SC2207: Prefer mapfile or read -a to split command output (or quote to avoid splitting).
^-- SC2006: Use $(..) instead of legacy `..`.
^-- SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.
Line 22:
read -p "Enter a number (1, 2, or 3): " number
^-- SC2162: read without -r will mangle backslashes.
Line 35:
if echo "$element" | grep -q "$num"; then
^-- SC2154: num is referenced but not assigned.

Not able to execute if condition in Cygwin terminal

Am new to unix,While executing below shell, am getting error as "unexpected token `fi'". am not sure why this error encounter though syntax are correct.
can any one help on this?
code:
#!bin/bash -xv
echo "this is test"
a=10
echo $a
if [a -gt 5]
than
echo "print"
echo $a+10
fi
O/P:
$ sh newsh.sh
this is test
10
newsh.sh: line 9: syntax error near unexpected token `fi'
newsh.sh: line 9: `fi'
The shell tokenization is white-space sensitive. You must use
if [ $a -gt 5 ]; then
do_something
echo $((a + 10))
fi
And it's #!/bin/sh; count your slashes...

Syntax error near unexpected token "(", indirect expansion variable declaration

In BASH,
I should note that the variables $Lambda0_List etc, are read from an input file earlier in the code.
PARAM_ARRAY=("Lambda0" "N" "M" "Sigma")
for i in "${PARAM_ARRAY[#]}"
do
List="$i"_List
Vary="$i"_Vary
Use_Range="$i"_Use_Range
Initial_Str="$i"_Initial
Final_Str="$i"_Final
Step_Str="$i"_Step
Initial=${!Initial_Str}
Step=${!Step_Str}
Final=${!Final_Str}
if [ "${!Vary}" == "T" ]
then
if [ "${!Use_Range}" == "T" ]
then
eval "$List=(`seq $Initial $Step $Final `)"
echo "$i : vary, use_range"
else
echo "$i: vary, use list"
fi
fi
done
Throws a syntax error
syntax error near unexpected token `('
Normally I'm able to interpret the error and find a solution, but I don't understand why the "(" is an unexpected token.
edit:
I've noticed that this line works if I run it in shell, but not in my script,
edit:
Fiddling around with the problematic line, I found that I get a syntax error even when its commented out!
/test.sh: line 266: syntax error near unexpected token `('
./test.sh: line 266: ######## eval "$List=(seq $Initial $Step $Final `)"'
After !Final you have a ) instead of a }
After sifting through some earlier code, I fixed some issue with ' vs ", and this error stopped coming. I'm new to BASH so I didn't expect that an error message with ')' to be caused by a quote 100 lines above.
why dont use elif or case ?
eval "$List=(seq $Initial $Step $Final)"
instead of
eval "${List=(seq $Initial $Step $Final)}" or eval "${List=seq $Initial $Step $Final}"

Resources