저는 유닉스를 연습하고 스스로 가르치면서 현금 지급기를 만들기 위한 기본적인 코딩을 했습니다. 지금까지 나는 다음을 수행했으며 마지막 장애물에 대한 지침이 필요합니다.
이 값을 업데이트하는 방법이 막혔습니다. 예를 들어, 사용자가 옵션을 선택하는 경우:- 1=5 2=10 3=20 4=50
이렇게 간단할 줄 알았는데expr $bal1 +5
아니면 awk 및 sed 명령을 사용하여 현재 값과 사용자가 입력한 값을 추출해야 합니까?
실제로 하나의 파일에 코드가 있습니다.
#!/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
답변1
위의 설명에서 언급한 것처럼 5를 추가하려면 bal1
다음과 같이 하면 됩니다.
bal1=$((bal1 + 5))
예를 들어:
$ x=42
$ echo $x
42
$ x=$((x + 5))
$ echo $x
47
$