1, Shell is the operating system Terminal command line
(1) The shell can understand the command-line interface that the software system provides to the user, which can be said to be a way of human-computer interaction.
(2) We can use the shell and the operating system, uboot and other software systems to interact. Specifically, we use the shell to enter the command into the software system and then enter the execution, and then return to the shell command line after execution to enter the command execution again.
(3) The above operating methods generally work very well, but there are defects. For example, we want to create a file under Linux A.C, can touch A.C but if I now use Linux under the creation of 100 files, respectively, a1.c, a2.c.....a100.c if this time or manually to the command line under the command of the creation can be, but very tired. The best thing to do is to write the creation process into a shell script, and then execute the shell script, which performs the same effect as the command line by manually typing those commands. (Recall when creating a Arm-linux-xxx symbolic link when installing the cross-compilation toolchain in arm bare metal)
2. Shell is a kind of programming language
(1) The language used to write shell scripts is the shell language, also called the scripting language.
(2) Shell scripts are actually a class of languages rather than a language.
3. Common Shell language: sh, bash, csh, Ksh, Perl, Python, etc.
(1) The scripting language used in Linux is actually bash, SH;
(2) Perl, Python, such as advanced shell scripting language, commonly used in network management configuration and other fields, the system operators generally want to learn these.
(3) scripting language is generally used in embedded, mainly for configuration. (A complex embedded program is configurable, and the configuration process is implemented in scripting languages) naturally does not use overly complex scripting language features, so only targeted learning is required.
(4) The most commonly used script under Linux is bash, and our study is also based on bash.
4. Shell script running mechanism: explanation run
(1) C language (c + +) This writing process is: Write the source code (the source code is not directly run) and then compile the link to form an executable binary program, and then to run, and the script program is different, the script is written after the source code can be run directly (no compile link process)
(2) Shell program is interpreted to run, so-called interpretation of the operation is that when we execute a shell program, the shell parser will be line-by-row interpretation of the shell program code, and then a line to run. (Sequential structure)
(3) The CPU actually knows only the binary code and does not know the source code at all. The script source code is actually not binary code, the CPU also does not know, also cannot execute directly. However, the process of compiling and linking the script is not done in the source code of the script, but rather in the process of running the script from the source code to the binary (not necessarily the compile link, because this line of script may have already been compiled and connected well, here I They just call it).
5, write the first shell
1, editor, compiler, Run method (3 ways to execute the script)
(1) Shell program is in text format, As long as the text editor is available. But because our shell is running under a Linux system, the line break must be ' \ n ' and the line break under Windows is "\ r \ n", so The editor in windows writes a shell that cannot run under Linux. So our entire course was written and debugged under Linux using the VI editor (which is actually vim). The
(2) compiler is not involved because the shell is an explanatory language and can be run directly after editing.
(3) shell programs run in several ways, here are three ways to do this:
The first:./xx.sh, as in running binary executable methods. Running the shell this way requires the shell program to have executable permissions. chmod a+x xx.sh to add executable permissions.
The second: source Xx.sh,source is a command of Linux, which is used to execute a scripted program. This operation does not require the script to have executable permissions.
The third: Bash Xx.sh,bash is a script interpreter, essentially an executable program. This execution is equivalent to executing a bash program and then passing xx.sh as argv[1] to him.
2. Hello World Procedure and explanation
(1) The first line of the shell program is generally: #!/bin/sh this jargon begins with #!, followed by a pathname, which means to specify which interpreter is interpreted by the shell program when it executes. So here we write/bin/sh meaning that this shell will be executed by the SH executable in the/bin directory of the current machine.
You can write the first line as: #!/bin/bash to specify that the script be executed using bash.
Note: The interpreter sh, which is used by default on Ubuntu, is not actually bash, but dash. Dash is the scripting interpreter used by default in Ubuntu.
(2) The comments in the script use #, #开头的行是注释行. If you have more than one line to comment on, precede each line with #. (#就相当于是C语言中的//)
(3) The body of the shell program is composed of many line shell statements.
3. The shell is not mysterious
(1) The shell is the program that executes the command typed in the previous command line. The shell is actually a technique that is invented to avoid repeated manual input at the command line, to record manual input steps, and then to re-retell the manual input process of the original record by executing the shell script program.
(2) Shell editing can be run directly (do not need to compile)
6. Variable definitions and references in the shell
(1) Definition and initialization of variables. A shell is a weakly typed language (a variable in a language is a strongly typed language if it has a definite type), a variable with no explicit type is a weakly typed language, and a different C language. Defining variables in shell programming does not require a type, nor is there a concept of type.
(2) Variable definition can be initialized, use = to initialize the assignment value. The value assigned in the SHELL = There cannot be spaces on either side.
Note: The shell is very concerned about the syntax, very strict. A lot of places have to be empty or must have, and can not arbitrarily have no spaces.
(3) Variable assignment, the variable can be assigned again after the value, the new assignment will overwrite the old assignment. The shell does not deliberately distinguish between the definition and assignment of variables, anyway, each variable is a symbol, the value of this symbol is the last value assigned to him.
(4) Variable reference. A variable referenced in the shell must use the $ symbol, and the $ symbol is the variable dereference symbol.
Note: The $ symbol is followed by a string, and the string is parsed as a variable. If the string itself is undefined, execution does not give an error, but instead resolves the variable to null. In other words, a variable that is not defined in the shell is actually a variable that is defined and assigned an empty value.
Note: Variable references can be $var or ${var}. The difference between the two is that in some cases you can only use ${var} and not simply $var
Shell Programming Learning 1