Shell script description, shell script structure and execution, date command usage, variables in shell scripts

Source: Internet
Author: User
Shell script introduction
The shell is a scripting language aming_linux (public account) blog.lishiming.net (blog)
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. Shell script structure and execution
You need to add #! / Bin / bash at the beginning, which means that the next statement is parsed by this file, because with it we can execute ./1.sh like this, or only / bin / bash 1.sh
[[email protected] shell] # chmod a + x 1.sh
[[email protected] shell] # ./1.sh
123
22:37:09 up 4:02, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts / 0 192.168.21.1 22:29 5.00s 0.10s 0.00s / bin / bash ./1.sh
root pts / 1 192.168.21.1 19:00 3: 31m 0.08s 0.08s -bash
1.sh
[[email protected] shell] # / bin / bash 1.sh
123
22:37:27 up 4:03, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts / 0 192.168.21.1 22:29 7.00s 0.10s 0.00s / bin / bash 1.sh
root pts / 1 192.168.21.1 19:00 3: 31m 0.08s 0.08s -bash
1.sh
The lines starting with # are used as explanations, except for the first line of #! / Bin / bash and some special scripts
The name of the script ends with .sh to distinguish this is a shell script
There are two methods of execution
chmod + x 1.sh; ./1.sh
sh 1.sh
sh -x 1.sh shows the execution process

[[email protected] shell] # sh -x 1.sh
echo 123
123
w
22:43:05 up 4:08, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts / 0 192.168.21.1 22:29 1.00s 0.11s 0.00s sh -x 1.sh
root pts / 1 192.168.21.1 19:00 3: 37m 0.08s 0.08s -bash
ls
1.sh
The + sign is the executed statement
sh -n 1.sh can check the syntax of the script date command usage
year month day
[[email protected] shell] # date +% Y-% m-% d
2018-07-11
[[email protected] shell] # date +% y-% m-% d
18-07-11
[[email protected] shell] # date +% Y% m% d
20180711
[[email protected] shell] # date +% D
07/11/18
[[email protected] shell] # date +% F
2018-07-11
date +% s ?? timestamp, how many seconds have passed since 0: 0 on January 1, 1970, and conversion between timestamp and time
[[email protected] shell] # date +% s
1531320917
[[email protected] shell] # date -d @ 1531321618
Wednesday, July 11, 2018 23:06:58 CST
[[email protected] shell] # date +% s -d "2018-07-10 23:06:58"
1531235218
time
[[email protected] shell] # date +% H:% M:% S
22:57:20
[[email protected] shell] # date +% T
22:57:32
week
[[email protected] shell] # date +% w
3
[[email protected] shell] # date +% W
28
W is the first week of the year
How to log yesterday's date
[[email protected] shell] # date -d "-1 day" +% F
2018-07-10
[[email protected] shell] # date -d "-1 month" +% F
2018-06-11
[[email protected] shell] # date -d "-1 year" +% F
2017-07-11
[[email protected] shell] # date -d "-1 hour" +% T
22:05:55
[[email protected] shell] # date -d "+1 day" +% F
2018-07-12
View calendar
[[email protected] shell] # cal
  July 2018
Day one two three four five six
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
Variables in shell scripts
When a string is used frequently in a script and the length of the string is long, you should use variables instead
When using conditional statements, often use variables?? If [$ a -gt 1]; then ...; fi
When referring to the result of a command, use variables instead?? N = wc -l 1.txt
Variables are also essential when writing scripts that interact with users ?? read -p "Input a number:" n; echo $ n?? If you didn'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
Mathematical operations a = 1; b = 2; c = $ (($ a + $ b)) or $ [$ a + $ b]
Introduction to shell script, shell script structure and execution, date command usage, variables in 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.