Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Bash to guess numbers in linux

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use Bash to guess numbers in linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Guessing numbers with Bash

Bash is the standard shell for most Linux systems. In addition to providing a rich command-line user interface, Bash supports a complete programming language in the form of scripts.

If you are not familiar with Bash, I recommend you read these introductions:

What is Bash?

Start programming with Bash

Getting started with Bash scripts for system administrators

How to write functions in Bash

Read more about Bash

You can explore it by writing a Bash version of the number guessing game. This is my realization:

#! / bin/bash number=$ (($RANDOM% 100 + 1)) echo "Guess a number between 1 and 100" guess=0 while ["0$ guess"-ne $number]; do read guess ["0$ guess"-lt $number] & & echo "Too low" ["0$ guess"-gt $number] & & echo "Too high" done echo "That's right!" exit 0 dismantle this script

The first line in the script, #! / bin/bash, tells Linux to run the script using Bash shell. Each script uses #! The character pair begins, which indicates that it is a shell script. Right behind #! Next is the shell to run. In this case, / bin/bash refers to Bash shell.

To assign a value to a variable, list the = sign after the variable name. For example, the statement guess=0 assigns a zero value to the guess variable.

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

To reference the value of a variable, use $before the variable name. So, after storing a value in the guess variable, you can use $guess to retrieve it.

You can use any variable name you like, but Bash keeps some special variable names for yourself. A special variable is RANDOM, which produces a large random number each time it is referenced.

If you want to perform an operation while storing a value, you need to enclose the statement in special parentheses. This tells Bash to execute the statement first, while = stores the resulting value in the variable. To evaluate a mathematical expression, surround your statement with $(()). Double parentheses represent an arithmetic expression. In my example, number=$ (($RANDOM% 100 + 1)) evaluates the expression $RANDOM% 100 + 1, and then stores the value in the number variable.

Standard arithmetic operators, such as + (add),-(minus), * (multiply), / (divide), and% (module) all apply.

This means that the statement number=$ (($RANDOM% 100 + 1)) produces a random number between 1 and 100. The modular operator (%) returns the remainder of the division of two numbers. In this case, Bash divides a random number by 100, and the rest ranges from 0 to 99. By adding 1 to this value, you can get a random number between 1 and 100.

Bash supports conditional expressions and flow controls such as loops. In the game of guessing numbers, the loop continues as long as the value in guess is not equal to number,Bash. If the guessed number is less than the random number, Bash will print "too low", and if the guessed number is greater than the number, Bash will print "too high".

How does it work?

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

Guess a number between 1 and 10050Too high30Too high20Too high10Too low15Too high13Too low14That's right!

Each time you run this script, Bash randomly selects a different number.

This "guessing numbers" game is a good starter for learning a new programming language because it exercises several common programming concepts in a straightforward way. By implementing this simple game in different programming languages, you can show some core concepts and compare the details of each language.

The above is all the contents of the article "how to guess numbers with Bash in linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report