Shell Script Introduction
Shell is a scripting language
You can use logical judgments, loops, and other syntax
Functions can be custom-defined
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
Need to add #!/bin/bash at the beginning
Lines that begin with # are interpreted as explanatory notes
The name of the script ends with. SH and is used to differentiate between this is a shell script
There are two methods of executing
chmod +x 1.sh;./1.sh
Bash 1.sh
View the script execution process bash-x 1.sh
See if the script is syntactically incorrect bash-n 1.sh
Date command usage
[[email protected]]# Datefri Apr 19:13:31 CST 2018[[email protected]]# Date +%y small y represents year 18[[EMAIL PR otected]]# Date +%y Large Y shows more full 2018[[email protected]]# date +%m small m represents month 07[[email protected]]# date +%m The large m represents the minute 14[[email protected]]# date +%d small D represents the day 11[[email protected]]# date +%d Large D displays more comprehensive 07/11/08[[email protected]]# date +%y%m%d This format displays the date 20180711[[email protected]]# date +%f The role of the large F 2018-07-11[[email protected]]# Date +%h large H represents the hour 19[[email protected]]# date +%s represents the time The timestamp 1524222940[[email protected]]# date +%s large S represents the second 45[[email protected]]# date +%t large T represents the second minute 19: 16:58[[email protected]]# Date +%w Small W represents the week of one weeks 3[[email protected]]# the date +%w large W represents the first week of the year 28[[ema il protected]]# date-d "-1 day" +%f-1 Day means 2018-07-11[[email protected]]# date-d "-1 month" +%f- 1 month indicates the first one months 2018-06-11[[email protected]]# date-d "-1 years" +%f-1 years for the previous year 2017-07-11
[[email protected]]# date +%s #时间戳1531378819[[email protected]]# date -d @1531378819 #通过时间戳换算出时间2018年 07月 11日 星期三 20:00:19 CST[[email protected]]# date +%s -d "2018-07-11 20:00:19" #通过时间戳算出时间1531378819
Variables in shell scripts
You should use a variable instead when you use a string more frequently in your script and the string length is long
When using conditional statements, the variable if [$a-gt 1] is often used; Then ...; Fi
When referencing the result of a command, replace n= with a variablewc -l 1.txt
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 variables $, $ $, $ ... $ $ represents the script itself, the first parameter, the $ A second ... $ #表示参数个数
Mathematical Operation a=1;b=2; c=$ (($a + $b)) or $[$a + $b]
Shell script Introduction, script structure and execution, date command usage, variables in scripts