20.1 shell script introduces 20.2 shell script structure and executes 20.3 date command usage 20.4 A variable in a shell script

Source: Internet
Author: User


-20.1 Shell Script Introduction
-20.2 Shell script structure and execution
-20.3 date command usage
-20.4 variables in shell scripts
# 20.1 Shell script introduction
-Shell is a scripting language Follow aming_linux blog.lishiming.net
-Can use syntax such as logical judgment, loop, etc.
-Can customize functions
-shell is a collection of system commands
-Shell script can realize automatic operation and maintenance, which can greatly increase our operation and maintenance efficiency








# 20.2 Shell script structure and execution
-You need to add #! / Bin / bahs at the beginning // Tell the system which interpreter this script uses to operate
-Lines beginning with # as explanation
-The name of the script ends with .sh to distinguish this is a shell script
-Create a shell directory first
`` `
[[email protected] ~] # clear
[[email protected] ~] # mkdir shell
[[email protected] ~] # cd shell /
[[email protected] shell] # ls
[[email protected] shell] # vi 01.sh

#! / bin / bash
echo "123"
w
ls
~
                                                                                   
~
: wq
[[email protected] shell] # vi 01.sh
`` `

-There are two implementation methods:
chmod + x 1.sh; ./1.sh
bash 1.sh, sh 1.sh
`` `
[[email protected] shell] # sh 01.sh
123
 21:07:19 up 5 min, 2 users, load average: 0.04, 0.33, 0.20
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root tty1 21:02 4:39 0.02s 0.02s -bash
root pts / 0 192.168.202.1 21:03 7.00s 0.03s 0.01s w
01.sh
[[email protected] shell] #
`` `
-Another way, the beginning of the file means that the next command is parsed by this file.
`` `
[[email protected] shell] # chmod a + x 01.sh
[[email protected] shell] # ./01.sh
123
 21:09:48 up 7 min, 2 users, load average: 0.14, 0.23, 0.18
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root tty1 21:02 7:08 0.02s 0.02s -bash
root pts / 0 192.168.202.1 21:03 4.00s 0.03s 0.00s / bin / bash ./01.sh
01.sh
[[email protected] shell] #
[[email protected] shell] # cat 01.sh
#! / bin / bash
echo "123"
w
ls
[[email protected] shell] #

`` `
-bin / bash is actually bin / sh
`` `
[[email protected] shell] # ls -l / bin / bash
-rwxr-xr-x. 1 root root 960392 August 3 2016 / bin / bash
[[email protected] shell] # ls -l / bin / sh
lrwxrwxrwx. 1 root root 4 October 5 22:18 / bin / sh-> bash
[[email protected] shell] #

`` `

-View the script execution process, each + represents a step
`` `
[[email protected] shell] # sh -x 01.sh
+ echo 123
123
+ w
 21:17:01 up 14 min, 2 users, load average: 0.02, 0.07, 0.12
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root tty1 21:02 14:21 0.02s 0.02s -bash
root pts / 0 192.168.202.1 21:03 5.00s 0.05s 0.00s sh -x 01.sh
+ ls
01.sh
[[email protected] shell] #
`` `
-You can also use sh -n to see if the syntax of the script is correct
-No output, indicating no errors

`` `
[[email protected] shell] # sh -n 01.sh
[[email protected] shell] #
`` `
-Modify the file to test the error
`` `
[[email protected] shell] # sh -n 01.sh
[[email protected] shell] # vi 01.sh

#! / bin / bash
echo "123"
w
ls
for i in `seq 1 10`
do
 echo $ i
~
                                                                                      
~
~
: wq

`` `
-Run sh -n again
`` `
[[email protected] shell] # sh -n 01.sh
01.sh: line 8: syntax error: unexpected end of file
[[email protected] shell] #
`` `
-Change back
`` `
[[email protected] shell] # vi 01.sh

#! / bin / bash
echo "123"
w
ls
~
~
~
                                                                                      
: wq
`` `
-Monitoring is useless
`` `
[[email protected] shell] # sh -n 01.sh
[[email protected] shell] #
`` `







# 20.3 date command usage
-date is very useful in shell; add a time to file suffix for later management
-date +% Y-% m-% d, date +% y-% m-d
`` `
[[email protected] ~] # date
Monday, November 20, 2017 22:01:01 CST
[[email protected] ~] #
`` `
-m months, M minutes
`` `
[[email protected] ~] # date
Monday, November 20, 2017 22:01:01 CST
[[email protected] ~] # date +% Y
2017
[[email protected] ~] # date +% y
17
[[email protected] ~] # date +% m
11
[[email protected] ~] # date +% M
27
[[email protected] ~] #
`` `
-Date d Big D
`` `
[[email protected] ~] # date +% d
20
[[email protected] ~] # date +% D
11/20/17
[[email protected] ~] #
`` `
-Date, year, month, day date +% Y% m% d
`` `
[[email protected] ~] # date +% Y% m% d
20171120
[[email protected] ~] #
`` `
-Date +% F
`` `
[[email protected] ~] # date +% F
2017-11-20
[[email protected] ~] #
`` `
-Hours, minutes, seconds
-s represents a timestamp indicating how many seconds have elapsed since January 1, 1970, 0: 0
`` `
[[email protected] ~] # date +% H
twenty two
[[email protected] ~] # date +% s
1511188397
[[email protected] ~] # date +% S
twenty two
[[email protected] ~] # date +% S
32
[[email protected] ~] #
`` `
-About time
-date +% T
-date +% H% M% S
`` `
[[email protected] ~] # date +% T
22:35:38
[[email protected] ~] # date +% H% M% S
223608
[[email protected] ~] #
`` `
-h shows the month,
`` `
[[email protected] ~] # date +% h
November
[[email protected] ~] #
`` `
-Change language to English first
`` `
[[email protected] ~] # LANG = en
[[email protected] ~] # date +% h
Nov
[[email protected] ~] #
`` `
-Add a colon date +% H:% M:% S is equivalent to date +% T
`` `
[[email protected] ~] # date +% H:% M:% S
22:42:38
[[email protected] ~] # date +% T
22:42:42
[[email protected] ~] #
`` `
-Week
-w day
-W weeks of the year
`` `
[[email protected] ~] # date +% w
1
[[email protected] ~] # date +% W
47
[[email protected] ~] #
`` `
-Show calendar cal
`` `
[[email protected] ~] # cal
    November 2017
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

[[email protected] ~] #
`` `
-date -d "+1 day" +% F one day later
-date -d "-1 day" +% F a day ago
-date -d "-1 month" +% F a month ago
-date -d "-1 min" +% F one minute ago

-date -d "-1 year" +% F one year ago

-Mark yesterday's date date -d "-1 day"
`` `
[[email protected] ~] # date
Mon Nov 20 23:07:38 CST 2017
[[email protected] ~] # date -d "-1 day"
Sun Nov 19 23:07:46 CST 2017
[[email protected] ~] #
[[email protected] ~] # date -d "-1 day" +% F
2017-11-19
[[email protected] ~] #

`` `
-Last month date -d "-1 month"
`` `
[[email protected] ~] # date -d "-1 month" +% F
2017-10-20
[[email protected] ~] #
`` `
-Last year date -d "-1 year"
`` `
[[email protected] ~] # date -d "-1 year" +% F
2016-11-20
[[email protected] ~] # date -d "-1 year"
Sun Nov 20 23:10:43 CST 2016
[[email protected] ~] #
`` `
-Last hour date -d "-1 hour"
`` `
[[email protected] ~] # date -d "-1 hour" +% T
22:11:43
[[email protected] ~] #
`` `
-Timestamp is converted to specific date and time
`` `
[[email protected] ~] # date +% s
1511190759
[[email protected] ~] #

[[email protected] ~] # date -d @ 1511190759
Mon Nov 20 23:12:39 CST 2017
[[email protected] ~] #
[[email protected] ~] # date +% s -d "2017-11-20 23:12:39"
1511190759
[[email protected] ~] #
`` `








# 20.4 variables in shell scripts
-Variables should be used when a certain string is used frequently in a script
-When using conditional statements, often use the variable if [$ a -gt 1]; then ...; fi
-When referring to the result of a command, replace n = `wc -l 1.txt` with a variable
-Variables are also essential when writing scripts that interact with users. Read -p "Input a number:" n; echo $ n If you don't write this n, you can use $ REPLY directly
-Built-in variables $ 0, $ 1, $ 2 ... $ 0 represents the script itself, $ 1 is the first parameter, $ 2 is the second .... $ # is the number of parameters
-Math operations a = 1; b = 2; c = $ (($ a + $ b)) or $ [$ a + $ b]  





20.1 shell script introduces 20.2 shell script structure and executes 20.3 date command usage 20.4 A variable in a shell script


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: info-contact@alibabacloud.com 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.