Nella
Nella

Reputation: 47

Perform an operation to a variable. Subtracting 1

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

Upvotes: 2

Views: 6916

Answers (1)

David Allewell
David Allewell

Reputation: 315

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.

Upvotes: 6

Related Questions