20.1 Shell Script Introduction
- Shell is a scripting language (Shell Script Learning: blog.lishiming.net)
- 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
20.2 shell script Structure and execution
- Shell scripts need to be #!/bin/bash at the beginning (the purpose is to specify the parser so that it performs better when porting to other machines).
- Lines beginning with # are interpreted (the system does not execute).
- The habit is that the name of the shell script ends with. SH and is used to differentiate between this is a shell script.
- There are two ways to execute a shell script:
(1) chmod +x 1.sh;./1.sh
(2) Bash 1.sh
- Plus-x parameter to view the script execution process
Bash-x 1.sh
- Add-n parameter to see if the script has syntax errors
Bash-n 1.sh
20.3 Date Command usage
- Date +%y-%m-%d (= Date +%f), date +%y-%m-%d Month Day
- Date +%h:%m:%s = Date +%t time
- Date +%s display timestamp
- Date +%s-d "2018-04-17 17:42:34" converts datetime to timestamp
- Date-d @1504620492 convert Timestamps to datetime
- Date-d "+1day" +%f a day later
- Date-d "-1 days" a day ago
- Date-d "-1 month" month ago
- Date-d "-1 hour" +%t an hour ago
- Date-d "-1 min" A minute ago
- Date +%w Day of the week
- Date +%w The first week of this year
- Cal Display Calendar
20.4 variables in a shell script
- 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
- Replace n= ' Wc-l 1.txt ' with a variable when referencing the result of a command
- Variables are also necessary when writing scripts that interact with users, such as:
Read-p "Input a number:" N; Echo $n
If you do not 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]
2018-4-17 Linux Learning Notes