20.1-20.4 Shell: script, script structure and execution, date usage, shell script variables

Source: Internet
Author: User

What is 20.1 shell?

    • The shell is the intermediate medium used when the system interacts with the computer hardware, and it is just a tool of the system.

    • In fact, he has a layer of things between the shell and the computer hardware-the system hard core. If computer hardware is likened to a person's body, then the system kernel is the human computer. As for the shell, it seems more appropriate to compare it to the facial features of the human being.

    • The user directly faces not the computer hardware but the shell, the user instructs the shell, then the shell again to the system kernel, and then the kernel to control the computer hardware to perform various operations.


    • Shell is a scripting language public number: Aming_linux blog: blog.lishiming.net Shell starts today with a daily look

    • You can use logical judgments (if,if else), loops (For,while), 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


    • How should the shell write?

    • Get the demand, have a specific idea, expand the demand link, start writing the shell



20.2 shell script Structure and execution


1 Creating a shell directory, followed by a script in it

[[email protected] ~]# mkdir shell

[[email protected] ~]# ls

2 Writing shell scripts

[Email protected] ~]# vim 01.sh#!/bin/bash echo "123" WLS


Parameter explanation:

#!/bin/bash the first line must be written so that if the script is executed only in native execution, it can be negative.

However, it must be added, considering that it is running on other machines, because the command is parsed by the #!/bin/bash command.


3 shell scripts are executed in two ways,

3.1 First of all, give script x Execute permissions

Given the script x permission, you can either./script name directly or execute it directly (absolute path)

[Email protected] shell]# chmod a+x 01.sh

[Email protected] shell]#!ls

Ls-ll 01.sh

-rwxr-xr-x 1 root root 15:02 01.sh


[Email protected] shell]#/01.sh 123 16:40:08 up + days, 2:15, 1 user, load average:0.03, 0.06, 0.06USER TTY From [email protected] IDLE jcpu PCPU whatroot pts/1 211.148.147.194 14:52 0.00s 0.11s 0 .00s W


Also, cat can view the contents of the script because the script already has execute permission and can be parsed by/bin/bash

[email protected] shell]# cat 01.sh #!/bin/bash echo "123" WLS


3.2 The second type

#bash 01.sh or #sh 01.sh

Because bash and SH are the same file output


4 Address of Bash parser

In fact,/bin/sh and /bin/bash are the same file,/bin/sh is linked to/bin/bash

[[email protected] shell]# ls-l/bin/bash-rwxr-xr-x 1 root root 960608 Sep 7 2017/bin/bash[[email protected] shell]# L S-l/bin/shlrwxrwxrwx 1 root root 4 Oct 2017/bin/sh-bash


5 lines starting with # are interpreted as explanations (the second line begins), and its purpose is simply to explain, without any meaningful execution.

Some special script # can be valid, but not all.


6 The name of the script ends with. SH and is used to differentiate between this is a shell script


7 Viewing the script execution process #bash-X 01.sh

[[email protected] shell]# bash-x 01.sh + echo 123123+ w 15:19:49 up-days, min, 1 user, load average:0.00, 0.01, 0.05USER TTY from [email protected] IDLE jcpu PCPU whatroot pts/1 211.148.147.194 14:5 2 5.00s 0.07s 0.00s bash-x 01.s+ ls01.sh

Each + represents an action , an action.


8 See if the script is syntactically incorrect, if there is no output to indicate that the script is not a problem.

[Email protected] shell]# bash-n 01.sh

8.1 Conversely, if there is a problem (only the shell syntax error is detected), there will be an error message output

For example, modify the script error, add the problematic for loop syntax, deliberately do not write for the end of the argument

[[email protected] shell]# vim 01.sh#!/bin/bash for i in ' seq 1 ' do echo $iecho ' 123 ' WLS

8.2 Execute Grammar Check command, make up, can see error

[Email protected] shell]# bash-n 01.sh

01.sh:line 9:syntax error:unexpected End of file

8.3 Write the correct parameters again check

#!/bin/bash for i in ' seq 1 ' do echo $idoneecho "123" WLS


No error output when correct parameters are entered

[Email protected] shell]# bash-n 01.sh

The above command with #bash can be used #sh, because point to the same command file



20.3 Date Command usage

Several common uses of the date command in shell scripts are as follows

Date +%y indicates that the year is printed in a four-bit number format

Date +%y indicates that the year is printed in a two-bit number format

Date +%m represents the month

Date +%d represents the day

Date +%h represents hours

Date +%h represents the month (English)

Date +%m represents minutes

Date +%s = seconds

The date +%w represents the week. The results show that 0 indicates Sunday.


[[Email protected] shell]# Date

Tue 15:52:08 CST 2018

[[Email protected] shell]# Date +%y2018

2018

[[Email protected] shell]# Date +%y18

18

[Email protected] shell]# date +%m Month

05

[[Email protected] shell]# date +%m min

52

[[Email protected] shell]# date +%d Day

29

[Email protected] shell]# date +%d month-day-year

05/29/18

[Email protected] shell]# Date +%f -month-day

2018-05-29

[[Email protected] shell]# Date +%s timestamp, from January 1, 1970 0:0 0 seconds to now how many seconds

1527580544

[[Email protected] shell]# date +%t current time, accurate to seconds

15:59:37

[Email protected] shell]# date +%h:%m:%s , same effect as date +%t

16:01:55

[[Email protected] shell]# Date +%w Week 2, Tuesday, Sunday is 0

2

[[Email protected] shell]# Date +%w Week of the year, 22nd week

22



Date format support combinations

Month and day: 20180529

[Email protected] shell]# date +%y%m%d

20180529


Calendar view Date

[[email protected] shell]# cal

May 2018

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

30 31


A day ago

[[email protected] shell]# date-d "-1 day"

Mon May 16:09:17 CST 2018

Date can be combined +%f use

[Email protected] shell]# date-d "-1 day" +%f

2018-05-28

One months ago

[Email protected] shell]# date-d "-1 month" +%f

2018-04-29

A year ago

[Email protected] shell]# date-d "-1 year" +%f

2017-05-29


Time can be combined with +%t

[Email protected] shell]# date-d "-1 Hour" +%t

15:13:01


Date +%s Timestamp

Time stamp conversion Standard Time format

[Email protected] shell]# date +%s

1527581785

[Email protected] shell]# date-d @1527581785

Tue 16:16:25 CST 2018

Time Format conversion timestamp

[[Email protected] shell]# date +%s-d "2018-05-29 16:19:00"

1527581940


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

    • 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]


20.1-20.4 Shell: script, script structure and execution, date usage, shell script variables

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.