I have a script where the user can add as many arguments as he would like (numbers).
The script will sum all the numbers beside the last number - The last number (argument) is the number that I need to divide by
For example:
./test.sh 2 2 6 5
This will sum the first 3 numbers (2+2+6) and divide the answer by 5 (the last argument)
- How can I use the last argument? Echo ????
- How can I move loop the first arguments besides the last one – I would like that all 3 arguments will be added to an array and I can loop it
- Please note that the number of arguments can be changed
Shortly (with bashisms)
As this question is tagged integer-arithmetic and bash:
Here is a small and efficient script:
But there's no loop...
Long answer
You could make your tries in command line:
Then
Ok, then
so
Still forward, under command line...
Use
declare -pto show$someArray's content:Then
Please note:
Using double quotes allow processing of strings containing spaces:
Difference betweeen use of "$@" (array to array) and "$*" (array to string)
If I take 3 first elements:
But