Shell Script Introduction
Shell is a scripting language
- The shell has its own syntax, and can use logical judgments, loops, and so on.
Functions can be customized to reduce duplicate code
The shell is a collection of system commands
- Shell scripts enable automated operations, which can greatly increase our operational efficiency
Shell script structure and execution structure
The first line of the script must be #!/bin/bash
The behavior that begins with # in the contents of the script as an explanatory note
Write a script note: Author, time, features and other information, easy to view later
- The script name ends with ". Sh" and is used to differentiate between this is a shell script
Execution method
1. Add Execute permission "chmod a+x test.sh" to the script, then execute the procedure directly
2, bash test.sh; SH test.sh
SH parameter
- -x:sh-x test.sh Viewing the script execution process
- -n:sh-n test.sh to see if there is a syntax error in the script
Create a shell script:
[Email protected] shell]# vim 01.sh
#! /bin/bash//fixed format
echo "123"
W
Ls
Add execute permissions to the script:
[Email protected] shell]# chmod a+x 01.sh
Execute script
[Email protected] shell]#./01.sh
Abc
22:46:45 up 2:33, 1 user, load average:0.00, 0.01, 0.05
USER TTY from [email protected] IDLE jcpu PCPU
Root pts/0 192.168.159.1 20:17 5.00s 0.06s 0.00s/bin/bash./01.sh
01.sh
In the current terminal, put the #! in 01.sh /bin/bash after the execution of the script, you will see that the results are the same, no problem, this means that the machine is able to recognize the inside of a single command, to run the command inside, but if you change a machine, it will not be able to carry out
- In the first line, the file header specifies #!/bin/bash, and the next command to run is the interpreter that is used to operate it, usually performed by the/bin/bash interpreter.
Date command usage
The date command is used to display or set the system time and date
Syntax: Date option parameter
Options
- -D: Displays the date and time specified by the string (double quotation marks must precede and before the string)
- -S: Sets the time and date based on the string (double quotation marks must be added before and after the string)
Parameters
Show Current time:
[[Email protected] ~]# Date
Wednesday, February 07, 2018 09:56:28 CST
To view the system calendar:
[[email protected] ~]# cal
February on 2018
Day 123456
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
Cal-y can view a calendar of one year
+ Time Date format: Specify the format of the date and time display
Date +%y (%Y): Displays the year in four-bit (two-bit) number format
[Email protected] ~]# date +%y
2018
[Email protected] ~]# date +%y
18
Date "+%y-%m-%d%h:%m:%s%w"
means: Year, month, day, time, minute, second, week
[Email protected] ~]# Date "+%y-%m-%d%h:%m:%s%w"
2018-02-07 10:02:43 3
When the above parameter combination, there is a special symbol in the middle to add double quotation marks
Date +%f: Displays the full month day
[Email protected] ~]# date +%f
2018-02-07
Date +%w: Shows the current time is the week ordinal of a year
[Email protected] ~]# date +%w
06
Date +%t: Shows the current time
[Email protected] ~]# date +%t
10:05:50
Date +%s: Timestamp
Displays the number of seconds from January 1, 1970 00:00:00 to the current experience
[Email protected] ~]# date +%s
1517969292
Time Stamp conversion:
[[Email protected] ~]# date +%s-d "20180207 10:10:00"
1517969400
[Email protected] ~]# date-d @1517969400
Wednesday, February 07, 2018 10:10:00 CST
Print specified date & time
Sometimes you need to use the date or time before n days (hours, minutes, seconds)
Two days ago:
Date-d "-2 Day"
[[Email protected] ~]# Date
Wednesday, February 07, 2018 10:14:10 CST
[[email protected] ~]# date-d "-2 Day"
Monday, February 05, 2018 10:14:13 CST
Two days later:
Date-d "+2 Day"
[[Email protected] ~]# Date
Wednesday, February 07, 2018 10:14:58 CST
[[email protected] ~]# date-d "+2 Day"
Friday, February 09, 2018 10:15:02 CST
Two months a year before:
Date-d "-1 year-2 month-1 Day"
[[Email protected] ~]# Date
Wednesday, February 07, 2018 10:16:32 CST
[[email protected] ~]# date-d "-1 year-2 month-1 Day"
December 06, 2016 Tuesday 10:16:34 CST
Time setting
Manual setup Time: Date-s "year-month-day time: minutes: Seconds"
[[email protected] ~]# date-s "2017-01-01 12:00:00"
Sunday, January 01, 2017 12:00:00 CST
[[Email protected] ~]# Date
Sunday, January 01, 2017 12:00:02 CST
Synchronize Network time: Ntpdate command
[[email protected] ~]# Yum install-y NTP
Install the Ntpdate command
[Email protected] ~]# ntpdate ntp1.aliyun.com
7 Feb 10:24:16 ntpdate[2681]: Step time server 182.92.12.11 offset 34726773.904725 sec
[[Email protected] ~]# Date
Wednesday, February 07, 2018 10:24:24 CST
Ntpdate back with NTP time server address
Variables in shell scripts
When a string is used more frequently in a script and the string length is long, you should use a variable instead of the string.
When using conditional statements, you often use variable if[$a-gt 1];then ...; Fi
Replace n= ' Wc-l 1.txt ' with a variable when referencing the result of a command
When writing and user interaction scripts, variables are also essential for read-p "input a number:" N; Echo $n If you don't write this n, you can use ¥reply directly.
Built-in variable $0,$1,$2 ... $ A represents the script itself, the first argument, the second argument of $ $n represents the nth parameter
- Mathematical Operations a=1;b=2;c=$ (($a + $b)) or $[$a + $b]
Shell scripting introduction, Structure and execution, date command usage, variables in shell scripts