3

I have a question. I have a bash script that works fine.

 while read val1 val2 val3 do echo "ncap2 -Oh -s'TOPOBATHY(($val1,$val2))=$val3.'" done < $tmpdir/tmp1 > $tmpdir/tmp2 

I would like just to perform one operation to val1 and val2, subtracting 1. I tried to subtract directly but it does not work. Is there a way to do that. Thanks in advance for your help!

 while read val1 val2 val3 do echo "ncap2 -Oh -s'TOPOBATHY(($val1-1),($val2-1))=$val3.'" done < $tmpdir/tmp1 > $tmpdir/tmp2 

1 Answer 1

7

You can perform (integer) arithmetic on variables within Bash with this syntax

For your specific case, you can either set the new values of the variables before calling them in echo:

val1=$((val1 - 1)) 

or you can put it in-line:

echo "ncap2 -Oh -s'TOPOBATHY($((val1 - 1)),$((val2 - 1 )))=$val3.'" 

Of course, you are presently relying on the user to input suitable values, as there's no error-checking/input sanitisation in the example you provide.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.