Shell Script Programming Basics

Source: Internet
Author: User
Tags naming convention python script

One: Program concept
? Program: Algorithm + data structure
? data: Is the core of the program
Data structure: The type and organization of the information in the computer
? algorithm: How data is processed

Program Programming Style:
Program: command-centric, data-serving instruction
Object type: Data-centric, instruction serves data

Advanced Programming Languages:
Compiled: High-level language--compilers--machine code--Execute
C,c++

Interpreted: High-level language-----------machine code
Shell,python,php,javascript,perl

The shell (bash) in Linux is a process-oriented, interpreted programming language.

The difference between compiled and interpreted language execution is as follows:

II: Shell Script Basics
★ What is a shell script?

A: Shell scripts are text files that contain commands or declarations and conform to a certain format: the first line of the shebang mechanism, such as #!/bin/bash (which explains what the script does with the interpreter), and if it is a Python script, it can just #!/usr/ Bin/python, which explains that this script is executed in Python.

★ How the script executes and cautions.
①: Scripts are executed in a number of different ways, as follows.
? 1: Give script execution permissions chmod 755 + script, using absolute or relative path execution
2: Use the interpreter to run directly such as: SH hello.sh this way you do not need to give script execution permission.
? 3: Use source or. To invoke, this way is to run the script directly in the current shell process, note that this way the variable will be directly in the current process is effective.

②: Scripts should be aware of the following specifications.
Program name, avoid changing the file name to not find the correct file
? version number
? The time after the change
? Author related information
The role of the program and precautions
? Finally, a brief description of each version of the update

③: Script Debugging method.
? Bash-n/opt/hello.sh is used to check the script for syntax problems.
? Bash-x/opt/hello.sh the execution of the process is displayed on the screen, detecting script problems when used frequently.

Three: The variable's type is the variable's naming convention.
★ What is a variable?
A: Named memory space.

★ Variable naming laws.
? You cannot make a reserved word in a program: for example if, for.
? You can use only numbers, letters, and underscores, and you cannot start with a number.
See the name of righteousness.
? Uniform naming rules: Hump nomenclature.

The type of the variable in the ★bash.
? local variable: The effective range is a snippet of code in the current shell process, usually referred to as a function. such as Name=fang
? position variable: $, $, ... To make the script call through the command line in the script code.
Pass the argument to it
? environment variable: The effective scope is the current shell process and its child processes. (an environment variable is defined in the current process that does not take effect in its parent process.)
? Special variables: $?, $ A, $*, [email protected], $#,$$

? $?: The previous command executed the result status code, 0 indicates that the previous command was executed properly, responsible for the last command execution failed, commonly used in scripts. Br/>?$0: The name of the current script.
? $*: All parameters passed by the current script.
$#: The number of general staff that the current script passes.
[email protected]: All parameters passed by the current script.


The difference between [email protected] and $ can be more visually displayed in a circular way.

You can see
that the $ is to take all the parameters as a whole, and [email protected] is a separate parameter.

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