I have to submit a script which adds two values within an for loop and puts every result in an array. I put together a script (which is not working) but I cannot figure out how to get it started.
#!/bin/sh
val1=$1
val2=$2
for i in 10
do
${array[i]}='expr $val1+$val2'
$val1++
done
echo ${array[@]}
Perhaps you mean this?
val1=$1
val2=$2
for i in {1..10}; do
array[i]=$(( val1 + val2 ))
(( ++val1 ))
done
echo "${array[@]}"
If you bash doesn't support {x..y}
, use this format:
for (( i = 1; i <= 10; ++i )); do
Also simpler form of
array[i]=$(( val1 + val2 ))
(( ++val1 ))
Is
(( array[i] = val1 + val2, ++val1 )) ## val1++ + val2 looks dirty
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With