0

Let say user choose 5 (antall). I want then to print 5+6+7+8+4 (for instans). In my script i get this print:

5+6+7+8+4**+**

How to get rid of the last +?? (I have tried echo "+" in a for loop, but havent manage to get i right)

My script:

plusse () {

while true; do
random=0
summit=0
read -p "Hvor mange tall vil du plusse>> " antall
echo "Hva er?  "


for (( i=1; i<=$antall; i++ )); do
random=$(( RANDOM % 10 ))
summit=$(( summit + $random ))

echo -n "$random "
    echo -n "+"
done
echo
read -p "Svaret mitt er>> " svarr
echo "$summit"
    if [[ $svarr == $summit ]]; then
        echo "Gratulerer! $svarr er riktig svar! "
        read -p "Vil du fortsette?>> " fortsette
            case $fortsette in
                ja|j|JA|Ja|y|Yes)   continue 
                                    ;;
                nei|n|Nei|n|N)      break 1
                                    ;;
                *)                  exit 1
                                    ;;
            esac
    else
        echo "Beklager, $svarr er feil. Riktig svar er $summit"
        read -p "Vil du fortsette?>> " fortsette
            case $fortsette in
                ja|j|JA|Ja|y|Yes)   continue
                                    ;;
                nei|n|Nei|n|N)      break 1
                                    ;;
                *)                  exit 1
                                    ;;
            esac

    fi
done
}
0

Your code:

for (( i=1; i<=$antall; i++ )); do
random=$(( RANDOM % 10 ))
summit=$(( summit + $random ))

echo -n "$random "
    echo -n "+"
done

will always append a + after the chosen digit. You might want instead to do something like:

prefix=""
for (( i=1; i<=$antall; i++ )); do
    random=$(( RANDOM % 10 ))
    summit=$(( summit + $random ))
    echo -n "${prefix}${random}"
    prefix="+"
done
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.