Shell script describes the shell script structure and executes the variables in the shell script of the date command usage

Source: Internet
Author: User

First, Shell script introduction

Shell scripts to write well, you must continue to practice writing to write well, there is no shortcut
To have a general idea of a script when we get a demand, think of how the need to achieve
Shell scripting can greatly improve our productivity
Second, shell script structure and execution

[[email protected] ~]# mkdir Shell//Create a Shell folder that holds the shell script for the experiment
[Email protected] ~]# CD shell/
[[email protected] shell]# ls
[[Email protected] shell]# VI first script
echo "123"
The first line must be written in this format, if the script is executed on this machine, the first line can be omitted, but if it is to be executed on other machines, it must be written, and we must specify which interpreter to use for the next command.
[[Email protected] shell]# sh under script, can execute normally
00:00:46 up 2:35, 1 user, load average:0.14, 0.06, 0.06
USER TTY from [email protected] IDLE jcpu PCPU
Root pts/0 23:37 6.00s 0.20s 0.02s W
You can also give the script permission to execute
[Email protected] shell]# chmod a+x
[[email protected] shell]#/ use./To execute the script, to execute the instructions these commands are parsed, and are recognized.
00:02:33 up 2:37, 1 user, load average:0.02, 0.04, 0.05
USER TTY from [email protected] IDLE jcpu PCPU
Root pts/0 23:37 1.00s 0.23s 0.01s W
In the second line of the script, another line #!/bin/bash is recognized as an explanatory statement.
#written by aming//explanation
#echo W LS//explanation
echo "123"
[[email protected] shell]# vi/etc/init.d/network//View the network script inside the system
#! /bin/bash
#network Bring Up/down Networking
#chkconfig: 2345 10 90//This has a special meaning, 2345 is the start level that defines it, 10 is the boot order, and 90 is the order of the shutdown
#description: activates/deactivates All network interfaces configured to \//scripting explanation
#start at boot time. Without these two lines, there's no way to join the Chkconfig list.
Lines starting with # as explanations, can write copyrights, write dates, explain what the script does, etc.
The name of the script ends with a. SH and is used to differentiate between this is a shell script
Two ways to execute shell scripts;
1, bash or sh
2, chmod +x, and then use the./,./is a relative path, can also be directly written absolute path to find this script directly executed can also #/root/shell/ to execute script
Sh-x, where the-X option is the process of displaying shell script execution, each + represents an action
[Email protected] shell]# sh-x

    • Echo 123
    • W
      07:10:42 up 3:29, 2 users, load average:0.01, 0.02, 0.05
      USER TTY from [email protected] IDLE jcpu PCPU
      Root pts/0 23:37 7:03m 0.30s 0.30s-bash
      Root pts/1 06:27 2.00s 0.10s 0.00s sh-x
    • Ls
      [[email protected] shell]# sh-n the-n option to see if the shell script has syntax errors and only detects syntax errors

Iii. use of the date command

Date is a great role in the shell, such as marking a log in a script, or making changes to a file, you can use date to decorate it, such as backing up an SQL file every day, plus a date, to know which day the SQL file was generated.
You can also generate backup files by week, For example, Monday generated 1.sql, Tuesday generated 2.sql and so on, generate 7 backup files per week, the next week to regenerate into a new 1.sql file automatically the last week to cover up the 1.sql files, do not need us to delete the backup file manually, it can automatically overwrite the name of the same file, so date is very useful in the shell, Practical.
[[Email protected] ~]# Date +%y//use%Y to represent four-digit year
[[Email protected] ~]# Date +%y//use%y to represent two-digit years
[[Email protected] ~]# Date +%m//use%m to represent month
[[Email protected] ~]# Date +%m//Use%M to represent minutes
[[Email protected] ~]# Date +%d//Use%d to indicate dates
[[Email protected] ~]# Date +%d//use%d to denote month/day/year date
[[Email protected] ~]# Date +%y%m%d//combine%y%m%d and use the date
[[Email protected] ~]# Date +%f//Use%F to indicate the date with the bars, the more friendly display
[[Email protected] ~]# Date +%h//use%H to denote hours
[[Email protected] ~]# Date +%s//use%s to represent seconds
[[Email protected] ~]# Date +%s//use%s for timestamp, from January 1, 1970 0:0 to now how many seconds have passed
[[Email protected] ~]# Date +%t//use%T to represent time
[[Email protected] ~]# Date +%h%m%s//use%h%m%s to represent seconds and minutes
[[Email protected] ~]# Date +%h:%m:%s//middle plus colon display more friendly, equivalent to%t
[[Email protected] ~]# Date +%w//Use%w to indicate weeks
[[Email protected] ~]# Date +%w//Use%W to indicate how many weeks today is this year
[[email protected] ~]# Cal//cal command can display calendar
July 2018
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
For example, in the Nginx cutting log, it will be in the daily 0 o'clock in the morning to cut the day before the log, this time to mark the date is the day before the date, this time can be used to mark yesterday's date:
[[email protected] ~]# date-d "-1 day"//This shows the date of yesterday
Fri Jul 16:40:59 CST 2018
[[email protected] ~]# date-d "-1 day" +%f//Plus%F more friendly display date
[[email protected] ~]# date-d "-1 month" +%f//using month means last month, January ago
[[email protected] ~]# date-d "-1 Years" +%f//using years means last year, same day and month can add s
[[email protected] ~]# date-d "-1 Year" +%f//Use the same year effect
[[email protected] ~]# date-d "-1 hour" +%t//using hour means an hour ago
[[email protected] ~]# date-d "-1 min" +%t//using min means a minute ago, seconds can be expressed as such
[[Email protected] ~]# date +%s-d "2018-07-14 16:55:23"//Convert specific time to timestamp
[[email protected] ~]# date-d @1531558523//In turn, the timestamp can be converted to a specific time
Sat Jul 16:55:23 CST 2018

Iv. variables in shell scripts

The shell script is full of variables, simply put, the variable is actually a change of parameters, a value, a string, you can use it repeatedly, call it

Shell script describes the shell script structure and executes the variables in the shell script of the date command usage

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: 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.