Shell Learning Notes

Source: Internet
Author: User
Tags case statement

First,shell variables

1. There can be no space between the variable name and the assignment symbol.

2. The first character must be a letter a~z or a~z.

3. Using a defined variable, add ' $ ' before the variable name, such as:

Myname= "Firefly"

Echo $myname

Echo ${myname}

The curly braces help identify the bounds of the variable, and it is recommended to add S.

4. Delete variable: unset varName, cannot delete read-only variable.

5, three variables: local variables, environment variables, shell variables

Second,Shell replacement

1. Variable substitution

Echo-e "Hello, emma!\n"

Without-e, no escaping and direct output

2, 3, slightly

Third, conditional statements

If-else statements

(1) if...then...fi

if [expression]

Then

Statement (s) to BES executed if expression is true

Fi

(2) if...else...fi

if [expression]

Then

Statement (s) to BES executed if expression is true

Else

Statement (s) to being executed if expression is not true

Fi

(3) if...elif...fi

If [expression 1]

Then

Statement (s) to being executed if expression 1 is true

elif [Expression 2]

Then

Statement (s) to being executed if expression 2 is true

elif [Expression 3]

Then

Statement (s) to being executed if expression 3 is true

Else

Statement (s) to being executed if no expression is true

Fi

Note:

(1) There are spaces between expression and square brackets []

(2) symbol >,>=,==,<,<=,! can be used in conditional statements = or-GT,-gq,-eq,-lt,-LQ,-nq judging size relationship

Case : SEAC Statements

1, with other language switch-case statement, for multi-branch selection structure;

2. The case statement matches a value or a pattern, and if the match succeeds, the matching command is executed. The case statement is in the following format:

Case value in #in为关键字

Mode 1)

Command1

...

CommandN

;;

Mode N)

Command1

...

CommandN

;;

*)

Command1

...

CommandN

;;

Esac

(1) Each mode must end with a closing parenthesis, the value can be a variable or constant;

(2) The command to execute the pattern after the matching discovery value conforms to a pattern until ';; ' end, ';; ' Analogy break statement;

(3) If there is no matching pattern, use ' * ' to capture the value, then execute the subsequent statement.

Four,shell comments : Only one-line comments, preceded by ' # '

If a large piece of code needs to comment or uncomment, you can enclose the comment section in curly braces, define it as a function, and do not perform the effect of a comment without being called.

string : Single quote, double quote, no quotation mark

Single-Quote String restrictions:

    1. -Any character in a single quotation mark is output as is, and the variable in the single-quote string is invalid;
    2. Single quotation marks cannot appear in single quote strings (not after using escape characters for single quotes).

Advantages of double quotes:

    1. -You can have variables in double quotes
    2. -escape characters can appear in double quotes

Six,shell array

1, the definition of the array, shell brackets represent arrays, array elements separated by a space symbol

Arrname= (1 2 3) or

Arrname= (1

2

3) or

Arrname[0]=0

Arrname[1]=1

arrname[2]=2

2, read array, format ${arrname[index]}

VALUE=${ARRNAME[2]}

Note: Use the symbol ' @ ' or ' * ' to read all the elements in the array

Eg: ${arrname[*]}, ${arrname[@]}

3. Get the length of the array

Gets the length of the array method of the same string

Gets the number of array elements: length=${#varName [*]}

Gets the length of an array of individual elements: lengthn=${#varName [2]}

Seven,Shell echo command

You are analogous to the cout in printf,c++ in C language.

There are also printf in the shell, slightly different

This is only a description of the difference from the C-language printf () function:

    • printf command without parentheses
    • Format-string can be without quotation marks, but it is best to add single quotes and double quotes.
    • When the parameter is more than the format control (%), format-string can be reused and all parameters can be converted.
    • Arguments use spaces separated by commas.

eg

    1. $ printf "%d%s\n" 1 "ABC"
    2. 1 ABC

Eight,shell loop Statements

1 , for Loop

Format:

For variable in list

Do

Command1

...

CommandN

Done

(1) A list is a series of values (numeric, string), each separated by a space.

(2) Once per loop, assign the next value in the list to the variable.

(3) The in list is optional and uses its positional parameters if not used.

For Var in 1 3 5 7

#!/bin/sh

For Var in 1 3 5 9

Do

Echo ${var}

Done

2. while loop

Format:

While command

Do

Statement (s) to BES executed if command is true

Done

3.unitl cycle

Format

Until command

Do

Statement (s) to being executed until command is true

Done

Exit Loop: Break, continue

Note:

(1) in a nested loop, break n means exiting the nth layer loop and counting from the inner layer.

(2) Continue exits the current loop.

Nine,shell function

    1. Delete function: $unset. f function_name
    2. When n>10 need ${n} to obtain parameters, n<10 can be directly $n;

X. input/Output redirection

(1) > (or <) output (or input) redirection will overwrite the original content;

(2) >> (or <<) the output (or input) redirection does not overwrite the original content.

Xi.Shell file contains

The shell can contain external scripts that merge the contents of the external script into the current script.

Format:

(1). FileName (note space)

(2) Source filename

Note: The included script does not require execute permissions.


Recommended Learning materials:

(1) Linux shell Script Tutorial

(2) Bash Reference Manual

(3) Bash Guide for Beginners

(4) Advanced bash-scripting

Shell Learning 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.