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
123
22:22:27 up 2:21, 1 user, load average: 0.02, 0.02, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
Root pts/0 192.168.65.1 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
123
+ w
22:23:55 up 2:23, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
Root pts/0 192.168.65.1 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
2018-04-17
[root@localhost ~]# date +%y-%m-%d
18-04-17
[root@localhost ~]# date +%d ## d Date
17
[root@localhost ~]# date +%D ## US format year, month, day
04/17/18
[root@localhost ~]# date +%M ## M minutes
06
[root@localhost ~]# date +%m ## m month
04
[root@localhost ~]# date +%F ## Date
2018-04-17





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


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





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
2
[root@localhost ~]# date +%W## The first few weeks of the year
16





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
1523973150 





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

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.