0

I am attempting to calculate a grade based off of two variables with given values, and set it to a new variable in my shell script. For example,

x=25
y=75

Using the arithmetic expansion operator I have made many attempts to do so such as:

grade=$(($x*($y / 100)))
grade=$(($x * ($y / 100)))
y=$(($y / 100))
grade=$(($x*$y))

& many more desperate attempts, but none seem to work. Any tips as to what I am missing/not understanding? I have looked at several examples/other similar questions but they do not match my sitaution.

Thank you.

ShaheerL
  • 61
  • 7
  • 3
    It’s integer arithmetics only. So `$((y / 100))` is always `0` in that case etc. – Andrej Podzimek Oct 05 '21 at 23:52
  • @AndrejPodzimek Oh I see. What do I need to do to get doubles/decimal numbers as a result? – ShaheerL Oct 05 '21 at 23:55
  • Mostly duplicate of [Bash Division Keeps giving 0 - Stack Overflow](https://stackoverflow.com/questions/45515025/bash-division-keeps-giving-0). The use case is a little different however – user202729 Oct 05 '21 at 23:55
  • @user202729 Oh I had no clue about that. However let's say I did need to make such a calculation, how would I go about it without something such as printf? – ShaheerL Oct 05 '21 at 23:56
  • 1
    See [BashFAQ #22](https://mywiki.wooledge.org/BashFAQ/022) – Charles Duffy Oct 05 '21 at 23:57
  • 1
    ...that said, you can do a lot with integer math if you just change your base. For example, multiply all the numbers by 100 before starting. :) – Charles Duffy Oct 05 '21 at 23:58
  • Show some code and what exactly you want to do (probably in a new question? It's not in the scope of this one) The top answer in the other question doesn't use printf. – user202729 Oct 05 '21 at 23:58
  • `x=25; y=75; grade=$(( (x * 100) / y ))` – Charles Duffy Oct 05 '21 at 23:59
  • @ShaheerL For floating-point support you need a language other than Bash. Like `bc`, `awk`, `js`, `python` etc. (The last one also has an awesome arbitrary-precision rational `Fraction` class in standard libraries.) If you *really* want to do *something* like this in Bash, no matter what, “poor man’s” approximations and rounding may help. Just set `big` big enough and: `big=10000; blah=$(((big * x * y + 50) / 100)); grade="$((blah / big)).$((blah % big))"; echo "$grade"`. However, `$grade` is not a “number” one could process in Bash; it’s just a string (in dire need of further processing). – Andrej Podzimek Oct 06 '21 at 00:08
  • For another demonstration see https://ideone.com/4tRgVE – Charles Duffy Oct 06 '21 at 00:09
  • 1
    @ShaheerL, btw, I don't understand "without something such as printf". `printf` is built into bash just as much as `echo` and `pwd` are -- in fact, the POSIX standard for `echo` recommends using `printf` instead (see the APPLICATION USAGE and RATIONALE sections of https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html). Why would you want to not use it? – Charles Duffy Oct 06 '21 at 00:11
  • 1
    Further random thoughts, mostly ugly ones: (1) `awk -vx="$x" -vy="$y" 'BEGIN {print x * y / 100}'` (2) `bc -l <<< "${x} * ${y} / 100"` – Andrej Podzimek Oct 06 '21 at 00:16
  • @CharlesDuffy The main reason is because I am using echo to print several different variables in my script, for example `echo "$weight, $mark, $grade`, and tried to incorporate printf earlier but it wasn't working. Now that you've told me its possible I will try to research its use more. – ShaheerL Oct 06 '21 at 00:19
  • 1
    @ShaheerL, `printf '%s, %s, %s\n' "$weight" "$mark" "grade"` for your particular example, or just `printf '%s\n' "$weight, $mark, $grade"`. Also, see [Why is printf better than echo?](https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo) at [unix.se]. – Charles Duffy Oct 06 '21 at 11:39

0 Answers0