Shell Introduction, Date command, Shell variable

Source: Internet
Author: User

What is Shellshell?

Shell is a scripting language
can use logical judgment, circular judgment and other statements;
functions can be customized;
A shell is a collection of system commands, such as a batch command of Windows;
Shell script can realize automatic operation and maintenance, can play to increase our operational efficiency;

Shell script Structure

#! must be added at the beginning /bin/bash
That is, the kernel that calls/bin/bash runs
The name of the script ends with. SH and is used to differentiate between a shell script;

There are two ways to do this: 1. As an executable program
chmod +x 1.sh       //是脚本具有执行权限./1.sh                       //执行脚本
2. As an interpreter parameter
bash 1.sh                              //也可以sh 1.sh

To view the script execution process: Bash-x 1.sh
See if the script is syntactically incorrect: Bash-n 1.sh

Date Day Command Month Date:

Command: Date +%y-%m-%d
Output: 2018-04-17

Command: Date +%f
Output: 2018-04-17

Command: Date +%y-%m-%d
Output: 18-04-17

Command: Date +%y%m%d
Output: 20180417

Time:

Command: Date +%h:%m:%s
Output: 20:23:16

Command: Date +%t
Output: 20:23:16

Week:

%w: The first few weeks of the month
%W: The first few weeks of this year

Time stamp:

Date +%s
Date-d @1504620492

Increase reduction Date:

Date-d "+1day" a day later
Date-d "-1 days" a day ago
Date-d "-1 month" month ago
Date-d "-1 min" A minute ago

Variable naming rules:

Names can only use letters, numbers, and underscores, and the first character cannot begin with a number.
You can use an underscore (_) without spaces in the middle.
Punctuation cannot be used.
You can't use the keywords in bash (you can see the reserved keywords using the help command).

Shell value:
a=1;b=2;        //给变量a赋予数值1,变量b赋予数值2c=$(($a+$b))    //c=a+b,使用运算必须前面加$
Shell string:

string when the shell becomes the most commonly used most useful data type (numbers and strings), strings can be in single quotes, or double quotes;

Single quotes:
str=‘this is a string’

Escape characters and variables cannot be used;

Double quotes:
your_name=‘aaaa‘str="Hello,I know your are \"$your_name\"! \n"

Advantages of double quotes: can have variables, can appear escape character;

The call variable value must precede with the $ symbol
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 "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]

Shell Introduction, Date command, Shell variable

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.