Shell Programming Basics

Source: Internet
Author: User
Tags shebang

Shell Authoring Steps:

1. Scripting Vim first.sh

2. Write the complete test script Bash-n first.sh

3. Granting permission chmod +x first.sh

4. Execute the script./first.sh

PS Shell Script Learning first need to understand Linux, system commands and general structure familiar, able to skilled operating system and build common services to provide services, to better learn shell programming ...

Variable:

Memory space, called by a variable name when referencing a data or array, that can be reclaimed and allocated to a different data store, called a variable.

Memory is the address of the storage unit, in physical memory each byte has a unique location, through a unique address can find a corresponding byte, the program is executed called a process, the process of all the data and instructions are in memory, in the process of executing a program to temporarily store some data, It is necessary to use variables, which may be changed in the middle of the data value, when a data is executed, the data is purged, re-assign the space to other data, so the amount of memory space storage is constantly changing, the amount of these changes are stored in a constantly changing the value of the storage space, And this storage space is the memory of a certain part of the storage space, such as how to find a certain amount of storage space value? According to the memory address can be found, generally refers to the start address, but for the user, to record memory address is very difficult, because it is hexadecimal, so there will be a mapping table inside the process, with the variable name (string) to identify a memory address, for the user to identify the name, A memory address that is recognized by the computer.

      

Storage of variables:      The size of the data may not be the same at each time the data is stored, and the memory capacity of storage 2 and 2000 is different,      example: Store character 10 The minimum storage unit for computers is 8bit,1 and 2 characters need 16bit            Store value 10, decimal conversion binary equals 1010, actually occupies 4bit, However, the minimum computer storage unit is 8 bytes, so you need 8bit      So when assigning data, you need to decide beforehand whether the character is stored or when the value is stored, or how much storage space is consumed, so the variable is of type   variable type:   Specifies the storage format in which the data is stored, and the variable type directly determines the storage format and length of the data       character type:      numeric:          Shaping:          floating point: With a decimal point, Example: 12.25, in fact, the floating point is stored separately, before the decimal point as a unit to store, after the decimal point is also stored as a unit, and then store the location of the decimal point        logical operations: with, or, non-, XOR or 1: true 0: false      with: As long as one is false, the result must be false           1 & 0 = 0          0 & 1 = 0          0 & 0 = 0          1 & 1 = 1   &N Bsp   OR: As long as one is true, the result must be true           1 | | 0 = 1          0 | | 1 = 1        &NBSp 0 | | 0 = 0          1 | | 1 = 1      Non: false-true   non-true even if False          ! true = False           ! False = True       XOR:          1 | 0 = 1          0 | 1 = 1          0 | 0 = 0          1 | 1 = 0

Not the same as 1 for the same 0

Strongly typed: Before use, the variable must declare the type of the variable, and even need to initialize (when the data is not stored after the application of memory space, the original value is randomly generated, so you need to initialize the data, the general value is initialized to zero, the string is also initialized to NULL, the equivalent of the initial null, Default initial value) You can also specify initial   weak types: variables are declared when they are used, do not differentiate whether variables are numeric or character, do not need to be declared, don't even need to distinguish between types, and if they are not, the general default is string       If you need to assign a variable to a numeric value you need to use the let var_name=value  variable name format:     1, can contain only letters, math and underscores, and cannot start with a number      2, should not be with the existing environment variables in the system life name      3, it is best to see the names of the meaning   variable assignment:var_name=value  reference variable: ${var_name}, also known as variable substitution, Refers to the variable name, the result is the data value in the variable, when referencing the variable, without causing the variable name confusion, you can omit the parentheses, otherwise you need to add brackets         bash   Variable type:      environment variable: scope is the current shell process and its child processes       Assignment variables: (also known as export variables)           1 , export var_name=value          2, var_name=value           / nbsp  export VAR_NAME       Local variables: scopes are available for the entire bash process, and you cannot use       assignment variables when the process ends:           VAR_NAME=VALUE           Local variables:Scopes are valid only for the current code snippet                 assignment variable mode:                    local var_name=value       Positional variables: Parameters used to reference scripts,          references: var_name=$1  var_name=$2   Assignment in scripts, the execution script is separated by a space to keep up with the arguments, This parameter is the variable with var_name=$1  var_name=$2 in the script, the first parameter is the variable data value, the second parameter is the variable data value of $2 , you can specify multiple, the execution parameter of the script corresponds to the position variable $n one by one in the foot.      shift combination position variable usage, shift command for rotation variable, same position variable, reference multiple parameters           shift N     table ,shift 2  example of n variables: 1 2 3 4 5, 1 3 5      :          #!/bin/bash&nb Sp         Echo $1          shift          Echo $1&nbsp ;         shift          echo $1 #./shift.sh 1 2 3   &nbsp ;   Special variables: A variable built into bash that holds variables for certain special data, even a special variable called a system variable           $?: The execution status return value of the previous command                 program execution, there may be two types of return values:  &N Bsp                 program implementation results                 &NBS P   Program Status return codes (0-255)                        0: for proper execution &N Bsp                        1-255: Indicates error execution, 1,2,127 for system reservation      echo $?     You can view the execution status return value of the previous command            $#  : Number of parameters for the script         &NBSP ; $*  : Parameter list of the script           [email protected]  : script parameter list         &NB sp; $$  : script pid          $2  : 2nd parameter of the script           &N Bsp    $0  : The name of the script   local variables: variables are used in a program, or programs are used to store data during run time, and bash is a special program that can declare variables within bash when it is run, bAsh declares variables internally, and this variable is related to the bash process, and if the bash process ends, then the related variables end, so the variables must be process variables, so you can define variables directly in the shell, anytime, anytime, without defining the type of data, Then when the Bash child shell is opened in bash, the variables defined in the parent shell do not take effect, because in different shells, it is actually two different shell processes, so variables defined in the parent shell are not in the child shell, Although the interface is the same, but inside the machine is completely different   script executes when it starts a child shell process of the current shell:      command line launches the script will inherit the current shell environment variables       System-initiated scripts (non-command-line startup) need to self-define the required environment variables        undo variables:      When assigning variables, it is actually set Var_ Name=value, just set can omit       Unset var_name     To undo variables   View variables in the current shell:     set           To view variables in the current shell, including environment variables and local variables    view environment variables in the current shell:     printenv     env     exprot  add data from variables:      Example:          var_name= $VAR _name:value     Add data to original variable data            Var_name=value: $VAR _name      Add data to previous variable data   add PATH environment variable:    export path= $PATH:/uer/locaL/mysql/bin     Add/uer/local/mysql/bin path to PATH environment variable after      export path=/uer/local/ Mysql/bin: $PATH      Add/uer/local/mysql/bin path to PATH environment variable before       script: command Stack, Implement the source program in accordance with the actual needs combined with the command flow control mechanism           Edit a text with a text editor and end with. SH        When scripting is complete, The system kernel cannot be executed because the system kernel cannot understand that the system kernel can only understand files in elf format, but the script is written in ASCII format, so you need to specify the execution magic number in the script       In the first line of the script you need to specify the execution magic number of the script (shebang) to #! Start, follow the path of the interpreter      #!     Indicates the specified number of enchantments (shebang)      #!/bin/bash     NOTE:/bin/bash must be the path to an executable interpreter program   when the script is executing, Submit the script to the kernel, the kernel execution is found wrong, cannot execute, but the kernel will find that the script has an execution magic number, you will understand the script is what format, and then according to the specified execution magic number path to start the corresponding interpreter to explain the script and execute the command, there will be an error in the middle of the process, While this error is handled by the kernel, the processing mechanism relies on the magic number   script to execute with execution privileges      chmod +x filename.sh        scripts execute when no permissions are executed           Bash filename.sh     explicitly specify bash scripts to execute scripts using the BASH interpreter. At this point, filename.sh is not an execution condition, only as a parameter of the Bash command   how the script executes:     1, where the scriptPaths to add PATH environment variable      2, specify path execution             Absolute path                /dir1/dir2/dir/filename.sh          Current catalogue           & nbsp    ./filename.sh   This article from the Learning God 1705 class monitor Guan Min Zong original adaptation

    

Shell Programming Basics

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.