我正在练习unix,自学所以我做了一些创建提款机的基本编码。到目前为止,我已经完成了以下操作,需要有关最后一个障碍的一些指导
我陷入了如何更新该值的困境。例如,如果用户选择选项:- 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
$