Preliminary understanding of shell script Programming

Source: Internet
Author: User
Tags arithmetic operators shebang egrep

Preliminary understanding of shell script Programming

Programming is the process of writing program source code using human natural language or machine language. This is what we all know, because IT, after all, has programmed the Student Achievement Management System and the library management system. And so on.

Programming in order to allow users to complete some tasks in non-interactive mode when using computers, users need to edit such tasks into a file, it also allows computers to read tasks in a specific order to achieve the expected functions;

To enable the computer to interpret and correctly execute such files, the program source code file must be converted to a binary format that can be recognized and used by the computer. This conversion process is called compilation; to complete the compilation process, you must use a specific compiler tool. Therefore, no matter which programming language is used for programming, all programs must be written in strict accordance with the specific format and syntax structure recognized by the compiler;

Programming Language:
Advanced language:
Classification based on the source code processing method:
Compiled language:
Source code --> Compiler (Compilation) --> [linker (Link) -->] assembler (assembly) --> executable binary code file;
Interpreted language:
Source code --> interpreter (line by line) --> interpret and execute

Whether to call the library or call external program files based on the functions in the programming process:
Complete programming language:
Programming Using libraries or programming components;
Script Programming Language:
Use the interpreter to call the selected external application;
From this we can see that the shell script is not a program and can only call external applications.
Categories based on program writing specifications:
Procedural language: for example, C Language
Program = command + Data
The data and data structure are designed based on the command functions. The data serves as the Command Service:
Implementation form of algorithms and commands:
Sequential execution
Select execution
Loop execution

Object-oriented languages: such as C ++ and java
Program = Algorithm + Data Structure
Data and data structures are the center. Data is instantiated and algorithms are deployed based on data requirements;
Class: instantiated data
Attribution: the basis for distinguishing different objects in the same class;
Method: The correct operation method of the class;

Low-level language:
Assembly Language

Machine language: binary language
Shell script programming-bash Script Programming:
Procedural programming language, explanation of running programming language, and script language (depending on External Application Files)

What is shell script?
1. plain text document-all commands and data stored or contained in files are stored in characters;
2. A simple or complex command assembly body that solves user problems based on user needs;
3. It is a program entity with "Execution Power Equality:
Execute idempotence: The execution results of any command are consistent with those of multiple executions;

Note:
Many Commands do not have the idempotence of execution. Therefore, a shell script requires a large number of program logic to determine whether a command meets its running conditions, to avoid serious errors during running:

How do I write the code in shell scripts?
1. The first line must be shebang, that is, the absolute path of the interpreter program. It must be the first line of the absolute line and be the first line of the interpreter. When executing the script, the corresponding interpreter will be started according to shebang instructions to explain many commands in the script; (similar to the first line of the C language # include <stdio. h>)
#! /Bin/bash


2. in shell scripts, except for shebang, all rows whose names start with # are interpreted as comment rows: that is, the interpreter only interprets the content but does not execute it; (Same as the C language)

3. The Interpreter ignores all the blank lines in the script file. It indicates that in a line of text, there are no other characters except the blank, space, and Indicator Characters;

4. A large number of commands and keywords
Command: internal or external application
Keywords: commands built into shell that can only be executed in a specific struct; keyword;
Such as: if, else, then, do, while, for, select, until, case, fi, esac,

5. All special function characters in shell;

Note: All commands, keywords, and symbols written into the shell script document must be ASCII characters. Other characters in the encoding format can appear in the shell script, but does not have any special meaning;

How to Write shell scripts?
You can use all text document editing tools to write shell scripts, such:
Nano, vi, vim, pico, emacs ,...
Vim is recommended for linux hair styles;

Script File naming:
Generally ". sh "name suffix; editing tools of earlier versions identify whether the file is a shell script file based on the file suffix name; text editing tools of later versions, such as vim7, you do not need to worry too much about file extensions.

Script running mode:

Script content:

#! Bin/bash
#
A = "'egrep" ^ [: space:] * $ "/etc/grub2.cfg | wc-L '"
B = "'egrep" ^ [: space:] * $ "/etc/issue | wc-L '"
Let c = $ a + $ B
Echo "$ c"

1. Grant the execution permission to the script file. You can directly run the file in an absolute or relative path:
# Chmod + x/PATH/TO/SOME_SCRIPT_FILE
#/PATH/TO/SOME_SCRIPT_FILE

Note: If the directory PATH of the script file is stored in the PATH variable, you can directly execute the script file name;

2. Run the script directly using the interpreter and use the script file as a parameter of the interpreter program;
# Bash/PATH/TO/SOME_SCRIPT_FILE
Common options for bash commands:
-X: enables bash to display the script interpretation process on the standard output. It is generally used for troubleshooting shell scripts;
-N: Pre-execute the script file to analyze whether there are syntax errors in the script. If there are no errors, no information is output. On the contrary, a concise prompt is output; you must determine the specific error location;

Note: In this method, it is not an important attribute to check whether the script file has the execution permission;

Note: When executing the script, a new sub-shell is opened in the current shell to run the script. Generally, when the script is running, the sub-shell will also be destroyed. Therefore, it is best to explicitly remove all variables defined in the script at the end of the script; unset gc

[Root @ localhost ~] # Bash-x/tmp/test1.sh
+ + Egrep '^ [[: space:] * $'/etc/grub2.cfg
++ Wc-l
+ A = 17
+ + Egrep '^ [[: space:] * $'/etc/issue
++ Wc-l
+ B = 1
+ Let c = 17 + 1
+ Echo 18
18
3. Run the script using the source command:
# Source/PATH/TO/SOME_SCRIPT_FILE
#./PATH/TO/SOME_SCRIPT_FILE

[Root @ localhost ~] # Source/tmp/test1.sh
18
[Root @ localhost ~] #./Tmp/test1.sh
18

Note:
1. The source command does not enable the sub-shell when running the script, but runs in the current shell;
2. Do not include exit commands in the scripts executed using the source command;

Contact: write a script:
If the Alex user does not exist, create it. If the user is successfully created, a prompt message is displayed. Otherwise, the user already exists;
#! /Bin/bash
#
! Id alex &>/dev/null & useradd alex & echo "cerat alex" | echo "no creat alex"

Use a bash script to perform arithmetic operations:
Arithmetic Operators:
Common basic arithmetic operators:
+,-, *,/, % ,**

Enhanced arithmetic operators:
+ =,-=, * =,/=, % =

Special enhanced arithmetic operators:
++ ,--

Arithmetic Operation Method:
1. $ [expression]
The expressions can be composed of numbers only or variables can be used to reference variable values. When using variables, you are willing to omit the $;

Example:
# Echo $[3 + 4]
# NUM 1 = 5; NUM2 = 4; echo $[NUM1 + NUM2]
2. let VAR = EXPRESSION
Perform arithmetic operations based on arithmetic expressions and assign values to specified variables;

3. $ (EXPRESSION ))
Same as 1

4. expr ARGU1 ARGU2 ARGU3
ARGU1 and ARGU3 must be integer values, and ARGU2 must be Arithmetic Operators.

5. echo "EXPRESSION" | bc

6. bc <"EXPRESSION"

This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151485.htm

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.