Shell Programming Preliminary

Source: Internet
Author: User
Tags arithmetic operators bit set logical operators

One: Hello World

Create a new file, named after the. sh suffix. Each bash file begins with the first row of table names file type: #!/bin/bash

Then enter the code below.

For example, the output Hello World:

#! /bin/"helloworld"

Run Bash file:./file name. sh

(Files created at the terminal are saved with vim edit, Ubuntu can be a blank document in the graphical interface, identified as bash file by suffix name)

Two: Variables

The variables defined in the shell have the following rules:

    • The first character must be a letter (a-z,a-z).
    • You can use an underscore (_) without spaces in the middle.
    • Punctuation cannot be used.
    • You can't use the keywords in bash
    • There can be no space between the variable name and the equal sign;

Use object: Gets a variable using the ${variable name}, just like an El expression. such as: ${name}

Read-only variable: You can use the ReadOnly modifier variable to become read-only after the assignment, which is equivalent to the final constant

#!/bin/bashname="Ygj"readonly name

Variable type:

    • 1) local variable local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.
    • 2) Environment variables All programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.
    • 3) shell variables shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.

String: Enclosed in "or"

string concatenation: Do not need operators, directly attached to write or with ${name} to the content to be spliced directly into the new definition of the string. Such as:

#! /bin/bashstr1="I am str1"str2="you arestr2,and  "str1str3="She is str3,${str2}"

Get string length: #strname, such as:

#! /bin/bashstr1="I am str1"echo ${#str1}

Intercept string: Strname:beginindex:endindex, such as:

#! /bin/bashstr1="I am str1"str2=${str1:0:2}

Find substring in string or word Poute: ' expr index ' ${strname} ' Find what ' (Note: front and back two anti-quotes.) Button under ESC on the keyboard)

#! /bin/bashstr1="I am str1""${str1}" am '

Shell array:

Definition array: arrname= (value0 value1 ... (Note: The elements are separated by a space, not a comma!) Arrname[i]=value_i can also be assigned individually.

The shell array has no scope restrictions, access is only accessed by subscript, and the subscript starts at 0.

Accessing array elements: ${arrname[index]}

Access all elements: ${arrname[@]}

Get number leader: With # Operator: ${#arrname [@]}

Get the length of the content of a single element: With the # operator: ${#arrname [index]}

Shell notes: Lines that begin with # are comments

Three: Passing parameters to bash files

When the terminal launches the bash file, you can add the incoming parameter values after the file name, such as:

0930  A

Get parameters in Bash file: $n, n refers to the number of arguments passed in, starting at 1. Such as:

#! /bin/Bashecho "No. 1 is: $1"

Number of statistical parameters: $#

Returns all parameters as a string: $*

Return all parameters individually: [email protected]

Displays the process running the current script id:$$

Four: operator

There are several main operators:

Arithmetic operators: +-*/%,==,! =

The operation expression of bash does not directly calculate the result of the mathematical operation, it needs to parse the expression with the ' expr expression ' (anti-quote) command to get the result. In addition, there is a space between the number, the variable and the operator when the expression is written, otherwise it is just a simple string. Such as:

#!/bin/1 '

The following operators use no expr required, but need to be placed in [], and spaces between the operators and operands are also left blank. Example: [NUM1 op num2]

Relational operators (relational operators only support numbers and do not support strings):

-EQ detects whether two numbers are equal and returns true for equality.

-ne detects whether two numbers are equal and returns true if they are not equal.

-GT detects if the number on the left is greater than the right and, if so, returns True.

-LT detects if the number on the left is less than the right and, if so, returns True.

-ge detects if the number on the left is greater than or equal to the right, and returns true if it is.

-le detects if the left-hand number is less than or equal to the right, and returns true if it is.

Boolean operators:

-O or operation, which returns true if an expression is true.

-A with operation, two expressions are true to return true.

! Non-operation, the expression is true returns False, otherwise true.

Logical operators:

&& Logic and

|| Logical OR

String manipulation operators:

= detects whether two strings are equal and returns true for equality.

! = detects whether two strings are equal, and returns true if they are not equal.

-Z detects if the string length is 0 and returns true for 0.

-N detects whether the string length is 0 and does not return true for 0.

File manipulation operators:

The-B file detects whether the files are block device files and, if so, returns True.

The-C file detects whether the files are character device files and, if so, returns True.

The-D file detects whether the files are directories and, if so, returns True.

The-F file detects whether files are normal files (neither directories nor device files), and if so, returns True.

The-G file detects if the SGID bit is set and returns True if it is.

The-K file detects whether the files have a sticky bit set (Sticky bit), and returns True if it is.

The-P file detects if the files are named pipes and, if so, returns True.

-U file detects whether the file has a SUID bit set and returns True if it is.

The-R file detects whether the files are readable and, if so, returns True.

The-W file detects whether the files are writable and, if so, returns True.

The-X file detects whether files can be executed and, if so, returns True.

-S file to detect whether the files are empty (the file size is greater than 0) and not NULL to return TRUE.

The-e file detects whether files (including directories) exist and, if so, returns True.

Shell Programming Preliminary

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.