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

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}"

Related

how to check if input is number or alphabet in GNU bash

I am getting error "If" and then"" unexpected in GNU sed command.How to check input continuously.Please help me
if ! [[ "$versionCode" =~ ^[0-9]+$ ]]
then
echo "Sorry integers only"
fi
This is my error
sh: 1: Syntax error: end of file unexpected (expecting "then")
sh: 1: Syntax error: "then" unexpected
Sorry integers only
sh: 1: Syntax error: "fi" unexpected
update:
echo "Enter version code"
read versionCode
case "$versionCode" in
(*[!0-9]*) echo "Sorry integers only";;
("") echo "Empty is not a version code";;
(*) echo "do something with $versionCode";;
esac
echo "$versionCode"
sudo sed "s/\(versionCode[[:space:]]*\)[0-9]*/\1${versionCode}/" Version.gradle
this is my error
Enter version code
4
sh: 1: Syntax error: end of file unexpected (expecting ")")
sh: 1: Syntax error: word unexpected
sh: 1: Syntax error: word unexpected
sh: 1: Syntax error: word unexpected
sh: 1: Syntax error: "esac" unexpected
The POSIXly portable way to do this is
case "$versionCode" in
(*[!0-9]*) echo "Sorry integers only";;
("") echo "Empty is not a version code";;
(*) echo "do something with $versionCode";;
esac

How to debug "syntax error in conditional expression" in 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.

Dealing Bash Syntax Errors

I have my bash code.
#!/bin/bash
read message
echo $message | tr "A-Za-z" "N-ZA-Mn-za-m"
if [ $message == false ]; then
fi
done
It works but I get syntax errors after,
rot13.sh: line 6: syntax error near unexpected token `fi'
rot13.sh: line 6: `fi'
Now I don't expect a solution in this thread but I'm just wondering where should I go and that should I do when I get these errors? Is there any commands I can use in the compiler or documentation/checklists that check for syntax errors.
I am new to bash code so being able to check and understand there syntax error would help a lot.
Thank you.
#!/bin/bash
read message
echo "$message" | tr "A-Za-z" "N-ZA-Mn-za-m"
if [ "$message" = false ]; then
echo 'false'
fi
You need something between then and fi.
Before asking human help, always test your code on http://www.shellcheck.net/ before
And if you need to store the output of the tr command :
message="$(echo "$message" | tr "A-Za-z" "N-ZA-Mn-za-m")"
Last thing, you have a lost done statement at the end of the script

Syntax error near unexpected token "0"

Ok I know this has been posted a lot by bash newcomers but still, this is the program a teacher gave me, and it's not working properly.
#! /bin/bash
echo "month"
read month
case $month in
january)
echo "31" ;;
february)
echo "30" ;;
*)
echo "INVALID" ;;
esac
exit(0)
The terminal gives me two errors,
./shell2: line 15 : syntax error near unexpected token "0"
./shell2: line 15 : `exit(0)'
Could someone explain me one time for all?
Thank you
In bash parameters are not put in braces. It should be exit 0 instead of exit(0)

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

Resources