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

Guess the price of Linux-shell fun

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

Case requirements:

A random number of prices (0-999) is pre-generated by the script as the actual price

Judge whether the price guessed by the user is higher or lower than the actual price, and ask the user to guess again after giving the corresponding prompt.

Until the user guesses the actual price, the number of times the user guesses and the actual price are output.

The steps of the experiment:

1. Write a script with an arbitrary name, shell.sh:

The screenshot is as follows:

The contents are as follows:

#! / bin/bash

PRICE=$ (expr $RANDOM 1000)

TIMES=0

Echo "the actual price of the commodity is between 0-999, guess what it is?"

While true

Do

Read-p "Please enter the number of prices you guess:" INT

Let TIMES++

If [$INT-eq $PRICE]

Then

Echo "Congratulations on your correct answer, the actual price is $PRICE"

Echo "you guessed a total of $TIMES times"

Exit 0

Elif [$INT-gt $PRICE]

Then

Echo "too high!"

Else

Echo "too low!"

Fi

Done

2. Add x permissions to the script, and execute the script to view the execution of the script:

In fact, there are a lot of real fun in the script, learn to be flexible and follow suit.

Attached:

You can view environment variables with the env or export command

Use the set command to view all variables (including environment and custom variables)

Use the unset command to delete a variable

Most versions of the LINUX system have a random number generator, the / dev/random file.

Random numbers can be obtained randomly through the system environment variable $RANDOM associated with this random number file.

In the BASH environment, the content of this RANDOM variable is between 0Murray 32767 (man bash, search RANDOM for instructions)

Therefore, as long as echo $RANDOM, the system will take the initiative to randomly pick up a value between 0Murray 32767.

For example, when you want to have a random number between 0Mel 9, you can declare the numeric type with declare

Welcome to the official account of Wechat: Xiao Wen study Society.

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

Network Security

Wechat

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

12
Report