subtraction in bash doesn't work - bash

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.

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

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)

Syntax error at line 1 : `(' is not expected

As I'm new to Unix, can someone help why I get this error?
Error: 0403-057 Syntax error at line 1 : `(' is not expected
Unix server used: AIX servname 1 6 00F635064C00
Script used (to send email alert if day before yesterday source files didn't arrive):
#!/usr/bin/ksh
count=$(sqlplus $PROD_DB #select count(*) from file_audit where (file_name like '%abc%' or file_name like '%dce%') and substr(file_name,17,8)=to_char(to_date(sysdate-2,'DD/MM/YY'), 'yyyymmdd') > asa_file_count.log)
daybefore=`TZ=aaa48 date +%d-%m-%Y`
if [[ $count -lt 20 ]]
then
echo "Alert - Source files are yet to be received for date: $daybefore" | mail -s "Alert : Source data files missing" s#g.com
fi
Parentheses are special to the shell. Your SQL script contains parentheses you don't want the shell to process. However, the shell processes all non-quoted parentheses. Therefore, you can use quotes to prevent the parentheses in your SQL from being interpreted by the shell:
count=$(sqlplus $PROD_DB "#select count(*) from file_audit where (file_name like '%abc%' or file_name like '%dce%') and substr(file_name,17,8)=to_char(to_date(sysdate-2,'DD/MM/YY'), 'yyyymmdd')" > asa_file_count.log)
# ^ and similarly, a closing quote at the end, just before ">asa_file..." .
Now, there is a second issue: you have
count=$(sqlplus ... > asa_file_count.log)
However, I think this means count will always be empty, since the count will go into asa_file_count.log and will not be available to be captured with $(). I believe removing the >asa_file_count.log will probably do what you want:
count=$(sqlplus "$PROD_DB" "<your query>")
(I also put double-quotes around $PROD_DB just in case PROD_DB's value contains any spaces.)

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

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