Introduction to Shell Programming Step-up programming

Source: Internet
Author: User
Tags arithmetic character set chmod lowercase socket


http://blog.51cto.com/zt/267



Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://johnwang.blog.51cto.com/474770/326073



Shell Introduction

The shell is in the outer layer of the kernel, which is responsible for receiving commands entered by the user, and then interprets the commands as kernel can understand, then executed by kernel, and returns the results back to the default output perimeter. The shell is a command interpreter and also a programming language.   There are many types of shells, and in many Linux distributions, the standard shell is bash and the main object of our learning. Example of the first program: hello.sh #!/bin/bash Echo ' Hello World ' before execution, give execution permission: chmod +x hello.sh execute:./hello.sh output: Hello World A shell script is running successfully.









Basic Concept Part 

Landing host Landing host is divided into local landing and remote landing. Local login: Is directly facing the host, by the keyboard input user password login, using TTY1-TTY6, or graphical interface Tty7 remote login: Through the network connection host, the way of virtual terminal, using SSH to log out of 1. Use the command exit 2. Use "Crtl+d" to quit the file In Linux, everything including hardware is treated as a file. Files can be divided into several types: generic files: Plain text files, binary files. If the file has execute permissions, it is called the Execute file. Directory: Contains files. Device files: Character files, disk files. Internal process Communication file: Socket file, connection file (pipe, FIFO) two kinds. Special files: Symbolic link file, soft link or symbolic link hidden file: The first character of the file name is a dot (.) Common file Code:-General File D directory l Symbolic link file B Block file C character file s socket   Part P pipe File directory directory is also a file, the directory can contain subdirectories and files. Wildcard wildcard is the style of a file name that can be used to compare filenames to find matching files. * represents an arbitrary string, can be an empty string?   Represents a character, but cannot be thought of as a non-bearish backslash \ is an escape character, also known as an escape character, in order to make the subsequent character revert to the original use of the character. The character set [ABC] represents any of the characters in the a,b,c [a-z] for any one of the characters in A to Z, and the lowercase English letter [A-z] represents the uppercase English letter [a-za-z] for the letter [0-9] means that the number [a-z_-] represents any lowercase English letter, _ or- [!0-9] Represents a non-numeric [0-9!] Represents a number or exclamation mark [\!0-9] represents a number or exclamation mark bracket Extension {g,nc,s}ftp represents gftp ncftp sftp









The basic structure of a shell program
The shell structure is generally composed of setting variables, built-in commands, shell syntax structure, and functions.
 Use case description:
test.sh #! / bin / bash #Describe the use of / bin / bash as an interpreter for this script # Define a function function my_fun () {echo "Hello, $ 1, today is $ 2"} #Define a variable name = $ 1 today = `date` #function call my_fun" $ name "" $ today "The script above needs some operations to run, first give execute permission chmod + x test.sh and then execute ./test.sh john output Hello , john, today is Tue Jun 1 14:51:46 CST 2010 Before the parent and child shells execute the script, the environment they are in is the parent shell. When the script is executed, the parent shell forks out a new shell environment according to #! / Bin / bash, and then executes it in the subshell. After the execution is completed, the subshell ends and returns to the parent shell, which will not affect it. The environment of the parent shell. Login shell and non-login shell Shell programming entry step by step __programming This picture is the flow of the login shell. When it is a non-login shell, only the part marked in the box is executed. From this figure we can know the execution process in the following situations. Login / etc / profile ~ / .bash_profile Logout ~ / .bash_logout Execute a new shell, which is divided into two cases 1. Execute interactive shell ~ / .bashrc / etc / bashrc 2. Execute non-interactive Shell, such as executing the script will check the content of the BASH_ENV variable, if it is defined, then execute









variable 

variables are places that are temporarily used to store data, which is a memory space. Bash shell and other programming languages, there is no "data form", that is, by default, does not distinguish whether a variable is integer or float, etc., unless you use the DECLARE statement to declare the variable type. In the bash shell, the default is only one data type, which is a string consisting of characters. At the same time, the set of variables exists only in the current shell, that is, each shell will maintain a copy of their own variables, the other will not affect each other.   You can export variables to environment variables so that other shells can be referenced by the shell. Naming rules for variables: 1. You can use English letters, numbers, and underscores to make up 2. The first character cannot be a number 3. Case-sensitive variable settings: variable name = value For example: Name=john recommend that you set the variable: name= "John" or Name= ' John ' when referencing a variable Double quotes and single quotes are different, and single quotes do not replace variables. In double quotes, if you also want to suppress variable substitution, you need to use the escape character backslash \ Reference variable: $ variable name is recommended to refer to a variable: ${variable name} environment variable: Use any of the following methods to make name into an environment variable 1.name= "John" Export name 2.export Name= Some important built-in variables for "John" 3.declare-x name= "John" bash: $ ~ $n parameter position. When n exceeds 9, use ${n}, for example ${10} $* to represent all parameter positions, and as a string $@ represent all parameter positions, but the number of serial $# parameters that represent each positional parameter is $? The return value of the previous command $! Number of the previous background process $$ the current shell's process number









3 topics in advanced variable advanced variables: Variable extension: ${variable name} command substitution: $ (command) Arithmetic extension: $ (arithmetic) test variable exists or not r=${name-' John '}, if name does not exist, then "return" to default value. The test variable does not exist or is vacant r=${name:-' John ' if name does not exist or exists but is empty, then the default value is returned. The test variable does not exist or the value is empty, then a default value of r=${count:=100} is given to the variable. The test variable does not exist or the value is empty, then the error message ${variable name:? Prompt message}









arithmetic operations

are summed up in 5 ways 1. Arithmetic extensions, such as r=$ (()) 2. Use external program expr, such as r= ' expr + 3. Use $[], such as r=[1+1] 4. Use command declare, such as Declare-i r=1 +1 5. Use the command let, for example let r=1+1









Process Control

if-then if condition; Then action fi


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.