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

Source: Internet
Author: User

Shell Script Introduction

Shell is a scripting language blog.lishiming.net (Amin blog, you can go inside to find the shell problem)
You can use logical judgments, loops, and other syntax
Functions can be customized to reduce duplication of 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

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
Write a simple script
#!/bin/bash
#Linletao
#2018-5-29
Echo llt
W

There are two methods of executing
1.chmod +x 1.sh;./1.sh
[Email protected] shell]# chmod +x 1.sh
[Email protected] shell]#./1.sh
Llt
19:50:36 up min, 1 user, load average:0.00, 0.01, 0.05
USER TTY from [email protected] IDLE jcpu PCPU
Root pts/0 192.168.218.1 19:40 4.00s 0.10s 0.00s/bin/bash./1.sh

2.bash 1.sh
[Email protected] shell]# bash 1.sh
Llt
19:51:10 up to Min, 1 user, load average:0.00, 0.01, 0.05
USER TTY from [email protected] IDLE jcpu PCPU
Root pts/0 192.168.218.1 19:40 6.00s 0.10s 0.01s W

3.sh 1.sh
[[Email protected] shell]# sh 1.sh
Llt
19:52:10 up min, 1 user, load average:0.00, 0.01, 0.05
USER TTY from [email protected] IDLE jcpu PCPU
Root pts/0 192.168.218.1 19:40 2.00s 0.11s 0.01s W

View the script execution process bash-x 1.sh
[Email protected] shell]# bash-x 1.sh

    • Echo llt
      Llt
    • W
      19:52:49 up min, 1 user, load average:0.00, 0.01, 0.05
      USER TTY from [email protected] IDLE jcpu PCPU
      Root pts/0 192.168.218.1 19:40 1.00s 0.10s 0.00s bash-x 1.sh

See if the script is syntactically incorrect bash-n 1.sh
Write an error script
#!/bin/bash
#Linletao
#2018-5-29
Echo llt
W
For i in ' SEQ 1 10 '
Do
Echo $i
There's no end in this.
[Email protected] shell]# bash-n 1.sh
1.sh: Line 10: syntax error: unexpected end of file
This will detect the syntax error of the script.

Date command usage

Date +%y-%m-%d, date +%y-%m-%d Month Day
Y is a four-bit year, such as 2017. Y is a two-bit year, such as 17
M is the month. M for minutes
D is the date. D can be directly tagged date, such as Date +%d 05/29/18

We can also combine them together
[Email protected] shell]# date +%y%m%d
180529

It can also be done, data+%f
[Email protected] shell]# date +%f
2018-05-29

Date +%h:%m:%s = Date +%t time
[Email protected] shell]# date +%h:%m:%s
20:31:26
H for hours
M for minutes
S is seconds.
Time can also be displayed using date +%t.
[Email protected] shell]# date +%t
20:26:58
Date +%s time stamp, based on how many seconds it's been in the past from 0:0 January 1, 1970.

[[email protected] shell]# cal
May 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 29) 30 31
View dates as a calendar

Date-d "+1day" a day later
Date-d "-1 days" a day ago
Date-d "-1 month" month ago
Date-d "-1 min" A minute ago
Date +%w, date +%w Week
W is week. W is the first week of the year.

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 description, shell script structure and execution, date command usage, variables in shell scripts

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.