Here's what I'm trying to make: $echo 1 3 5 7 9 | sh test
which outputs 25. (final goal is a derivative calculator, don't laugh at me, I'm only grade 12).
When the amount of variables are given, it's pretty easy. But what if you don't know how many variables are there, how do I take everything behind echo, in this case 1 3 5 7 9
, and put them into something similar to an array in Java, then simply do something like for i in array and i+=1
?
This was my idea:use cat $*
to read all numbers, then use while loop to go through all the numbers, and add them to the sum.
Alternate idea: replace all spaces with \n
after cat $*
then echo it to some file, and use something similar File Reader -> read line in Java for the while loop.
Here's what I don't understand about shell, when you cat $*
, do you assign it to something, like a=cat $*
and get value by a[0]? and when you do the while loop, do you while read a
or what do you while
about?
First time posting here, self learning is much harder than I thought..
Thanks for helping!