我有这个菜单脚本工作。我应该编写一个代码,它将在日志文件中记录菜单脚本的使用次数以及菜单中使用的选项。
#\usr\bin\bash
clear;
echo "Welcome"
while [ $option!=7 ]
do
clear;
printf "\n\t\tMenu Principal"
printf "\n1)Pega 2 "
printf "\n2)Pega 3 "
printf "\n3)Pega 4 "
printf "\n4)Loto "
printf "\n5)Revancha "
printf "\n6)Ver Log File "
printf "\n7)Salir "
read option
case $option in
'1')
clear;
echo "Los numeros del Pega 2 son : "
awk 'BEGIN { for(i=1;i<=2;i++) print int(9*rand())}'>temp
cat temp ; sleep 3
;;
'2')
clear;
echo "Los numeros del Pega 3 son : "
awk 'BEGIN { for(i=1;i<=3;i++) print int(9*rand())}'>temp
cat temp ; sleep 3
;;
'3')
clear;
echo "Los numeros del Pega 4 son : "
awk 'BEGIN { for(i=1;i<=4;i++) print int(9*rand())}'>temp
cat temp ; sleep 3
;;
'4')
clear;
echo "Los numeros de la Loteria son : "
awk 'BEGIN { for(i=1;i<=6;i++) print int(46*rand())}'>temp
cat temp ; sleep 5
;;
'5')
clear;
echo "Los numeros de la Revancha son : "
awk 'BEGIN { for(i=1;i<=6;i++) print int(46*rand())}'>temp
cat temp ; sleep 5
;;
'6')
clear;
;;
'7')
clear;
echo "Gracias por participar! " ; sleep 2
exit
;;
esac
done
在选项6中是日志文件应该去的地方。有什么指针吗?
答案 0 :(得分:1)
需要脚本部分我认为它比bash更awk。首先,您需要一些变量来统计执行每个选项的次数。如果您的bash版本接受它,我会使用关联数组:
首先,当第一条指令声明它时:
declare -A options
对于每个执行的选项,增加其在哈希中的位置:
...
read option
((++options[ $option ]))
total_executed_times=0
case $option in
...
因此,当单击第六个选项时,遍历数组以打印结果:
'6')
for i in "${!options[@]}"; do
total_executed_times=$(($total_executed_times + ${options[$i]}))
echo "Option $i executed ${options[$i]} times"
done
echo "Times the menu was used: $total_executed_times"
sleep 5
clear;
;;
请注意,未选择的选项将不会打印,并且没有按键顺序,但如果您愿意,可能需要改进。
我执行了测试,在clear
之前捕获了它并且它产生了:
Option 6 executed 2 times
Option 3 executed 1 times
Option 4 executed 2 times
Option 1 executed 4 times
Option 2 executed 1 times
Times the menu was used: 10