Alterando um saldo de valor variável

0

Estou praticando unix, auto-aprendendo, então fiz algumas codificações básicas para criar um caixa eletrônico. até agora eu fiz o seguinte e preciso de algumas orientações sobre o obstáculo final

Estou preso em como posso atualizar o valor. por exemplo, se o usuário selecionar a opção: 1 = 5 2 = 10 3 = 20 4 = 50

Eu estava pensando que seria tão fácil quanto algo assim expr $bal1 +5

ou devo usar o comando awk e sed para extrair o valor atual e o valor que o usuário digita?

Aqui está o código, na verdade tudo em um arquivo

#!/bin/bash
#This is an automated cash machine

##########################
#Created Variables
##########################
user1=1234
user2=0000
user3=0124
name1=Mike
name1=John
name2=Brad
name3=Sophie
bal1=0
bal2=0
bal3=0
###################################
#Created functions add_funds
###################################

main_screen ()
{
    echo "####################################"
    echo "1.\tAdd Funds"
    echo "2.\tWithdraw Funds"
    echo "3.\tCheck Balance"
    echo "4.\tExit"
    echo "####################################"

    echo "Enter Option:\c"
    read number

    if [ $number = 1 ]
    then
        add_funds

    elif [ $number = 2 ]
    then
        clear
        echo "withdraw Funds"

    elif [ $number = 3 ]
    then
        clear 
        echo "Show balance"

    elif [ $number = 4 ]
    then
        exit

    else   
        echo "wrong selection, try again"
        sleep 1
        clear 
        while [ $number -gt 4 ]; do
            main_screen
        done
    fi
}

add_funds ()
{
    clear
    echo "How much would you like to add?"
    echo "select one of the following options"

    echo "1.\t£5.00"
    echo "2.\t£10.00"
    echo "3.\t£20.00"
    echo "4.\t£50.00"

    echo "Enter Option:\c"

    read amount

    if [ $amount = 1 ]
    then
        $(('expr $bal1+5')) #this does not work
        echo "you have added £5.00"
        echo "ACCOUNT UPDATED $bal1"

    elif [ $amount = 2 ]
    then
        echo "you have added £10.00"
        echo "ACCOUNT UPDATED"
    elif [ $amount = 3 ]
    then
        echo "you have added £20.00"
        echo "ACCOUNT UPDATED"
    elif [ $amount = 4 ]
    then
        echo "you have added £50.00"
        echo "ACCOUNT UPDATED"
    else
        echo "wrong selection, try again"
        sleep 1
        while [ $amount -gt 4 ]; do
            add_funds
        done
    fi
}

################################
# MAIN CODE
################################

clear
echo "***********************"
echo "   CASH DESPENSER      "
echo "***********************"

echo "Enter Pin:\c"

read pin

if [ $pin = $user1 ]
then
    clear
    echo "Welcome $name1, How can I assist you"
    main_screen
elif [ $pin = $user2 ]
then
    clear
    echo "Welcome $name2, How can I assist you"
    main_screen
elif [ $pin = $user3 ]
then
    clear
    echo "Welcome $name3, how can I assist you"
    main_screen
else
    echo "Incorrect pin user, try again"
fi
    
por Kam 01.11.2017 / 13:30

1 resposta

1

Como mencionei no comentário acima, se você quiser adicionar 5 a bal1 , poderá fazer:

bal1=$((bal1 + 5))

Por exemplo:

$ x=42
$ echo $x
42
$ x=$((x + 5))
$ echo $x
47
$
    
por 01.11.2017 / 18:42

Tags