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 what student performance management system, library management system. Wait a minute...
Programming in order to enable users to use the computer, you can let the computer do some tasks in a non-interactive way, at this time, users need to edit such tasks as a file, and let the computer love in a specific order to read the task, so as to achieve the desired function;
In order for a computer to interpret the contents of such a file and execute it correctly, the program source code file must be converted to a binary format that the computer can recognize and use, which is called compilation, and a specific compiler tool must be used to complete the compilation process, so no matter what programming language is used to write the program, Must be programmed in strict accordance with the specific format and grammatical structure that the compiler can recognize;
Programming Languages:
Advanced Language:
Classification according to the source code processing mode:
Compile-language:
Source code--compiler (compile)--[Linker (link)--] Assembler (assembler)--Executable binary code file;
Interpreted language:
Source--Interpreter (line-by-row explanation)--side interpretation edge execution
Depending on whether the functional implementation in the programming process is calling the library or calling an external program file:
Full programming language:
Programming using a library or programming component;
Script programming Language:
Use the interpreter to invoke the selected external application;
From this you can see that the shell script is not a program and can only invoke external applications.
According to the program's writing specification classification:
Programming language: C language, for example
program = instruction + data
With instruction as the center, the function of instruction is implemented to realize design data and structure,data for the command service:
Algorithm and instruction implementation form:
Sequential execution
Select Execute
Loop execution
Object-oriented languages: such as C++,java, etc.
program = algorithm + data structure
The data and data structure are the center, it is instantiated,deploy algorithms around the needs of your data;
Class: The data being instantiated
Attribute (attribution): The basis of distinguishing different objects in the same class;
Method: The correct operation method of the class;
Low-level language:
Assembly
Machine language: Binary language
Shell script Programming--bash scripting:
Programming languages, scripting languages (run with external application files)
What exactly is a shell script?
1. Plain text Document--all the stored or contained instruction + data in a file is stored in character units;
2. Simple or complex command combination to solve user's problem according to user's requirement;
3. is a program entity with the "Power of Execution":
Execute idempotent: The result of one execution of any command is consistent with the result of multiple execution;
Attention:
Many commands do not have "idempotent", so there is a need to use a lot of program logic in the shell script to determine whether a command will conform to its operating conditions, thus avoiding serious errors that occur during the run:
How is the code content in the shell script written?
1. The first line must be shebang, that is: the absolute path of the interpreter program, must occupy the absolute beginning and compulsory to occupy the first line alone. When executing the script, the corresponding interpreter is started according to Shebang's instructions to explain many commands in the script, (similar to the first line of the C language #include<stdio.h>)
#!/bin/bash
2. In the shell script, all lines that start with the # character except Shebang are interpreted as comment lines: that is, the interpreter interprets its contents but does not execute; (as in C//)
3. The interpreter ignores all blank lines in the script file and refers to lines that do not have any other characters except whitespace characters, space characters, and indicator characters in a single line of text;
4. Lots of commands and keywords
Command: Internal or external application
Keywords: commands built into the shell that can only be executed in a particular structure; keyword;
such as: If,else,then,do,while,for,select,until,case,fi,esac,
All special function characters in 5.shell;
Note: All commands, keywords, and symbols written into the shell script document must be ASCII-encoded characters, and other encoded-format characters can appear in the shell script, but without any special meaning;
How do I write a shell script?
You can use all of the text document editing tools for Shell scripting, such as:
Nano,vi,vim,pico,emacs, ...
Vim is usually recommended for each hairstyle version of Linux;
How the script file is named:
In general, the name suffix of ". Sh" will be set for the script, and the lower version of the editing tool will identify whether it is a shell script file based on the suffix name of the file, and a higher version of the text Editing tool, such as VIM7, without having to worry too much about file suffix names.
How the script runs:
Script content:
#!bin/bash#a= "' Egrep" ^[[:space:]]*$ "/etc/grub2.cfg | Wc-l ' "b=" ' Egrep "^[[:space:]]*$"/etc/issue | Wc-l ' "Let c= $a + $becho" $c "
1. Give execute permissions to the script file, which can be run directly as an absolute path or a relative path:
# chmod +x/path/to/some_script_file
#/path/to/some_script_file
Note: If the directory path where the script file resides is stored in the path variable, it is executed directly with the script file name;
2. Run the script directly using the interpreter and use the script file as the parameter of the interpreter program;
# Bash/path/to/some_script_file
Common options for Bash commands:
-X: Enables bash to interpret the script in the standard output, typically used for Shell scripting errors;
-N: The script file is pre-executed to parse the script for syntax-class errors, and if there is no error, no information is output;
Note: In this way, whether the script file has execute permissions is not a very important property;
Note: In both of these ways, a new child shell is opened in the current shell to run the script when the script is executed, and in general, the child shell is destroyed as soon as the script is run, so all variables defined in the script, at the end of the script, should be explicitly revoked ; unset GC
[Email protected] ~]# 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 1818
3. Run the script using the source command:
# Source/path/to/some_script_file
# . /path/to/some_script_file
[Email protected] ~]# Source/tmp/test1.sh18[[email protected] ~]#. /tmp/test1.sh18
Attention:
The 1.source command does not open the child shell when the script is run, but runs in the current shell;
2. Do not include commands such as the exit class in scripts executed with the source command;
Contact: Write a script:
Created when the Alex user does not exist, or if it is successfully created, displays the prompt message that was created successfully, or the user already exists;
#!/bin/bash
#
! ID Alex &>/dev/null && useradd Alex && echo "Cerat Alex" | | echo "No creat Alex"
Use the bash script to implement arithmetic operations:
Arithmetic operator:
Common basic arithmetic operators:
+,-,*,/,%,**
Arithmetic operators with enhanced type:
+=,-=,*=,/=,%=
Special Enhanced Arithmetic operators:
++,--
Arithmetic Operation Method:
1.$[expression]
The expression can be made up of pure numbers, or you can use variables to reference variable values, and you may want to omit the $ symbol when using a variable;
Example:
# echo $[3+4]
# NUM 1=5; num2=4; Echo $[num1+num2]
2.let var=expression
Completes the arithmetic operation according to the arithmetic expression and assigns the value to the specified variable;
3.$ ((EXPRESSION))
Same 1
4.expr ARGU1 ARGU2 ARGU3
where ARGU1 and ARGU3 must be integer values; ARGU2 is an arithmetic operator.
5.echo "EXPRESSION" | Bc
6.BC <<< "EXPRESSION"
Preliminary shell script programming