Shell Base Notes

Source: Internet
Author: User
Tags case statement echo command

 variables

Declaring variables:
DECLARE attribute variable
#attribute表示变量的属性, the common properties are as follows:
#-I defines the variable as an integer;-a variable is declared as an array variable;-r declares the variable as a read-only variable;-X sets the variable to an environment variable

Local variables:
Local variables are defined inside the function through the local keyword, and in addition, the parameters of the function are local variables. The scope starts from the defined place, to the end of the function or to the deletion that is displayed, and the use of local variables outside the function gets a null value

Global variables:
You can define it in a script, or you can define it in a function (without using the local keyword). The scope starts at the defined place, until the end of the shell script or the deletion that is displayed

System variables:
The $n represents the nth argument passed to a script
The name of the current script
$# number of command-line arguments
$? The return status code of the previous command or function (also including the return value of the function), the execution returns 0 successfully, otherwise, 1
$* with "parameter 1 parameter 2 parameter 3:" Returns the value of all parameters in the form
[Email protected] with "parameter 1" said "Parameter 2" "Parameter 3 ..." Returns the value of all parameters in the form
$$ returns the process ID (PID) of this program
$_ last parameter of the command executed before saving

Environment variables:
When the shell program runs, it receives a set of variables that are environment variables

#使用 the SET command to list all environment variables

The path command searches for paths with colons as delimiters and the current directory is not in the system path (this differs from Windows)
The path name of the home user home directory, which is the default parameter for the CD command
Sehll the full path name of the shell
PWD Current Working Directory
Histfile Command History file
LOGNAME the current login name
Tmout Shell Automatic exit time, in seconds, if set to 0 prohibit shell auto-exit

Variable assignment:
Variablename=value

Command substitution:
#指在shell程序中, assign the execution result of a shell command to a variable.
#在bash中, there are two types of syntax that you can replace
Variablename= ' Command '
variablename=$ (command)


The left and right sides of the #赋值号 "=" cannot be directly followed by a space, or the shell treats it as a command

Reference variable:
${variablename} or $variable

Indirect variable references:
Var=name
Name=john
${!var} has the same effect as ${name}


To clear a variable:
Unset variableName

Special attention:


The character nonalphanumeric enclosed in single quotes appears as a normal character
Characters enclosed in double quotation marks, except "$" "\" "" "(single quote) and" "" (double quotation marks), other characters treated as ordinary characters
The string that is enclosed in quotation marks is interpreted by the shell as a command, and at execution time, the shell executes the command first and replaces the entire anti-quote part with its standard output.

------------------------------------------------------------------------------------------
Condition Test (Test command and [command):

#条件测试中的指定条件为真时, the return value of the conditional test is 0; false, the return value is a value other than 0

#test命令
Test expression

#[command
#为了增强程序可读性, the shell requires a closing parenthesis appended to the conditional expression to pair with the preceding [command]
#条件表达式与左右方括号之间必须都保留一个空格
[Expression]

#如果expression是一个字符串, then-N to determine whether the string is non-null,-Z to determine if the string is an empty string, and "=" and "! =" to determine whether the two strings are equal

#如果expression是一个文件名,
#则-E (or-a) to determine whether the file exists,-R,-W,-X to determine whether the file exists, and whether it is readable, writable, executable;-B,-C,-D,-l determine whether the file exists, and whether it is a block, character directory, linked file

#如果expression是一个由逻辑与, logical, or logical non-constituent expressions, then! represents logical non;-a represents logic and;-O represents logic or

------------------------------------------------------------------------------------------

If statement:

If expression
Then
Statement1
Statement2
...
Else
Statement3
Statement4
...
Fi

Or:
If expression
Then
Statement1
Statement2
...
Elif expression2
Then
Statement3
Statement4
...
Elif Expression3
Then
Statement5
Statement6
...
Else
Statement7
...
Fi


#在shell中, there is a special command called the null command, which means that the method is a colon ":", the command does nothing, but its exit status is always 0

Exit statement:
#exit语句的基本作用是终止shell程序的执行, with an optional parameter to specify the status code when the program exits
Exit status

------------------------------------------------------------------------------------------
Case statement:

Case variable in
value1)
Statement1
...
Statement2;;

value2)
Statement3
...
STATEMENT4;;
*)
Statement5
...
STATEMENT6;;
Esac

------------------------------------------------------------------------------------------
For loop:

For variable in {list}
Do
Statement1
Statement2
...
Done

#list是一个列表, can be a series of numbers or strings, between elements separated by a space, if you use a string to do list elements, the outside curly braces can be omitted
# {1..8} indicates shorthand for 1-8
# {1..100..5} represents 1-100, starting from 1, each increment of 5, i.e. 1,6,11,16 ...
# ${arrayname[*]} represents all elements of an array arrayname


For loop without list:

#shell从命令行获取条件列表, the following three forms of equivalence

For variable-variable in [e-mail protected] for variable in $*
Do does do
Statement1 statement1 Statement1
Statement2 Statement2 Statement2
...                       ...                               ...
Done do done

Class C-style for loop:

For ((Expression1;expression2;expression3))
Do
Statement1
Statement2
...
Done

------------------------------------------------------------------------------------------
Util cycle:

Util expression
Do
Statement1
Statement2
...
Done

------------------------------------------------------------------------------------------
While loop:

While expression
Do
Statemnet1
Statement2
...
Done

------------------------------------------------------------------------------------------
Definition of the function:

function functionname ()
{
Statement1
...
}

#function可以省略

------------------------------------------------------------------------------------------
Setting and deletion of aliases:

Alias name= "Command"

Unalias Name

------------------------------------------------------------------------------------------
Arithmetic operations:

Expr, $ (...), $[...] and let

#expr是一个shell命令, you can calculate the value of an expression
Expr expression

Example:
#先计算2和6的差, then multiply by 12 and assign the result of the calculation to the variable

#一定要注意运算符 (Power operation "* *") space around, and expr cannot calculate a power operation

result= ' expr \ (2-6 \) \* 12 '
result=$ ((1 + 2))
result=$[1 + 2]
Let N=n + 1 (or let n=n+1?)

------------------------------------------------------------------------------------------
Cat View File Contents:

Cat FileName
#-N Displays the line number of the text

Cat-n FileName
# grep-n "" FileName has the same effect as the previous line

---------------------------------------------------------------------------------
echo Output text

echo [Options] String

#如果要输出的文本是由多个单词组成的, you can use double quotation marks or single quotation marks to bring them together.
#如果不用引号引起来, the Echo command splits a string into multiple strings based on a space.

#-N Disables automatic addition of line breaks after Echo statement execution completes
# escape character \c suppress continuation of output text

Echo ' Command '
#echo可以将shell命令执行结果显示出来, and you need to use the back quotation marks to reference the command.

#将echo命令显示的信息覆盖到目标文件中
echo String > FileName

#将echo命令显示的信息追加到目标文件的末尾
echo String >> fileName

------------------------------------------------------------------------------------------
Rev reverses the order of each line of characters

Rev [Files]
#files表示要处理的文件的文件名列表, if you have multiple files, separate the file names with a space




Shell Base Notes

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.