Explore Bash in Linux by writing a "guess number" game

Learning a new programming language is very interesting. Whenever Jim Hall tries to learn new variables, he will focus on defining variables, writing statements and calculating expressions. Once he has a general understanding of these concepts, he can usually figure out the remaining concepts himself. Most programming languages ​​have some similarities, so once you understand one programming language, learning the next programming language requires understanding its unique details and identifying the differences.

To help Jim Hall practice a new programming language, he likes to write some test programs. An example program often written by Jim Hall is a simple "guess number" program in which the computer chooses a number between 1 and 100 and asks to guess the number. The program loops until Jim Hall guesses it right.

The "Guess Number" program uses several concepts in programming languages: how to assign values ​​to variables, how to write statements, and how to perform conditional calculations and loops. This is a good practice experiment for learning a new programming language.

Guess Bash's number

Bash is the standard shell for most Linux systems. In addition to providing a rich command line user interface, bash also supports a complete programming language in the following form: script .

You can explore Bash by writing a version of the "guess the numbers" game. The following is my implementation:


Break down the script

The first line in the script, #!/bin/bash tells Linux to use Bashshell to run this script. Each script has a #! character pair, which means it is a shell script. What will happen next #! is the shell to run. In this case, /bin/bash is the shell.

To assign a value to a variable, please list the name of the variable, followed by = sign. For example, the statement guess=0 is the guess variable.

You can also use the read statement. If you write read guess, Bash waits for the user to enter some text, and then stores the value in the guess variable.

To quote the value of a variable, use $ before the variable name. Therefore, in the guess variable, you can use $guess.

You can use any name you like for the variable, but Bash reserves some special variable names for itself. A special variable is RANDOM, which generates a very large random number every time it is referenced.

If you want to perform an operation while storing the value, you need to enclose the statement in special brackets. This will tell Bash to execute the statement first, and = store the resulting value in a variable. To calculate mathematical expressions, use $(( )) about your statement. Double brackets indicate arithmetic expressions . In my example, number=$(( $RANDOM% 100 + 1 )) calculates the expression $RANDOM% 100 + 1 and then stores the value in the number variable.

Standard arithmetic operators such as + (plus)-(minus), * (multiply), / (minute), and% (modulus) apply.

This means that number=$(( $RANDOM% 100 + 1 )) generates a random number between 1 and 100. The modulus operator (%) returns the remainder after dividing by two numbers. In this case, Bash divides a random number by 100, and the remainder is between zero and 99. By adding a value to this value, a random number between 1 and 100 can be obtained.

Bash bracket conditional expression and flow control are like loops. In the "guess number" game, bash continues to loop as long as guess is not equal to number. If the guess is less than the random number, Bash prints "too low", if the guess is greater than the random number, Bash prints "too high".

How it works

Now that you have written the Bash script, you can run it to play the "guess number" game. Keep guessing until you find the correct number:

Each time the script is run, Bash will choose a different random number.

When learning a new programming language, this "guess the numbers" game is a good entry program because it practices several common programming concepts in a very simple way. By implementing this simple game in different programming languages, you can demonstrate some core concepts and compare the details of each language.

Do you have a favorite programming language? How would you write a "guess the number" game? Follow this article to see examples of other programming languages ​​that you might be interested in.

Finally, if you also want to become a programmer and want to quickly master programming, quickly join the learning penguin circle !

There are senior professional software development engineers who can answer all your doubts online ~ Introduction to programming language "so easy"

Programming learning books:

Programming learning video:

Guess you like

Origin blog.csdn.net/Hsuesh/article/details/112466846