1, Shell programming (shell script) _ Understand programming and variables

Source: Internet
Author: User
Tags stdin

在study(Shell)专栏中,会深刻学习到shell编程

And as an OPS person, the most basic ability is to learn shell script programming
To improve efficiency in our work!
Shell Programming:
Compiler: Also called interpreter
The shell provides us with another function: To facilitate our work!

Programming language: Turning human language into a machine-understandable language
Machine language (01 code)
Assembly language (still very low, growth cycle is longer, learning is more difficult)
High-level language (however, it is not a human language, but the closest to human language)
Static and dynamic languages

1. Static language: Compiled language
(There is a program development environment, do not need to use the additional binaries, write code directly, after the completion of the need for a compiler, put the code into the compiler can be given to the hardware to form a separate running program, we call him static language)
To convert into an executable format beforehand; for example, java,c++.
A key feature required for static languages: strongly typed (variable)
Key words:
Complete conversion before execution, prior to conversion to executable format program.
For example, Java is just the beginning of a. java file (that is, the source code), compiled into an executable file is a. class file (that is, the compiled file, it is not the source)

2. Dynamic language: Interpreted language on the fly
(a code after our programmer has finished writing, he does not need to convert to binary format, but there is an interpreter, explain a, execute one, that is, after the execution of the conversion)
(usually) weak type: Edge interpretation Side Execution
Like Php,shell,python (very popular, object-oriented, many game scripts are used in Python), Perl (earlier popular: pronunciation: po (broken))
Like c,c++,java,c# (no interpreter needed, self can run)

BASH: The easiest to learn and the easiest to get started with is the shell
If you can learn Python is more powerful, add a lot of wings.
Perl process-oriented, although perl6 with objects, but now for a variety of reasons also not popular
In fact, a lot of scripts are developed in Python, in the study of computers for many years, have not learned python, so school is disjointed

Process-oriented: relatively small application development: Shell,c
The focus of programming is mainly on the process of problem solving itself.
Object-oriented: relatively large application development: JAVA (Object-oriented language only), Python (object-oriented language only), perl,c++
Object-oriented meaning: Abstract the entire project we want to implement, and define the action between objects, is the operation can be issued to complete, so this is more suitable for the development of large programs
But any tool is a double-edged sword (a chopper can kill or cut a dish)

To understand programming, you have to understand variables
Variable (changing amount): Is the memory space, the name of the content space (is the amount of change, stored in a memory of the storage space, according to the address can find this)
Memory is the storage unit that is stored
We correspond to variables and memory addresses, so what we see is a variable, like a, which corresponds to a memory address space.

Instance:
10:16 bit required for character storage
10: Stored as a numeric value, is 1010, just need 4 bits is enough, of course, the smallest memory of the computer is a byte, so this is also required 8 bits.
By the above we know that our variables need to have types of

1. Character type
2, numeric type, and the value type is divided into:
1. Integral type
2, Float type: 11.23 (storage example: a place before the decimal point, there is a place after the decimal point, there is a position in the decimal point), it may be stored in this format 1.12310^1,0.112310^2 (may only save 1123 and 2)
If we want to store the year, for example: 2013/10/10 and 99999, the format and size of the storage are different.
So we determine the type of the variable: in advance to determine the storage format and length of the data
Memory: Addressable storage unit

What is overflow?
The following example
• Integral type, 8bit:256
• 0-255, full, flowed out, overflowed
If the overflow of data takes up space in other people's processes, the other is overwritten, and if this well-designed buffer overflows, some attacks exploit this overflow.
Simple to prevent overflow: see the data and my program variable type is appropriate, inappropriate to return an error, do not let it write. Therefore, the variable is required type.
So the good code will check the variables and check for exceptions (for example: India's outsourcing industry is much stronger than China's, that's why)

Boolean type: TRUE or False
Logic: 1+1>2
Logical operations: With, or, non-, XOR (the same operand is false, and the operands are different true)
! true = False
! False = True
Actually, the electrical logic of our computer is like this.
In fact, the circuit of our entire digital circuit is this kind of operation, we can also design our own adder
On this basis the constant addition, becomes a complex function.
As far as speaking, as long as there is a false, the result must be false
For or to speak, as long as one is true, the result must be true

is a principle of electromagnetic induction (this is the computer professional name said: Non-door)

The whole computer is just a bunch of these doors to compute.

Strongly typed programming:
Variables must be declared beforehand before they are used, or even initialized values are required:
Null:
Weakly typed programming:
Variables are declared when they are used (directly with them), even if the type is not differentiated (the default is a string if it is not distinguished):
Our shell programming is a weak type of programming language

Variable Assignment Var_name=value (this value is stored in the variable space) such as: shengao=175

Programming Ability:
Bash Script programming
Bash Variable type:
Environment variables
Local variables (local variables (local variables are not necessarily local variables))
Positional variables
Special variables (bash built-in to hold bash special data, others call IT system variables)

Local variable: (the variable must be a variable of the process)
[Email protected] ~]# Name=jerry
[Email protected] ~]# echo $NAME
Jerry
We refer to variables with: ${varname} (usually {} can be omitted), as above
But sometimes it causes the variable to be confused, we need to add curly braces, as follows
[Email protected] ~]# echo $NAME
Jerry
[Email protected] ~]# echo "My name is $NAME"
My name is Jerry
[Email protected] ~]# echo "My name is $NAMEs"
My name is
[Email protected] ~]# echo "My name is ${name}s"
My name is Jerrys
[Email protected] ~]# echo "My name is ${name}:s+w"
My name is tom:s+w can see that some characters are not recognized, such as:, + ...
Child bash: Open a shell in a shell

The learning of variables and the types of variables
Local variables:
Varname=value: scope for the entire bash process:
Local variables:
Local Varname=value: Scope is the current code snippet
Export defines an environment variable, the scope of the environment is as follows (export means: "exports")

Varname=value
Export VARNAME

Of course, environment variables do not work if you reopen a shell, but the child shell is useful.

Positional variables (which are used to refer to the parameters of the script):
$1,$2, ...
such as the following example
#!/bin/bash
Let Sum=$[$1+$2]
Echo-e "The sum is $sum"
And then you can output the results.
[Email protected] ~]#./myshell.sh 15 96
The sum is 111
[Email protected] ~]#./myshell.sh 5 9
The sum is 14

Special variables:
$?: The return value of the execution state of the previous command, the output we see is the result of the execution, if it is correct or wrong, it is the state result, also called the status return value
program execution, there may be two types of return values, one is correct, one is wrong
$?: The execution status return value of the previous command: if it is not 0 (execution status return code), the execution state is wrong, and if 1-255 is wrong, it is not the correct execution state.
1-255 system reserved Three 1,2,127, other can be defined by their own
[Email protected] ~]# ifconfig &>/dev/null
[[email protected] ~]# echo $?
0
&> error or correct output to/dev/null (software simulation device, also known as bit bucket (data black hole) put what goes in will disappear), and/dev/null is the garbage station, anything can receive, we use the above command, Just to verify that the last command is correctly output, echo $?, so put it in null instead of displaying the screen.

After the variable is exhausted, it is useless, we can also undo the variable
Originally we define a variable to have a set, but in fact it has the default, so we do not use set, and Undo is to use unset VARNAME

Set
View all variables in the shell directly with set, including all variables (including environment variables)
If you only view environment variables with PRINTENV or env or export
Use: Add something on the basis of the environment variable (it is not recognized as a variable name, so you do not need {})

Similar to window, use: Separate to add a new variable
Remember: This symbol--the variable is not able to do arithmetic operations by default

What is a script?

How to write a script: In fact, is the order of the stack, according to the actual needs, combined with the command flow control mechanism to implement the source program script.
Linux can only recognize the ELF format to execute (that is, the first line of the script must be #!/bin/bash,bash is the interpreter,/bin/bash is the interpreter path), so that an ASCII-formatted text can be executed
Almost all of our files are in Aciss format.
So we want to run the script file, must be in the first line of the file with
#!/bin/bash (then the # that comes up again is the # of comments (that is, as an effective component)
So once the file is executed, it will read the first line, and it will run with the Bash editor.
And know that the shell script file is going to use the. sh suffix, just like a local warehouse to use. Repo.
As a shell script below, used to enter the network configuration file
[Email protected] tmp]# vim 1.sh
#!/bin/bash
cd/etc/sysconfig/network-scripts/
Vim ifcfg-eno16777736
[[email protected] tmp]#/1.sh./indicates running the current directory script
If you do not have X execute permission, but do it as someone else's argument, you do not have to execute permissions
You can bash 1.sh directly (and this way, you don't need to #!/bin/bash to call the compiler at the beginning, because it's already done through bash.

Practice

Useradd user001
Useradd user002
echo "112233" | passwd--stdin user001 &>/dev/null
echo "112233" | passwd--stdin user002 &>/dev/null
Echo user001 passwd Shezhi Successful
Echo user002 passwd Shezhi Successful

CHINANAME=USER001 (cannot be set chinaname=user001 in script, test discovery is unsuccessful)
Userdel-r $Chinaname
echo "The user already delete"

1, Shell programming (shell script) _ Understand programming and variables

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.