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

Source: Internet
Author: User

20.1 Shell Script Introduction

What is a shell?

    • Shell is a scripting language
    • You can use logical judgments, loops, 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
20.2 shell script Structure and execution
    • Need to add #!/bin/bash at the beginning
    • To start 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 two methods of executing

Example 01:

[[email protected] shell]# cat 1.sh #!/bin/bashecho "123"
    1. chmod +x 1.sh;. /1.sh
[[email protected] shell]# chmod a+x 1.sh[[email protected] shell]# ./1.sh 123
    1. Bash 1.sh
[[email protected] shell]# bash 1.sh 123
    • View the script execution process bash-x 1.sh
[[email protected] shell]# sh -x 1.sh + echo 123123
    • See if the script is syntactically incorrect bash-n 1.sh
[[email protected] shell]# sh -n 1.sh #无错误#只用于检测shell的无法错误
20.3 Date Command usage

Show Current time:

[[email protected] ~]# date2018年 02月 05日 星期一 17:55:33 CST#变更为英文[[email protected] ~]# LANG=en[[email protected] ~]# dateMon Feb  5 17:57:24 CST 2018
    • Date +%y-%m-%d,date +%y-%m-%d Month Day
;年[[email protected] ~]# date +%Y2018;取后两位[[email protected] ~]# date +%y18;月份[[email protected] ~]# date +%m02;号(日)[[email protected] ~]# date +%d05;日期[[email protected] ~]# date +%D02/05/18;即[[email protected] ~]# date +%Y-%m-%d2018-02-05;或[[email protected] ~]# date +%F2018-02-05[[email protected] ~]# date +%y-%m-%d18-02-05
    • Date +%h:%m:%s = Date +%t time
;时[[email protected] ~]# date +%H18;分[[email protected] ~]# date +%M07;秒[[email protected] ~]# date +%S11;即[[email protected] ~]# date +%H:%M:%S18:06:43;或[[email protected] ~]# date +%T18:08:48
    • Date +%s Timestamp
;时间戳[[email protected] ~]# date +%s1517825370
    • Date-d @1517825370
#根据时间戳转换成日期[[email protected] ~]# date -d @1517825370Mon Feb  5 18:09:30 CST 2018
    • Date-d "+1day" a day later
[[email protected] ~]# date -d "-1 day"Sun Feb  4 18:19:25 CST 2018[[email protected] ~]# date -d "-1 month" +%F2018-01-05
    • Date-d "-1 years" a day ago
[[email protected] ~]# date -d "-1 years" +%F2017-02-05
    • Date-d "-1 month" month ago
[[email protected] ~]# date -d "-1 month" +%F2018-01-05
    • Date-d "-1 min" A minute ago
[[email protected] ~]# date -d "-1 min" +%F2018-02-05
    • Date +%w, week date +%w the first few weeks
[[email protected] ~]# date +%w1;是今年的多少周(星期)[[email protected] ~]# date +%W06
    • Show Calendar
[[email protected] ~]# cal    February 2018   Su Mo Tu We Th Fr Sa             1  2  3 4  5  6  7  8  9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28
20.4 variables in a shell script
    • When a string is used in a script, multiple times and the length of the string is long, you should use a variable instead

    • When using conditional statements, use the variable if [$a-gt 1];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 "Lnput a number:" N; echo $n If you do not write this n, you can use $reply directly

    • Built-in variable $0,$1,$2 ... $ A represents the script itself, the first parameter, the second ... $#: Indicates the number of arguments
    • Mathematical operations a=1;b=2;c=$ ($a + $b) or $[$a + $b]

Shell script Introduction, script structure and execution, date command usage, variables in 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.