我的英语很差,但我正在努力解释我的问题。
我想在bash上创建一个小程序,用户必须猜出数字。但我的计划没有结束。
我是bash脚本的新手,但我每天都在努力变得更好。
我在用CentOS 7。我转到/usr/bin/local并创建一些文件,给他chmod+x并用gedit打开:

#!/usr/bin/bash

#This script should ask us our name and play the game "Guess the Number" with us.

echo "What is your name?"
read name
echo "Hello $name"
echo "I want to play $ name with you, you have to guess the number from 1 to 10)"
echo "Are you ready?"
read rdy


answer=$(( answer = $RANDOM % 10 ))


read -p "Enter your number: " variant
     while [ $answer != $variant ]; do

if [ $answer -gt $variant ]; then
     echo "Your number is bigger, try again"

elif [ $answer -lt $variant ]; then
     echo "Your number is smaller, try again"

  continue

fi

done

eternal "Your number is smaller/bigger"
until you press ctrl+c

我需要做的,请帮忙。我知道非业余爱好者不喜欢新手,但我需要你的帮助,告诉我我的错误,我可以变得更好。谢谢!

最佳答案

#!/bin/bash
echo "What is your name?"
read name
echo "Hello $name"
echo "I want to play  with you, you have to guess the number from 1 to 10"


answer=$(( $RANDOM % 10 ))

while true
do
    read -p "Enter your number: " variant
    if [ "$answer" -gt "$variant" ]; then
        echo "The number is bigger, try again"

    elif [ "$answer" -lt "$variant" ]; then
        echo "The number is smaller, try again"
    else
        break;
    fi

done
echo "Yes, the answer was ${answer}"

10-08 08:11
查看更多