Friday, September 5, 2014

Bash recursive subtraction function

#!/usr/bin/env bash

#recursive subtract, arg1 - arg2
function f_r_s() {
if [[ $1 -eq $2 ]]; then
 echo 0; 
elif [[ $1 -gt $2 ]]; then
 echo "1 + $(f_r_s $(echo "$1 - 1"|bc) $2)" | bc;
else
  echo "f_r_s(), reverse the args and try again";
fi;

No comments:

Post a Comment