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