Bourn Again Shell Programming

Source: Internet
Author: User
Tags arithmetic

The shell is both a command interpreter and a high-level programming language. The shell is an interpreted language.

    1. The creation and operation of bash scripts:

Comment Lines start with #

#! The following parameters tell the system to execute the program of this file

There are two ways of executing a script file:

1./bin/bash SCP1

2. chmod u+x SCP1

./SCP1

2. Shell variables:

1. More important environment variables: $HOME, $PATH, $ $1~$9, $*, [email protected], $#, $$, $?, $!

2. User-defined variables:

A bash variable is a string by default.

Bash does not have to declare variables, but some special types of variables must be declared, and variables can be declared using the Declare or typeset commands, initialized, and set their properties.

declare [options] [Name[=value]]

typeset [Options] [Name[=value]]

Common options:-a array,-f function,-i integer,-R read-only,-x variable can be accessed by the quilt process

Bash variables do not have to be declared or initialized before they are used, and an empty string is the initial value of a variable that does not declare uninitialized.

Declaring an existing variable does not change the current value of the variable.

Note that when the variable is assigned, there cannot be a space before or after the = number, and if the value has a space, it must be added "".

An shaping variable cannot be given a non-shaping variable, and a non-shaping variable can give any value.

Unset[name-list] re-or delete the variable value or function in Name-list

In the shell, you can access its contents by adding a $ symbol before the variable name, and do not use the $ symbol when assigning a value to a variable.

"Inside explanation $, ', \ Symbol, '" is not explained, \ can remove the special meaning of these characters.

$ (command) or ' command ', the shell replaces it with the output of this command, a process called command substitution.

Read [options] variable-list assigning a user's input value to a shell variable

Common options:-a name read into name,-e a whole line read into the first variable,-n cursor stay on the same line,-P prompt if it is input from the terminal, display prompt string

    1. Delivery of the shell script positional parameters:

When you run a command or a script in Linux, you can pass the arguments on the command line, and the values of the first 9 parameters are stored in the shell environment variable \ $9. However, with more than 9 parameters for the script, use the shift command for more than 9 arguments.

The value of the positional parameter can be set by using the SET command.

    1. Control Structure Statement:

If-then-elif-else-fi

Expression expressions can be detected with the test expression command or [expression], which detects an expression and returns True or false. Note: Leave at least one space before and after the operand and the operator or the parentheses.

The condition types that can be used by the test command can be categorized as three classes: string comparisons, arithmetic comparisons, and file-related conditional tests.

For variable [in argument-list]

Do

Command-list

Done

While expression

Do

Command-list

Done

Until expression

Do

Command-list

Done

Case variable in

PATTERN1)

Command-list;;

PATTERN2)

Command-list;;

...

*)

Command-list;;

Esac

    1. A few other useful statements:

Break, continue, exit, printf (floating-point number not supported)

    1. Numerical Processing:

In a Linux system, the values of bash variables are stored as strings, and if arithmetic and logical operations are required, they must be converted to integers before being converted to strings.

Bash provides three ways to perform arithmetic operations on numeric data:

1. Let express-list use shell variables in express-list without adding $.

2. The shell variable is used in Express, without adding $.

3. Expr command: x=$ (expr $x + 1)

    1. Array:

You can use declare, local, ReadOnly, and other statements to declare an array variable, or you can directly assign a method to declare an array:

ns= (Max San [6]=zhang [25]=wang)

Echo ${ns[6]}

A= ("${ns[@]}") The original array is the same as the first array

B= ("${ns[*]}") All values of the original array are assigned to the first element of the array first

size=${#ns [*]} ${#ns [5]} The size of the 6th array cell, in bytes

    1. Function:

Function_name () {

Command-list

}

When a function is called, the positional parameters $*, [email protected], $#, $1~$9 are replaced with the parameters of the function.

    1. Here Documentation:

It allows a command to execute, as if it were reading a file or keyboard, and actually getting input data from the script.

Command<<[-]input-marker

.... Input data ...

Input_marker

    1. EXEC command:

Use the EXEC internal command to execute a new command to replace the current shell process instead of generating a new one. At the end of the command, the parent process is returned instead of the process that called it (that is, the process being overwritten).

Can be redirected through the EXEC command, but after redirection, the shell does not return to the standard terminal unless the Exec</dev/tty or Exec>/dev/tty is executed

    1. Trap command:

The trap command is used to specify what action will be taken after the signal is received. A common use of the trap command is to complete the cleanup operation when the script is interrupted. The signal is stored in the signal.h, and when the signal name is used, the sig prefix can be omitted, trap-l view the signal number and the associated name.

Trap [' command-list '] [signal-list]

Trap-signal-list Return to default action

Trap ' signal-list ignore signal

Kill-l List all signals

    1. Debug Script (slightly)

Bourn Again Shell Programming

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.