Just learn Linux process control statements while, case

while conditional loop statement

The while conditional loop statement is a statement that allows the script to repeatedly execute commands according to certain conditions. Its loop structure often does not determine the final number of executions before execution, which is completely different from the purposeful and scoped use of the for loop statement. Scenes. The while loop statement decides whether to continue to execute the command by judging whether the condition test is true or false. If the condition is true, it will continue to execute, and if it is false, it will end the loop. The syntax format of the while statement is shown in the figure.

insert image description here

vim Guess.sh
#!/bin/bash
PRICE=$(expr $RANDOM %1000)re
TIME=0
echo "商品实际价格为0-999之间, 猜猜看是多少?"
while true
do
	read -p "请输入你猜测的价格数目:" INT
	let TIMESS++
	if [ $INT -eq $PRICE ] ; then
		echo "恭喜你答对了,实际价格是 $PRICE"
		echo "您总共猜测了 $TIMES次"
		exit
	elif [ $INT -gt $PRICE ] ; then
		echo "太高了"
	else
		echo "太低了"
	fi
done

Use exit to end the while loop.

case condition test statement

The case statement is to match data in multiple ranges. If the match is successful, the relevant command will be executed and the entire conditional test will end; if the data is not in the listed range, the default command defined in the asterisk (*) will be executed.
insert image description here
Next, we write the script Checkkeys.sh to prompt the user to input a character and assign it to the variable KEY, and then show the user whether the value is a letter, a number or other characters according to the value of the variable KEY.

vim Checkkeys.sh
#!/bin/bash
read -p "请输入一个字符,并按enter键确认:" KEY
case "$KEY" in 
	[a-z] | [A-Z])
		echo "您输入的是 字母"
		;;
	[0-9])
		echo "您输入的是 数字"
		;;
	*)
		echo "您输入的是 空格、功能键或其他控制字符。"
esac
# bash Checkkeys.sh

Scheduled task service program

How to set up the scheduled task service of the server, and hand over the periodic and regular work to the system for automatic completion.
Planned tasks are divided into one-time planned tasks and long-term planned tasks

➢ One-time planned task: Restart the website service at 23:30 tonight.
➢ Long-term planned task: package and backup the /home/wwwroot directory as backup.tar.gz every Monday at 3:25 am.

One-time scheduled tasks are executed only once and are generally used for temporary work needs. You can use the at command to achieve this function, just write it in the form of "at time". If you want to view the one-time scheduled tasks that have been set but have not yet been executed, you can use the at -l command; if you want to delete them, you can use "atrm task number". The parameters and their functions in the at command are shown in the table.

insert image description here
When using the at command to set up a one-time scheduled task, the default is the interactive method. For example, use the following command to set the system to automatically restart the website service at 23:30 tonight.

at 23:30
systemctl restart httpd
at -l

You can put the previously learned pipe character (arbitrary gate) between the two commands, and let the at command receive the output information of the previous echo command, so as to achieve the purpose of creating and planning a one-time task in a non-interactive way.

echo "systemctl restart httpd" | at 23:30
at -l

Two of the same scheduled tasks are set above, and one of them can be easily deleted by using the atrm command:

atrm 2
at -l

There is also a special scenario here—write the scheduled task into the Shell script, and start the countdown execution when the user activates the script, instead of at a fixed time ("at 23:30" command) as above. What should we do?

Generally, we will use the method of "at now +2 MINUTE", which means that the task will be executed after 2 minutes (MINUTE), or it can be replaced by words such as hour (HOUR), day (DAY), month (MONTH) :

at now + 2 MINUTE
systemctl restart httpd

It is hoped that the Linux system can perform certain specific tasks periodically and regularly, then the crond service enabled by default in the Linux system is simply perfect. The command to create and edit a scheduled task is crontab -e, the command to view the current scheduled task is crontab -l, and the command to delete a scheduled task is crontab -r. In addition, if you log in to the system as an administrator, you can add the -u parameter to the crontab command to edit other people's scheduled tasks. The parameters and their functions in the crontab command are as shown.
insert image description here
Before officially deploying the planned tasks, please read the formula "orders of minutes, hours, days, months, and weeks" with Teacher Liu Tun. This is the parameter format for setting tasks using the crond service (see Table 4-8 for its format). It should be noted that if some fields are not set, you need to use an asterisk (*) to place them. As shown in the figure, it is assumed that at
insert image description here
insert image description here
3:25 in the morning every Monday, Wednesday, and Friday, you need to use the tar command to put the data of a certain website The directory is packaged as a backup file. We can use the crontab -e command to create a scheduled task without using the -u parameter when creating a scheduled task for ourselves. The specific implementation effect of the crontab -e command and the result of the crontab -l command are as follows:

crontab -e
crontab -l

It should be noted that, in addition to using commas (,) to represent multiple time periods, for example, "8,9,12" represents August, September and December. You can also use a minus sign (-) to indicate a continuous period of time (for example, if the value of the field "day" is "12-15", it means the 12th to 15th of each month). You can also use the division sign (/) to indicate the interval of executing the task (for example, "*/2" means to execute the task every 2 minutes).

If the crond service needs to include multiple command statements of scheduled tasks at the same time, only one command statement should be written in each line. For example, let's add another scheduled task, its function is to automatically clear all the files in the /tmp directory every Monday to Friday at 1 am. In particular, it should be noted that in the scheduled task parameters of the crond service, all commands must be written in the form of absolute paths. If you do not know the absolute path, please use the whereis command to query. The path to the rm command is in bold in the output below.

whereis rm
crontab -e
crontab -l

Summarize the considerations for using planning services.

➢ In the configuration parameters of the crond service, comment information is generally written at the beginning of the # sign like a shell script, so that when reviewing this command code in the future, you can quickly understand important information such as its function, requirements, and writers.

➢ The "minute" field in the scheduled task must have a value, and it must not be empty or *, and the "day" and "week" fields cannot be used at the same time, otherwise conflicts will occur.

Deleting the crond scheduled task is very simple, just use the crontab -e command to enter the editing interface and delete the text information inside. You can also use the crontab -r command to delete directly:

crontab -r
crontab -l

Guess you like

Origin blog.csdn.net/AdamCY888/article/details/131305560