cannot solve syntax error: operand expected (error token is "+") - bash

getting this error, where my code is:
#!/bin/bash
# argument passed to script (or any other source if needed like intern to script)
file=$1
rs=$2
clin_sig=$3
mut_type=$4
pos=$6
allele=$7
chain_pos=$8
abs_pos=$(($pos+$chain_pos))
echo $abs_pos
where command line is:
./program.sh 1 1 1 1 1 1 1
./program.sh: line 11: 1+: syntax error: operand expected (error token is "+")
similar questions have been solved using $(()) to do arithmetic, this is not working for me.

use default value if variable is empty
e.g:
pos=${6:-0}
chain_pos=${8:-0}

inside $((…)) you omit the $ in front of variable names:
abs_pos=$(( pos + chain_pos ))

Related

Shell Script : shortcut add and assignment += : command not found

Using Mac Terminal and Shell Script
Trying to do Shortcut of add and assignment operator like
SumVar=1
$(( SumVar += 5 ))
echo $SumVar
Getting error :
Error: line 3: 5: command not found
What is the correct syntax of this ?
Your syntax for incrementing the variable is correct, but you are using it in a context where Bash wants a command, and it complains that the result of the increment (4) is not a recognized command.
The let keyword is your friend.
let SumVar+=4
Or better yet just leave out the dollar sign (thanks #chepner);
(( SumVar += 4 ))
When you use a substitution like this, bash will try to execute a command of the substituted name/value. You can use this instead:
(( Sumvar += 5 ))
Or if you really insist on using substitution here, you can use it as an argument to the : command:
: $(( Sumvar += 5 ))

subtraction in bash doesn't work

I have the following problem with a bash script:
validParameters=0
argumentLength=${#1}
argumentLength==$((argumentLength - 1))
#[code to increment validParameters]
if [[ $validParameters != argumentLength ]]
then
diff=$((argumentLength - validParameters))
printf "Attention:\n$diff invalid argument(s) found!\n"
fi
exit 1
The error happens in the line: diff=$((argumentLength - validParameters))
=3: syntax error: operand expected (error token is "=3")
with the command script.sh abc
If I set diff to a fixed value (e.g. diff=1) instead of the subtraction, the script works perfectly.
Is my subtraction syntax somehow wrong?
argumentLength==$((argumentLength - 1))
You've got two =s here. It's equivalent to:
argumentLength="=$((argumentLength - 1))"
That's why the error message says =3.
Sounds like one of the variables argumentLength and validParameters did not store a number, but something including the string =3.
For debugging, try to print both variables before subtracting them.
By the way, you can write ((diff = argumentLength - validParameters)).
Edit after your edit: Found the Bug
There is one = too much in
argumentLength==$((argumentLength - 1))
write
argumentLength=$((argumentLength - 1))
or
(( argumentLength-- ))
instead.

bash - math multiple integer with float type

In bash,I am trying to do math with a integer and a float number to get a integer result. below code snippet doesn't work:
x=25
y=0.2
z=$((x*y))
echo $x*$y=$z
The error message is:
sh: line 3: 0.2: syntax error: invalid arithmetic operator (error token is ".2")
If both variable is integer, it works fine.
How can I get "25*0.2=5" from bash script?
Place your printout in quotes in echo. Also your z=$((x*y)) will make z empty or error:
25*0.2: syntax error: invalid arithmetic operator (error token is
".2")
So... Here is tested code and might be like this:
x=25
y=0.2
z=$(echo $x*$y | bc)
echo "$x*$y=$z"
result will be like this:
25*0.2=5.0
Note: we used bc command for z calculation
Try bashj (a bash mutant with java support) https://sourceforge.net/projects/bashj/.
for instance:
#!/usr/bin/bashj
echo Math.cos(0.5)
echo Math.hypot(3.0,4.0)

Bash script operand expected

I'm having a small problem with this following snippet and I'm not sure why. The error given is (line indicated):
*2: syntax error: operand expected (error token is "*2")
while [[ $numberServers -gt $newindex ]]; do
serverPort=$((9001+$(($newindex*2)))) <--- This line
clientPort=$(($serverPort+1))
newindex=$(($newindex+1))
localhostport=$((serverPort-2))
string=$(($string,localhost:$(($serverPort-2))))
...
Any help would be greatly appreciated.
The problem is that the variable newindex is empty, so the expression became:
$((9001+$((*2))))
check the initialization of newindex.
Example:
$ echo $((9001+$(($newindex*2))))
bash: *2: syntax error: operand expected (error token is "*2")
$ newindex=4
$ echo $((9001+$(($newindex*2))))
9009

Bash script, check numeric variable in in range

This is my script:
#!/bin/bash
JOB_NUM=4
function checkJobNumber() {
if (( $JOB_NUM < 1 || $JOB_NUM > 16 )); then
echo "pass"
fi
}
...
checkJobNumber
...
When I try to launch the script I get the message:
./script.sh line 49: ((: < 1 || > 16 : syntax error: operand expected (error token is "< 1 || > 16 ")
(Please notice the spaces in the error message)
I really don't understand what the problem is. If I do the evaluation manually at the command line, it works.
Also, I tried different evaluations like if [[ "$JOB_NUM" -lt 1 -o "$JOB_NUM" gt 16 ]];... still no success.
UPDATE: As suggested I made few more attempts in the rest of the code outside the function call, and I found the problem.
My variables declaration was actually indented this way:
JOB_NUM= 4
THREAD_NUM= 8
.....
VERY_LONG_VAR_NAME= 3
and apparently this hoses the evaulation. BAH! It always worked for me before, so why doesn’t it now?
If I delete the white spaces, the evaluation works:
JOB_NUM=4
THREAD_NUM=8
....
VERY_LONG_VAR_NAME=3
OK I officially hate bash . . . sigh :(
this will work fine
#!/bin/bash
JOB_NUM=7
function checkJobNumber() {
if [ $JOB_NUM -lt 0 ] || [ $JOB_NUM -gt 16 ] ; then
echo "true"
else
echo "false"
fi
}
checkJobNumber
And if you want to check variable during tests you can write in your bash :
set -u
this will generate a message like "JOB_NUM: unbound variable" if variable is not well set

Resources