一个商品购买练习脚本
#!/bin/bash read -p "请输入当前money:" qian read -p "请输入工资:" gongzi cat ./caidan #read -p "请输入要购买的商品价格:" shangpin kemai(){ awk 'BEGIN{FS=":"}{print $3}' caidan | while read jiage;do if [ $1 -ge $jiage ];then mai=`awk 'BEGIN{FS=":"}/:'$jiage'$/{ print $0}' caidan` echo $mai fi done } read -p "是否要输出可购买的商品[y/n]:" ans1 [ $ans1 = y ] && kemai $qian yimai(){ qian=$1 while :;do read -p "请输入要购买的商品编号:" num1 if awk 'BEGIN{FS=":"}{print $1}' caidan | grep $num1 &> /dev/null;then jiage=`awk 'BEGIN{FS=":"}/^'$num1':/{print $3}' caidan` [ $qian -ge $jiage ] && qian=$[$qian - $jiage] && echo "你还剩余${qian}元"|| { echo "你的钱不够!你只有${qian}元了" && n=`echo "scale=1;( $jiage - $qian ) / $2" | bc` [ `echo ${n##*.}` -gt 0 ] && n=$[${n%%.*}+1] echo "你还需要收入${n}次工资才能购买" break } fi done } yimai $qian $gongzi
其中商品caidan文件格式如下:
1:自行车:100
2:汽车:1000
3:电脑:500
好吧,其实这是个py练习=.=
[星空刺]
|-->一颗星辰一闪即逝,支撑它的唯有方向和目的