2018-4-17 16 weeks 5 Lessons shell script date, if

Source: Internet
Author: User

20.1 Shell Script Introduction

Shell is a scripting language

• Logic Judgments, loops, and other syntax can be used

• Functions can be customized

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

• You need to add #!/bin/bashat the beginning, you can run on this machine, the change machine is not necessarily able to execute. At the beginning of the header, specify which interpreter to run the next command to operate.

• The line that begins with # as an explanation, the name of the script ends with. SH and is used to differentiate between this is a shell script

• There are twoways to do this: 1,bash or sh sh file; 2, add execute permission to file, then./File

1,bash 1.sh/bin/bash is actually/bin/sh.

[root@localhost shell]# ll /bin/bash
-rwxr-xr-x. 1 root root 960472 8月   3 2017 /bin/bash
[root@localhost shell]# ll /bin/sh
lrwxrwxrwx. 1 root root 4 12月 28 05:37 /bin/sh -> bash

2, have the executive permission, chmod +x 1.sh; can be directly./1.sh

3, in fact, the absolute path of the direct input file can also be executed

[root@localhost shell]# /root/shell/01.sh
22:22:27 up 2:21, 1 user, load average: 0.02, 0.02, 0.05
Root pts/0 20:08 3.00s 0.06s 0.00s /bin/bash /root/shell/01.sh
Total usage 4
-rwxr-xr-x 1 root root 32 April 16 22:22 01.sh

• View the script execution process:bash-x 1.sh/sh-x 1.sh

[root@localhost shell]# sh -x 01.sh
+ echo 123
+ w
22:23:55 up 2:23, 1 user, load average: 0.00, 0.01, 0.05
Root pts/0 20:08 3.00s 0.07s 0.00s sh -x 01.sh
+ ls -l
Total usage 4
-rwxr-xr-x 1 root root 32 April 16 22:22 01.sh

• See if the script is syntactically incorrect:bash-n 1.sh

Deliberately write the wrong script (random characters are not error)

[root@localhost shell]# sh -n 01.sh
01.sh: Line 10: Syntax error: Unexpected end of file

20.3 Date command usage

[[email protected] ~]# date
2018 April 17 Tuesday 20:15:04 CST

date +%y-%m-%d, date +%y-%m-%d Month Day

[root@localhost ~]# date +%Y-%m-%d
[root@localhost ~]# date +%y-%m-%d
[root@localhost ~]# date +%d ## d Date
[root@localhost ~]# date +%D ## US format year, month, day
[root@localhost ~]# date +%M ## M minutes
[root@localhost ~]# date +%m ## m month
[root@localhost ~]# date +%F ## Date

Date +%h:%m:%s = Date +%t time

[root@localhost ~]# date +%H:%M:%S
[root@localhost ~]# date +%T
[root@localhost ~]# date +%H ##hour
twenty one
[root@localhost ~]# date +%h ##month (with format, set Chinese is April, English is Aprl)
[root@localhost ~]# date +%M ##minutes
[root@localhost ~]# date +%m ##month
[root@localhost ~]# date +%S ##seconds
[root@localhost ~]# date +%s ##timestamp

Date +%s time stamp

How many seconds from 1970-01-01-00-00-00 past

date +%w, date +%w Week

[[Email protected] ~]# date +%w## weeks 2[[email protected] ~]# date +%w## The week of this year 16

date-d @1504620492

[root@localhost ~]# date +%w## Day of the week
[root@localhost ~]# date +%W## The first few weeks of the year

date-d "+1day" a day later

[root@localhost ~]# date -d @1504620492##Know the timestamp to check the date
Tuesday, September 05, 2017 22:08:12 CST
[root@localhost ~]# date +%s -d "2018-4-17 21:52:30"##Know the date to check the timestamp

date-d "-1 days" a day ago

date-d "-1 month" Month ago # #年year

[Email protected] ~]# date-d "-1 month" +%F2018-03-17

date-d "-1 min" A minute ago # #小时hour, seconds second

[[email protected] ~]# date-d "-1 min" +%t21:47:09

[[email protected] ~]# cal April 2018 1,234,561 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30

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, use the variable if [$a-gt 1]; 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 operations a=1;b=2; c=$ (($a + $b)) or $[$a + $b]

2018-4-17 16 weeks 5 Lessons shell script date, if

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.