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 01.sh//create first script 01.sh
#!/bin/bash
echo "123"
W
Ls
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 01.sh//Run under 01.sh script, can execute normally
123
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 192.168.238.1 23:37 6.00s 0.20s 0.02s W
01.sh
You can also give the 01.sh script permission to execute
[Email protected] shell]# chmod a+x 01.sh
[[email protected] shell]#/01.sh//Direct use./To execute the script, to execute the instructions these commands are parsed, and are recognized.
123
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 192.168.238.1 23:37 1.00s 0.23s 0.01s W
01.sh
In the second line of the script, another line #!/bin/bash is recognized as an explanatory statement.
#!/bin/bash
#written by aming//explanation
#2018-07-13//explanation
#echo W LS//explanation
echo "123"
W
Ls
[[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 01.sh or sh 01.sh
2, chmod +x 01.sh, and then use the./01.sh,./is a relative path, can also be directly written absolute path to find this script directly executed can also #/root/shell/01.sh to execute 01.sh script
Sh-x 01.sh, where the-X option is the process of displaying shell script execution, each + represents an action
[Email protected] shell]# sh-x 01.sh
- Echo 123
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 192.168.238.1 23:37 7:03m 0.30s 0.30s-bash
Root pts/1 192.168.238.1 06:27 2.00s 0.10s 0.00s sh-x 01.sh
- Ls
01.sh
[[email protected] shell]# sh-n 01.sh//Use 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
2018
[[Email protected] ~]# Date +%y//use%y to represent two-digit years
18
[[Email protected] ~]# Date +%m//use%m to represent month
07
[[Email protected] ~]# Date +%m//Use%M to represent minutes
53
[[Email protected] ~]# Date +%d//Use%d to indicate dates
14
[[Email protected] ~]# Date +%d//use%d to denote month/day/year date
07/14/18
[[Email protected] ~]# Date +%y%m%d//combine%y%m%d and use the date
20180714
[[Email protected] ~]# Date +%f//Use%F to indicate the date with the bars, the more friendly display
2018-07-14
[[Email protected] ~]# Date +%h//use%H to denote hours
15
[[Email protected] ~]# Date +%s//use%s to represent seconds
07
[[Email protected] ~]# Date +%s//use%s for timestamp, from January 1, 1970 0:0 to now how many seconds have passed
1531551793
[[Email protected] ~]# Date +%t//use%T to represent time
16:23:53
[[Email protected] ~]# Date +%h%m%s//use%h%m%s to represent seconds and minutes
162715
[[Email protected] ~]# Date +%h:%m:%s//middle plus colon display more friendly, equivalent to%t
16:27:28
[[Email protected] ~]# Date +%w//Use%w to indicate weeks
6
[[Email protected] ~]# Date +%w//Use%W to indicate how many weeks today is this year
28
[[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
2018-07-13
[[email protected] ~]# date-d "-1 month" +%f//using month means last month, January ago
2018-06-14
[[email protected] ~]# date-d "-1 Years" +%f//using years means last year, same day and month can add s
2017-07-14
[[email protected] ~]# date-d "-1 Year" +%f//Use the same year effect
2017-07-14
[[email protected] ~]# date-d "-1 hour" +%t//using hour means an hour ago
15:48:44
[[email protected] ~]# date-d "-1 min" +%t//using min means a minute ago, seconds can be expressed as such
16:51:13
[[Email protected] ~]# date +%s-d "2018-07-14 16:55:23"//Convert specific time to timestamp
1531558523
[[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