* Simple Script test-create a file using date *

Source: Internet
Author: User
Tags date1

Imagine that my server has a database, and the data in the database is different every day. So when I back up the database, I want to back up the data every day to a different file name, in this way, the old data can be saved and not overwritten. Wow! Different file names! This is really a problem? Do I need to modify the script every day?

No! The daily "date" is not the same, so I can name the file like backup.2011-08-15.data. Isn't it possible to have a different file name every day? Haha! Indeed. How did you get this? That's the point! The following example shows how to create three empty files.Touch)
, The beginning of the file name is determined by the user input. if the user has entered filename, the current date is. I want to create these files on the previous day, yesterday, and today, that is, filename_20151113, filename_20151114, and filename_20151115. How can this problem be solved?

 

#! /Bin/bash
# Program creates three files, which named by user's input and date command.
# History:
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path

#1. Ask the user to enter the file name and obtain the fileuser variable;
Echo-e "I will use 'touch' command to create 3 files." # purely display information
Read-P "Please input your filename:" fileuser # Prompt user input

#2. To prevent users from pressing enter at will, is there any configuration for the file name using the variable function?
Filename =$ {fileuser:-"FILENAME"} # Start to determine whether there is a configuration file name

#3. start to use the date command to obtain the required file name;
Date1 = $ (Date
-- Date = '2 Days ago '+ % Y % m % d)
# Date of the previous two days
Date2 = $ (Date
-- Date = '1 days ago'+ % Y % m % d) # date of the previous day
Date3 = $ (date + % Y % m % d) # today's date
File1 =$ {filename }$ {date1} # Name of the configuration file in the following three rows
File2 =$ {filename }$ {date2}
File3 =$ {filename }$ {date3}

#4. Create a file name!
Touch "$ file1" # create a file in the following three lines
Touch "$ file2"
Touch "$ file3"

 

Test:

[Root @ sor_sys test] #Sh datefile. Sh
I will use 'touch' command to create 3 files.
Please input your filename: bakupmas
[Root @ sor_sys test] # ll
Total 12
-RW-r -- 1 Root 0 Aug 15 bakupmas20151120.13
-RW-r -- 1 Root 0 Aug 15 bakupmas20151120.14
-RW-r -- 1 Root 0 Aug 15 16:51
Bakupmas20151115

-RW-r -- 1 Root 1015 Aug 15 16:35 datefile. Sh
-RW-r -- 1 Root 248 Aug 15 echo-e.sh
-RW-r -- 1 Root 434 Aug 15 read-p.sh
[Root @ sor_sys test] #

++ ++

If you do not enter a carriage return, the file filename20151116 filename20151117filename20151118 will be generated?

Variable configuration methodSTR is not configured as a Null String. STR is configured as a non-null string.

VaR =$ {STR:-expr}Var = expr Var = $ Str

++ ++

The reason listed above can be explained:

If you enter a non-empty value, it indicates that STR has been set to a non-empty character, then VaR is $ Str

If you press enter directly, that is, enter an empty string, then var takes expr

In this case, do you understand?

 

 

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.