Not able to execute if condition in Cygwin terminal - bash

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

Related

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

Mac Bash - doesn't recognize if[$#=0]

It's been a while since I've scripted in bash so I made a small script to test things out. This is my script (the quoted text is some Dutch, doesnt really matter):
#isingelogd
if[$#=0]
then
echo "Geef user-id's op!" 1>$2 ; exit 1
fi
for uid in $*
do
if who|grep $uid >dev/null
then
echo $uid is ingelogd
else
echo $uid is niet ingelogd
fi
done
If I try to run it it tells me the following:
bash-3.2$ ./isingelogd
./isingelogd
./isingelogd: line 3: if[0=0]: command not found
./isingelogd: line 4: syntax error near unexpected token `then'
./isingelogd: line 4: `then'
If I check my version with bash -v I'm running 3.2 which I thought supported square brackets.
Has someone had a similar problem and found solution?
Look at your errors:
bash-3.2$ ./isingelogd
./isingelogd
./isingelogd: line 3: if[0=0]: command not found
./isingelogd: line 4: syntax error near unexpected token then'
./isingelogd: line 4:then'
See that command not found? you have an error in your script.
The [..] are actual commands, and like all commands, they need to be separated by white spaces. The = is a parameter to the [ command and also needs to be surrounded by white space. Change line #3 to this:
if [ $# -eq 0 ]
Since $# and 0 are numeric, you should use -eq which compares to numbers and not = which compares strings.
Try these commands:
$ ls -li /bin/test
$ ls -li /bin/[
You'll see they have the same inode number. They're links. (Yes, the [ and test are builtins into the shell, but they are linked builtin commands).
$ man test
will give you all of the various tests that [ can do. Again, note the difference between -eq vs. = and -gt vs. >.
Note the following:
if [ 54 > 123 ]
then
echo "54 is greater than 123"
fi
This will print out "54 is greater than 123". This won't:
if [ 54 -gt 123 ]
then
echo "54 is greater than 123"
fi
a.bash works for me in Mac. Content of a.bash is the following :
#!/bin/bash
if [ $# == 0 ]; then
echo "Usage da da do"
fi
export A=$1
echo $A
then execute with the following :
\# ] ./a.bash
Usage da da do

BASH: Pattern match not working

I am using BASH 3.2. I can execute the following from the command line:
$ build_number=23332
$ if [[ $build_number != +([0-9]) ]]
> then
> echo "Bad Build Number ($build_number). No CPU time for you!"
> else
> echo "Build Number ($build_number) is numeric"
> fi
Build Number (2332) is numeric"
If I change build_number to23332a`, this returns:
Bad Build Number (23332a). No CPU time for you!
Now, I'll try to put this into my shell script:
#! /bin/bash
...
#
# Set options
#
while getopts :hu:j:b:p: option
do
case $option in
p) promotion_name="$OPTARG";;
u) jenkins_url="$OPTARG";;
j) job_name="$OPTARG";;
b) build_number="$OPTARG"
if [[ $build_number != +([0-9]) ]]
then
error "Build Number must be numeric"
fi
;;
h) printf "\n$USAGE\n"
exit 0;;
*) error "Invalid Argument";;
esac
done
shift $(( $OPTIND - 1 ))
When I try to execute my program, I get the following error:
$ ./promotion.sh -b 238d -jasdad
./promotion.sh: line 55: syntax error in conditional expression: unexpected token `('
./promotion.sh: line 55: syntax error near `+(['
./promotion.sh: line 55: ` if [[ $build_number != +([0-9]) ]]'
So, what am I doing wrong?
You need to enable extended globbing:
shopt -s extglob
well the most obvious thing is that the plus checks if the pre-ceeding character matches the pattern , here you have no pre-ceeding character put the plus after it

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