I'm interested in learning how to create shell scripts with bash so here's one of my first exercises, taken from here. It is an extension of the problems Elementary - 4 and 5.
The goal of the program is to sum all multiples or ARG1 or ARG2 below the user's input number.
#!/bin/bash
echo "This program will find all the multiples of ARG1 or ARG2 below or equal to your number and sum them up. Please input your limit:"
read limit
commonMultiple=`expr $1 \* $2`
sumArg1=0
sumArg2=0
sumCommonMultiple=0
for (( i = 0; i <= $limit; i+=$1 )); do
sumArg1=`expr $sumArg1 + $i`
done
for (( i = 0; i <= $limit; i+=$2 )); do
sumArg2=`expr $sumArg2 + $i`
done
for (( i = 0; i <= $limit; i+=$commonMultiple )); do
sumCommonMultiple=`expr $sumCommonMultiple + $i`
done
echo "The result is `expr $sumArg1 + $sumArg2 - $sumCommonMultiple`."
This works perfectly but I'd like the input of more experienced programmers. I don't think my logic is efficient. There must be something obvious I'm missing that could have made my code a lot simpler.
Disclaimer: apparently some of the stuff I used is outdated. I'll take it into account for my future code.*
bash
really isn't designed for computational/algorithmic problems at all. It is a tool for specific types of jobs (see link) and in that domain it excels. \$\endgroup\$