Use variables in Linux to name files

Source: Internet
Author: User
Tags echo command
Article Title: naming a file using variables in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Recently, a Linux system administrator asked me for help. He said that they deployed an Oracle 10 Gb 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 the backup file to a specific place every day. No problem till now. However, they want to use the variable name to name the backup file during the replication process. For example, 1backup. dmp, 2backup. dmp, and so on. The first two represent the day of the week. In this case, the cycle is one Monday. By 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 be increased without limit.

I don't know why they didn't directly use the backup tools provided by Oracle to create backup policies for databases. However, the above variables can still be used to name the file. The author elaborated on the implementation methods for the system administrator according to their enterprise needs.

 1. Relationship between file creation time and system time.

When using variables to name a file (such as a time variable), you must note the relationship between the File Creation Time and the system time. As shown in the preceding requirement, if a file was created on Monday and copied on Tuesday. What is the file name? Whether the name is based on the file creation time or the copy time. From the above requirement, it is more reasonable to use the file creation time to name the file. This creation time truly reflects the database backup time.

 2. parsing related command parameters.

If you want to copy a file to another place and rename it using time variables, you can use the following command to implement cp mydb. log "(date % w)" mydb. log. This command copies mydb. log to a specific location (under the current directory) and renames it. The naming rule is to add a Time Parameter before the original file variable. The week time variable is used here.

"(Date % w)" is the time variable of the day of the week. Note that the time here is based on the file creation time, instead of referring to the file copy time. That is to say, if the file is created on Wednesday and copied on Thursday, the variable value is 3, not 4. Because the file is created at 3. I used to make this mistake when I first started to access the Linux operating system. I hope that the common mistakes made by people who have come over can arouse everyone's vigilance.

In addition, the preceding time parameter must be enclosed in double quotation marks. Otherwise, the system will prompt that this command has an error. This is a syntax error. Therefore, if the system administrator uses this command in the batch processing program, it is best to perform a pre-test. Because of these syntax errors, even the old system administrator is easy to make. Practice is the only criterion for testing truth. This sentence will not be wrong.

Is there a small difference between "(date % w)" and "(date + % w? There are both differences and none. This mainly depends on the application. If it is used in the CP command, the plus sign (+) is not added in the middle, which is the same. However, in other command cases, the plus sign must be added in the middle. Otherwise, the system will prompt an error. For example, the ECHO command that I want to talk about below has this requirement.

  3. Use the Echo command for testing.

If the system administrator is not certain about the command parameters he has compiled, he can use the echo command to test the parameters. The Echo command can display the values of environment variables or specific variables. In fact, these variables are the environment variables in the system. As shown in. Echo $ (date + % w) can be used to display the system time of the current day. This command only displays the day of the week.


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

First, to use the echo command to display the system variable value, you must add the $ symbol before the variable. If this symbol is added, the system considers it an environment variable or a variable converted from the environment variable. If this symbol is not added, the operating system will not be able to identify it, and the value of this variable will not be displayed normally.

In this variable, the + number must be added in the middle. As shown in, if this + number is not added, the operating system will prompt an error saying % w is an invalid parameter. But in the cp command, there is no such restriction. Adding this plus sign in the middle can achieve the same effect. However, to improve the accuracy of parameters, it is recommended that you follow the format that can be recognized by the echo command. Because the echo command can recognize the format, it is common in other commands. On the contrary, the cells that can be used in other commands are not necessarily common in echo and other commands. Therefore, in order to improve the portability of the written script program, it is best to use a common variable writing method.

 

[1] [2] Next page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.