67. Shell script Introduction, shell script structure and execution, date command usage, variables in shell script

Source: Internet
Author: User

67. Shell Script Introduction, shell script structure and execution, date command usage, variables in shell script

First, Shell script introduction

The shell is a scripting language aming_linux blog.lishiming.net

You can use logical judgments, loops, and other syntax

Functions can be custom-defined

Define the purpose of the function: In order to simplify, in order 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


Second, shell script structure and execution

Script Example:

#!/bin/bash

echo "123"

W

Ls

The first need to add #!/bin/bash, the script in the current machine execution can recognize the inside of the command, the other machine may not recognize the execution, so need to add this line. The following command is written by/bin/bash the interpreter to parse the execution, and it is the function of this.

The line starting with # in the second line is the explanatory note

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

There are two methods of execution:

# mkdir Shell

# CD shell/

# sh 01.sh

# chmod A+x 01.sh

#./01.sh

View the script execution process bash-x 1.sh

See if the script is syntactically incorrect, but can't check out the non-existent command bash-n 1.sh


Iii. use of the date command

Date +%y-%m-%d = Date +%f Month Day

Date +%y-%m-%d Month Day

y:2018, Y:18. Year two-bit or four-bit

M: Month, M: minutes. M:05,h:5 Month

D: Sun, D:05/30/18 (May 30, 18)

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

H: Hours

M: Minutes

S: seconds, S: timestamp, how many seconds elapsed from January 1, 1970 0:0.

Date +%s Timestamp


Cal: Look at the calendar for the entire month.

Date-d @1504620492 @ back with timestamp

Date-d "+1day" a day later

Date-d "-1 days" a day ago

Date-d "-1 month" month ago

Date-d "-1 month" +%f

Date-d "-1 years" a year ago

Date-d "-1 year" +%f

Date-d "-1 min" A minute ago

Date-d "-1 Hour" +%t

Date +%w, date +%w Week

W: Week, Monday Tuesday. W: The first few weeks of the year.


Iv. 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

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]


67. Shell script Introduction, shell script structure and execution, date command usage, variables in shell script

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.