Bash script operand expected - bash

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

Related

Error on certain line: Syntax error: "(" unexpected

Have an error "Syntax error: "(" unexpected" when execute an script:
sync.sh: 11: sync.sh: Syntax error: "(" unexpected
line 11 contains on this:
declare -a FOLDERS=('/scripts' '/backup')
and on the top of script have the interpreter:
#!/bin/bash
Execute the script with:
sh /wdmycloudex2/$(hostname)/scripts/sync.sh
/wdmycloudex2/RASPBIAN/scripts/sync.sh: 11: /wdmycloudex2/RASPBIAN/scripts/sync.sh: Syntax error: "(" unexpected
The firsts 11 lines:
#!/bin/bash
IP='10.0.1.7'
PORT='443'
HOSTNAME=$(hostname)
DATE=$(date +%d%m%Y_%H%M%S)
SOURCE='/scripts'
DEST='/wdmycloudex2'
declare -a FOLDERS=('/scripts' '/backup')
anybody know and explain what's the problem?
The header #!/bin/bash is ignored when you start the script with sh sync.sh.
It will go better with bash /wdmycloudex2/RASPBIAN/scripts/sync.sh or
chmod +x /wdmycloudex2/RASPBIAN/scripts/sync.sh
/wdmycloudex2/RASPBIAN/scripts/sync.sh

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)

Runtime error (func=(main), adr=9): Divide by zero

I am getting below error. The script curl the provided url and compare it with the previous curl. Several tests can be made, the default one is calculating how many percentage of code have been changed since last check
error
Runtime error (func=(main), adr=9): Divide by zero ./check_defacement.sh: line 145: [: -ge: unary operator expected ./check_defacement.sh: line 151: [: -ge: unary operator expected
code
if [ "$int_per" -ge "$int_critical" ]; then
OUTPUT="CRITICAL - $int_per% code changed since last check | $int_per%"
STATE=$STATE_CRITICAL
cp $dump_dir/check_defacement_"$url" $dump_dir/dump_CRITICAL_date +"%d%m%Y%H%M%S"_"$url"
fi
if [ "$int_per" -ge "$int_warning" ]; then
OUTPUT="WARNING - $int_per% code changed since last check | $int_per%"
STATE=$STATE_WARNING
cp $dump_dir/check_defacement_"$url" $dump_dir/dump_WARNING_date +"%d%m%Y%H%M%S"_"$url"
else
OUTPUT="OK - $int_per% code changed since last check | $int_per%"
STATE=$STATE_OK
fi

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

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

Resources