Naming files with variables in Linux

  
                  

A few days ago, a Linux system administrator asked the author for help. He said they deployed an Oracle 10G database system on the Linux operating system. Now they use the data pump tool in the database system to back up data from the system. They want to copy this backup file to a specific place every day. There is no problem until this step. But they hope that during the copying process, the backup file can be named with the variable name. Named as 1backup.dmp, 2backup.dmp, etc., depending on the day of the week. The first ones and twos indicate the meaning of the day of the week. If you set this up, it will be a week of reincarnation. By the next Monday, the new backup file will replace the old backup file. In this case, the hard disk space occupied by the backup file will not increase without limit.

The author is puzzled why they do not directly use the backup tools provided by Oracle to develop a backup strategy to back up the database. However, to achieve the above use of variables to name the file, it can still be achieved. According to the needs of their enterprises, the author gave the system administrator a detailed explanation of the implementation method.

First, the relationship between file creation time and system time.

When you use variables to name a file (such as a time variable), you need to pay attention to a problem, that is, the relationship between the creation time of the file and the system time. As above, if a file was created on Monday, the file was copied on Tuesday. So what is the name of the file at this time? Is it named after the file creation time, or by the time of the copy. If you look at the above requirements, it is more reasonable to use the file creation time to name it. Because this creation time really reflects the backup time of the database.

Second, the relevant command parameters analysis.

If you want to copy a file to another location and rename it with a time variable, you can use the following command to implement cp mydb.log "(date %w)"mydb.log . The meaning of this command is to copy mydb.log to a specific location (in the current directory) and rename it. The naming rule is to add a time parameter in front of the original file variable, here is the time variable of the week.

"(date %w)" This parameter is the time variable for the day of the week. Note that the time here is based on when the file was created, not when the reference file was copied. That is, if the file was created on Wednesday and the copy time is Thursday, then the value of this variable would be 3 instead of 4. Because the file was created at a time of 3. I used to make this mistake when I first started to contact the Linux operating system. I hope that the mistakes that the author has made often can cause everyone to be vigilant.

In addition, the above time parameter must be enclosed in double quotes. But if you don't do this, then the system will prompt that this command has an error. This is a grammatical error. So if the system administrator uses this command in a batch program, then it is best to have a pre-test. Because these grammatical mistakes, even if you are an old system administrator, it is easy to make a mistake. Practice is the sole criterion for testing truth. This sentence is not wrong.

There is a small detail that is "(date %w)" is different from "(date +%w)"? This is both different and can be said to be no. This is mainly to see what occasions are used. If used in the CP command, the middle plus no (+) plus sign is the same. However, in other command situations, the middle plus sign must be added. Otherwise, the system will prompt an error. As the ECHO order I want to talk about below, there are requirements in this regard.

Third, use the Echo command to test.

If the system administrator is not sure about the command parameters that he has written, then you can use the echo command to test. The Echo command can display the value of an environment variable or the value of a particular variable. In fact, these variables are the environment variables in the system. As shown below. Use echo $(date +%w) to display the system time of the day. This command only shows the day of the week.

If you use this command, there is still a gap between the final time parameters in the cp command, mainly in the following aspects.

First, when you want to use the echo command to display the value of a system variable, you must precede the variable with a $ sign. If you add this symbol, the system will consider this to be an environment variable, or a variable that is converted from an environment variable. If you do not add this symbol, the operating system will not recognize it, and you will not be able to display the value of this variable.

Secondly, in this variable, the + sign must be added in the middle. As shown in the figure above, if you do not include this + sign, the operating system will prompt an error saying that %w is an invalid parameter. But in the cp command, there is no such mandatory limit. Adding this plus sign in the middle can achieve the same effect. However, in order to improve the accuracy of the parameters, it is best to follow the format that the echo command can recognize. Because the echo command can recognize the format, it is common among other commands. Conversely, the cells that can be used in other commands are not necessarily generic on commands such as echo. Therefore, in order to improve the portability of the scripts written, it is best to use a common variable writing method.

Copyright © Windows knowledge All Rights Reserved