Shell script Programming

Source: Internet
Author: User

Shell script Programming

The history of programming languages: Machine language--Assembly languages--high-level languages

Categories of programming languages:
(1) According to the operating mode
Static language: Compiled language, pre-converted to executable format C, C + +, JAVA, C #
Compile run: Source code--compiler (compile)---EXE file

Dynamic language: Interpreted language, side-interpreted side execution shell, Python, Perl, ASP, PHP
Explanation Run: Source code--run-time start interpreter, run by interpreter side interpreter side

(2) According to the implementation of the function in the programming process is to call the library or external program files to divide
Shell scripting: Programming with commands and programming components on the system
Full programming: Programming with libraries or programming components

(3) Programming Model:
Process-oriented: organizing code with instruction, data serving instruction
Suitable for developing small programs, programming with a focus on the problem-solving process itself
Representative: C, Shell

Object-oriented: data-centric organization of code, directives serving data
Ideal for developing large programs that are designed to abstract items into objects and define actions between objects
Representative: JAVA, Python, Perl, C + +

Shell scripting Programming Definition: 1, process-oriented programming, relying on shell interpreter to interpret execution
2, after the command stack, according to the actual demand combined with command control process mechanism, realize the programming effect

The purpose of the shell script:
1, automate the execution of common commands
2. Working with text or files
3. Perform system administration and troubleshooting
4, create a simple application

Programming logic Processing: Sequential execution, select execution, loop execution

Variable: named memory space
How data is stored:
Character:
Value: Integer, floating-point

The role of variables:
1. Declare data storage format
2, affirm the participation of the operation
3. Stated data range

Variable type:
Strongly typed: The type must be defined when the variable is defined, and the participating operation must meet the requirements
Must be declared prior to use and even initialized
such as: Java,python

Weak type: No type is required, the default is character type, and the participating operation automatically
For implicit type conversions, variables can be called directly without prior declaration
For example: Bash does not support floating-point numbers

Variable naming laws:
1. Cannot make reserved words in the program: for example: if, for
2, only use numbers, letters and underscores, and cannot start with a number
3. See the meaning of the name
4, the Uniform naming rule: Hump name law

Bash variable type: Based on variable effective range
Local variable: The active scope is the current shell process and is not valid for Shell processes other than the current shell, including the current Shell's child shell process
Environment variable: The active scope is the current shell process and its child processes
Local variables: The effective range is a snippet of code in the current shell process (usually referred to as a function)
Positional variables: $, $, ... To indicate that the script is used to invoke parameters passed to it through the command line in the script code.
Special variables: $?, $, $*, [email protected], $#

Local variables
Variable assignment: Var-name=value
(1) String: var-name= "root"
(2) Variable reference: name= "$USER"
(3) Command reference: Var-name= ' command '
var-name=$ (COMMAND)

Variable reference: ${var-name}, $var-name, curly braces can be removed in most cases
"": weak reference, where the variable reference is replaced with the value of the variable
': Strong reference, where the variable reference is not replaced with the value of the variable, while preserving the original string

Show all variables that have been defined: set
Undo Variable: Unset var-name

Environment variables
Variable declaration, assignment:
Export Var-name=value
Declare-x Var-name=value
Variable reference: $var-name, ${var-name}
Show defined environment variables:
Export, env, printenv
Revocation: unset var-name
Bash has many built-in environment variables: PATH, SHELL, Usre,uid, Histsize, HOME, PWD, Oldpwd, Histfile, PS1

Read-only and positional variables
Read-only variable: can only be declared when defined, but cannot be modified and deleted
Declaration method:
ReadOnly Name
Declare-r Name

Positional variables: Calling parameters passed to the script through the command line in script code
$, $, ... : corresponding to 1th and 2nd parameters, Shift [#] Rotation 1-#个变量
$?: The execution status return value of the previous command
$: Command itself
$*: All parameters passed to the script, all parameters are combined into a string
[Email protected]: All parameters passed to the script, each parameter is a separate string
$#: The number of arguments passed to the script
[Email protected] $* only when it's wrapped in double quotes.

Create a shell script
First step: Use a text editor to create a text file
The first line must include the shell declaration sequence: #!
Format requirements: First line shebang mechanism
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/python
Add comments: Comments begin with #

#!/bin/bash #Author: lovefirewall#version:1.0#create time:2008-08-08 08:08:08 #Description:D ISK usage rate alarm

Step two: Run the script
Add Execute permission to specify a relative or absolute path to the script on the command line
Run the interpreter directly without adding execute permissions, run the script as a parameter to the interpreter program

This article is from the "Love Firewall" blog, be sure to keep this source http://183530300.blog.51cto.com/894387/1836991

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