擦除随机生成的数字,但将用户输入与从屏幕上删除后的数字进行比较

时间:2016-02-24 21:06:33

标签: bash

我必须编写一个bash脚本来生成一个10位数的随机数。然后我必须"擦除" x秒后的数字。然后让用户尝试记住他们所看到的数字和类型,并比较两者给出的是或否

这是我的随机数

echo $RANDOM$RANDOM$RANDOM | cut -c3-12

并且我在5秒后删除了一条线,但是它会删除它所依赖的线而不是我的随机数线

for i in {1..5};do echo $i;sleep 1;tput cuu1;tput el;done

所以我跑

echo $RANDOM$RANDOM$RANDOM | cut -c3-12
for i in {1..5};do echo $i;sleep 1;tput cuu1;tput el;done

并且在随机数下面的行上最多计数5,然后删除它所依赖的行。如何擦除随机数并仍将其存储以比较用户输入?

3 个答案:

答案 0 :(得分:0)

只需在tput cuu1;tput el循环后添加另一个for,所以您的代码如下:

echo $RANDOM$RANDOM$RANDOM | cut -c3-12
for i in {1..5}; do echo $i; sleep 1; tput cuu1; tput el; done
tput cuu1; tput el

答案 1 :(得分:0)

将随机数存储在变量中。使用您在循环中使用的相同命令擦除循环后的随机数:

random_number=$(cut -c3-12 <<< $RANDOM$RANDOM$RANDOM)
echo $random_number 

for i in {1..5}
do
   echo $i
   sleep 1
   tput cuu1
   tput el
done

tput cuu1
tput el

答案 2 :(得分:0)

我在bash中创建了一个类似于此的小记忆游戏。这是相当古老和丑陋,但也许它会帮助你一点点。

#!/bin/bash

clear

banner="=============================================="

echo ""
echo -e "\x1b[93m Yokai's Guessing game! \x1b[0m"
echo "$banner"
echo ""
echo "I will give you a number and you will have 10 seconds to remember it."
echo "After 10 seconds it will disappear and you will have to try to guess it."
echo ""
read -p "Press <enter> to continue"

main() {
clear

GEN="$(tr -dc '0-9' </dev/urandom | head -c 10)"

echo ""
echo -e "\x1b[93m Yokai's Guessing game! \x1b[0m"
echo "$banner"
echo ""
echo -e "Your number is:\x1b[31m $GEN \x1b[0m"
sleep 10
clear

echo ""
echo -e "\x1b[93m Yokai's Guessing game! \x1b[0m"
echo "$banner"
echo ""
echo "Can you remember the number?"
echo ""
read GUESS

if [ "$GUESS" == "$GEN" ]
then
clear
echo ""
echo -e "\x1b[93m Yokai's Guessing game! \x1b[0m"
echo "$banner"
echo ""
echo "You guessed correctly!! Well done!!"
sleep 2
echo "Would you like to play again?"
echo "(y/n)"
echo ""
read ANSWER

until [ "$ANSWER" == "y" ]
do
echo ""
echo "Okay, thanks for playing!"
sleep 2
clear
exit 0
done
fi

if [ "$GUESS" != "$GEN" ]
then
clear
echo ""
echo -e "\x1b[93m Yokai's Guessing game! \x1b[0m"
echo "$banner"
echo ""
echo "Oooooooh! Sorry but that number was incorrect!"
echo "Would you like to try again?"
echo "(y/n)"
read CHOICE

if [ "$CHOICE" == "n" ]
then
clear
echo ""
echo -e "\x1b[93m Yokai's Guessing game! \x1b[0m"
echo "$banner"
echo ""
echo "Okay thanks for playing!"
sleep 2
clear
exit 0
fi
fi


}

while true
do
main
done