Shell and makefile that complement the foundation

Source: Internet
Author: User
Tags case statement echo command switch case

2.2.1, Shell Introduction
    • (1) Shell can be understood as a software system to provide user operation of the command line interface, it can be said that he is a way of human-computer interaction
    • (2) We can use the shell and the operating system, uboot and other software systems to interact. In particular, we enter the command through the shell to the software system, and then enter execution, and then return to the shell command line to enter the command again after execution.
    • (3) The way we do this generally works very well, but there is a flaw. For example, we want to create a file under Linux A.C, can touch A.C, but if I now want to create 100 files under Linux, respectively a1.c a2.c a3.c..............a100.c

If it's time for us to go to the command line and execute the command, you can wear it, but it's tiring. The best thing to do is to write the creation process into a shell script, and then execute the shell script, and the effect of executing the program is the same as when you manually type in the command line. (Recall that installing the cross-compilation toolchain in arm bare metal is to create a arm-linux-xxx symbolic link)

2.2.1.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
2.2.1.3, common shell languages: sh, bash, csh, Ksh, Perl, Python, etc.
    • (1) The scripting language used in Linux is actually bash, sh
    • (2) High-level shell scripting language such as Perl and Python, commonly used in the Network Management configuration field, the system operation and maintenance personnel should generally learn these
    • (3) scripting language is generally used in the embedded, mainly for the configuration (a complex embedded program is configurable, the configuration process is implemented in scripting language) naturally does not use the overly complex scripting language features, so only the targeted learning can be
    • (4) The most common script under Linux is bash, and we learn to bash.
2.2.1.4, Shell script operation mechanism: explain the run
    • (1) C language (c + +) This writing process is: to write the source code (the source code is not directly run) and then compile the link to form a binary program, and then to run the script program is different, the script is written well after the source code can be directly run (no compile link process)
    • (2) Shell program is interpreted to run, so-called interpretation of the operation is that when we execute a shell, the shell parser will be line-by-row interpretation of Shell code, and then one line to run. (Sequential structure)
    • (3) The CPU actually only knows the binary code, does not know the source code, the script source code actually is not the binary code, the CPU also does not know, also cannot execute directly, but the script program compiles the link process not to be in the script program source code bit unit, Instead, the script's source code is turned into binary when the script runs through the execution of a line-by-row interpretation. (not necessarily a compile link, because this line of script might have been compiled, and here we just call it).
2.2.2, write the first shell2.2.2.1 editor, compiler, Run method (execution method in script 3)
    • (1) Shell program is text format, as long as the text compiler can. 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-written shell in Windows cannot run under Linux. So our entire course was written and debugged using the VI editor (actually vim) under Linux.
    • (2) Compiler: Not involved, because the shell is an explanatory language, can be run directly after editing
    • (3) There are several ways to run shell programs, here are three methods:

The first kind:./xx.sh is the same as running a binary executable, so running the shell must require the shell to have executable permissions. chmod a+k xx.sh to add executable permissions

The second: source xx.sh source is a command of Linux, which is used to execute a script, so that the runtime does not require the script to have executable permissions

The third type: Bash xx.sh bash is a scripting interpreter, essentially an executable program. This execution is equivalent to executing a bash program and then passing xx.sh as argv[1] to him to run

Explanation of 2.2.2.2, Hello World program
    • (1) The first line of the shell program is generally: #!/bin/sh the jargon begins with #!, followed by a pathname, which means that the shell execution is interpreted by the interpreter 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.
    • The first line can be written as; #!/bin/bash to specify that the script be executed using bash.
    • Note: On Ubuntu, the default interpreter sh is not bash, but Dash.dash is the default script interpreter used in Ubuntu
    • (2) The comments in the script use #, #开头的行是注释行, if there are more than one line need to comment, each line before the addition of # (#就相当是C语言//)
    • (3) The body of the shell program, consisting of a number of line shell statements
2.2.2.3, Shell is not mysterious
    • (1) The shell is a program that executes commands typed in a circle on the command line. The shell is actually invented to avoid repeated manual input at the command line, a technique that records the manual input process and then repeats the manual input process by executing the shell script.
    • (2) Shell editing can be run directly (do not need to compile)
2.2.3, Shell programming learning 12.2.3.1, Shell using Linux commands
    • (1) Exercise 1: Create a file in the current directory A.txt
    • (2) Exercise 2: Create a folder under the current directory dir, create a file under Dir b.txt

Summary: The purpose of the above two exercises is to let everyone basic learning to write scripts, understand that scripting is actually the command line entered before the command to move to the script and then once executed

Variable definitions and references in the 2.2.3.2, shell
    • (1) Definition and initialization of variables. The shell is a weakly typed language scripting language (a variable in a language is a strongly typed language with a definite type in it; a variable with no explicit type is a weakly typed language), unlike our C language. In shell programming, the definition of white energy does not require a specified type, and there is no concept of type in the shell, so variables can be defined directly in the shell.
    • (2) The variable definition can be initialized, using = to initialize the assignment. Both sides of the assignment in the SHELL = There are no spaces on either side.
    • Note: The shell is very concerned about the syntax, very strict. Many places must have no or must have, and can not arbitrarily no space
    • (3) Variable assignment, the variable can be assigned again after the value, the new assignment will overwrite the old assignment. The shell can not distinguish between the definition and assignment of variables, anyway, each variable is a symbol, and the value of this symbol is the last value assigned to him value
    • (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 this is not defined in the case of women, execution does not give an error, but the variable resolves to null. In other words, a variable that is not defined in the shell is equivalent to a variable that is defined and assigned an empty value.

Note: Variable references can be $var or ${var}. These two differences can only be used in some cases ${var} and not simple $var

2.2.3.3, the difference between single and double quotes in a shell
    • (1) The use of strings in the shell can be used without double quotation marks, and the space is also possible, but the flaw is not output "or other escape characters
    • (2) The shell can also use single quotation marks to represent the string, is also directly used, cannot output the transfer character
    • (1) Single quote: full literal conversion (cannot contain single quote itself)
    • (2) in double quotes:

$ plus variable name can take variable value

Anti-quotation marks still indicate command substitution

\$ $ literal Output $ symbol

The literal value of \ ' representation '

The literal value of \ "represents"

\ \ denotes the literal value of \

2.2.4, Shell programming learning 22.2.4.1, Shell calling Linux commands
    • (1) Direct execution
    • (2) The anti-quotes are executed, and sometimes we call the Linux command in the shell to get the return value of the command (the result value), it is appropriate to use a pair of anti-quotes (the key below the ESC key on the keyboard, and ~ on a key) to invoke the execution of the command.
Select Branch structure in 2.2.4.2, shell
    • (1) The Shell's if language usage is many, again introduces commonly used, other interested can learn by oneself
    • (2) Typical if language format

if[expression];then

Xxx

yyy

...

Else

Xxx

...

Fi

    • (3) If the typical application

Determine if the file exists. (-f) Note [] inside and around must be added a space, can not be omitted. Example: if [-f A.C];

Determine if the directory exists (-D)

Determine if the strings are equal ("str1" = "str2"), and note that you use an equal sign instead of two

Determine if the number is equal (-EQ), greater than (GT), less than (LT), greater than or equal to (GE) less than, and so on (-le) recall that in arm bare metal in the arm assembly condition execution, used to judge the abbreviation of these conditions (EQ is equal,gt greater than , Lt is less than, GE is great or equal, le is less or equal) to determine if the string is empty (-Z)

Determines whether a string is empty (-Z)

Note-Z judgment is not true if the variable itself is undefined (that is, Z does not define it as null)

Example:

#!/bin/sh
If [];then "Yes"
Else
    " No "
Fi
#!/bin/sh
str =""
if [-z  $str];then            "Yes"else            "  No"fi
    • (4) If the "-O" is used in the If judgment to indicate logic or. If [12-eq 12-o "ABCD" = "ABCD"];then

Equivalent to 2 formulas in C with bare metal, logic, or in the condition following the IF, the final if is based on the logical result of 2 formulas

    • (5) Logic with && and logic or | | An if expression with shorthand. [-Z $str] | | echo "Fei Kong"
Loop structure 2.2.5.1, for loop in 2.2.5, Shell
    • (1) Requirements: Can read, can change. Does not require to be able to write out completely without reference. Because embedded does not need to completely re-handwriting the shell, system administrator (server OPS, Application layer system-level management development needs to fully master the shell)
2.2.5.2, while loop
    • (1) There is no logical difference in the cycle of C language
    • (2) Be aware of a number of formatting requirements, such as: while the [] side of the [] there are spaces, [] followed by a semicolon (if do put on a line), i++ in the wording of the two-level brackets
2.2.5.3, Echo's Create and append input files
    • (1) In the shell, you can use the echo command to create a new file and write some content to these files. The key to creating a file and entering content is >.
    • (2) You can also use the echo command to append symbols >> append entries to the end of an existing file
Other interesting points of knowledge in 2.2.6, Shell 2.2.6.1, case statements
    • (1) The case statement in the shell acts in the same way as the switch case statement in the C language, with a different format
    • (2) The case statement in the shell is inherently free of break and does not require a break. And the switch case Butong in the C language. The case in the shell is the default match on the one that executes that, does not say the execution is finished and then executes the other case (as if the case language in the shell has a break by default)
2.2.6.2, invoking the shell program's arguments
    • (1) C language can pass the ARGC and argv of the main function to the program to pass the parameter
    • (2) The shell program itself can also be called Shishun to him. Used inside a shell program is also expressed using some specific symbols.

Includes: $ #表示调用该shell时传参的个数. ($ #计数时只考虑真正的参数个数)

$, $, $ ... the parameters of the arguments are represented in turn

C language:./a.out aa bb cc argc = 4, argv[0] =./a.out argv[1] = AA .....

Shell:source a.sh aa bb cc $# = 3 $ A is the name of the parser that executes the shell program. $ $ represents the value of the first valid parameter, and the value of $ is the second valid parameter ....

Many of the syntax features in the shell are the same as in C, and many are different. So the more you learn the more easily confused (the essential reason or the use of unfamiliar, less), the solution is: to do a summary. Take notes, write more code often.

2.2.6.3, while loop and case language and reference combination
    • (1) The break keyword in the shell and the C language have the same meaning (all jumps out) but the usage is different. Because the break in the shell is only used for looping out. So when a case statement is embedded in the while, the break in the box jumps out of the outermost loop, not out of the case statement.
    • (2) The value of the $# built-in variable in the shell is not immutable, but can be changed, and is changed by the shift instruction. The shift command is a bit like the left-shift operator, which shifts the arguments we gave to the shell program to the left, one out, the original $ $ #少了一个
Review of the role and significance of 2.2.7.1 and makefile in 2.2.7 and makefile basis
    • (1) Project C file too many projects, management is not convenient, so use makefile to do project management, easy to compile the link process
    • (2) Uboot and Linux kernel are essentially V-language projects that are made up of many files and therefore need to be managed by makefile. So to analyze uboot must have some knowledge of makefile.
2.2.7.2, goals, dependencies, commands
    • (1) The goal is that we are going to make xxx, the xxx, is the amount of things we will eventually generate
    • (2) dependency is the raw material used to generate the catalog
    • (3) The command is the processing method, so make XXX process is actually using the command will rely on processing into a target process
2.2.7.3, wildcard%, and makefile automatic derivation (rule)
    • (1)% is a wildcard character in Makefile, representing one or several letters. In other words,%.O represents all files ending in. O.
    • (2) The so-called automatic derivation is actually the rule of makefile. When Makefile needs a goal, he sets the target to a set of rules stating that once the jumper has a rule description, makefile will try to find the dependency in the rule, and if it does, it will execute the rule with the dependent birthday target.
Defining and using Variables in 2.2.7.4, makefile
    • (1) pseudo-target means that the target itself does not represent a document, and the goal is not to get a question or something, but simply to execute the following instruction.
    • (2) The goal is generally not dependent, because the pseudo-target is to execute the command under the target. Since you must execute the following command, you do not have to rely on it, because the non-dependency means unconditional execution.
    • (3) pseudo-target can be written directly, should not want to use, but sometimes in order to explicitly declare that the target is a pseudo-target will be used in front of the pseudo-target. Phony to declare it a pseudo-target.
2.2.7.6, Makefile's file name
    • (1) The makefile file name is valid for 2: Makefile or Makefile
Referencing other makefile (include directives) in 2.2.7.7, makefile
    • (1) Sometimes makefile is more complex, so it is divided into several makefile to write. Then refer to the other in the main makefile, with the include directive. The effects of the references are also expanded in place, and the header files in the C language contain very similar
2.2.8, makefile Supplement Learning 12.2.8.1, makefile in the comments with #
    • (1) Makefile in notes use #, like the shell
2.2.8.2, @ at the front of the command line for silent execution
    • (1) The @ in front of the makefile command line means silent execution
    • (2) Makefile in a silent situation before executing a line of command will first print out this line of command, and then execute the command
    • (3) If you do not want to see the command itself, point to see Command execution silently execution
Several variable assignment operators in 2.2.8.3 and makefile
    • (1) = simplest assignment
    • (2): = usually also assigned value

These two are the same in most cases, but sometimes they are different.

Use = Assign a variable, when parsed, his value depends on the value of the last assignment, so you see the value of the variable reference can not look forward, you should look back.

Use: = To assign value, is in-situ direct analysis, only with the forward look.

    • (3)? =

This assignment is performed if the variable is not previously assigned, and is ignored if it has been assigned previously. (Experiments can be seen: the so-called has not been assigned to the fact that this variable is not defined)

    • (4) + =

Used to assign a value to an already assigned variable, meaning to add this value to the original value, a bit similar to strcat. (in the shell makefile and other files, you can think of all variables are strings, + = is equivalent to the string strcat continuation content) (note that the details, + = To continue the content and the original content will be automatically separated by a space)

Note: It is not necessary to have spaces or no spaces on either side of the assignment operator in Makefile, which is a bit looser than the shell format requirement.

Environment variables of 2.2.8.4 and makefile
    • (1) Export to makefile is the environment variable. In general, the environment variable name is required to be larger, other common variable names are lowercase.
    • (2) The environment variable is different from the ordinary variable, it can be understood that the environment variable is similar to the global variable that can be shared among all makefile in the whole project, while the common variable knowledge is the local variable used in this makefile. So be aware that defining an environment variable affects other makefile files in your project, so be careful
    • (3) There are some environment variables in makefile that may be internal environment variables defined by makefile itself (for example, when we give makefile parameters when make executes). CC=ARM-LINUX-GCC, in fact, is expensive the current makefile passed an environment variable cc, the value is ARM-LINUX-GCC. We have the highest priority for the environment variable value in make is for makefile, which can override the assignment in makefile. This is like the compiler's predefined macro __line__ __function__, etc. in the C language.
2.2.9, makefile Supplementary study 2
    • Using wildcards in 2.2.9.1, makefile
    • (1) * Several arbitrary characters echo *.c
    • (2)? 1 arbitrary characters echo?. C
    • (3) [] [] The characters in the [] sequence and the outside of the combination of Echo [12].c

There is also a%, also a wildcard, that represents any number of characters, and * very similar, but% is generally used only in the rule description, also known as the regular wildcard.

Automatic variables for 2.2.9.1 and makefile
    • (1) Why use automatic variables. In some cases there are many files in the collection of files, the description of the time is very cumbersome, so we makefile with some special symbols to replace the set of files that meet certain conditions, which forms an automatic variable
    • (2) Meaning of the automatic variable: predefined special meaning symbols
    • (3) Common automatic variables:

[email protected] The target file name of the rule

The dependent file name of the $< rule. If the target of the dependency is defined in a pattern (that is,%), then "$<" will be a series of file sets conforming to the pattern. Mainly, it is a one to take out.

$^ Dependent collection of files

Shell and makefile that complement the foundation

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.