Chapter 10 Learning Experience

1. Outline

This chapter discusses sh programming, explaining sh script and different versions of sh; Compare sh script with C program, and point out the difference between interpreting language and compiling language. Details how to write sh script, including sh variable,sh statement, sh build-in commands, general system commands, and command substitution; Explain sh control statements, include test conditions, for loops, while loops, do-until loops, case statements, etc., and demonstrates their usage; Show how to write functions As well as call functions with parameters; Also illustrates the wide application of SH scripts, including installation, initialization and management of Linux systems.
Shell is a program with special functions, which provides an interface for users to interact with the kernel. It receives commands entered by the user and sends them to the kernel for execution. The kernel is the heart of the Linux system and resides in the computer's memory from POST until the computer is shut down, while the user's applications are stored on the computer's hard drive and are loaded into memory only when needed. Shell is an application, when a user logs in to the Linux system, the Shell will be transferred into memory to execute. Shell is independent of the kernel, it is a bridge connecting the kernel and the application, and the command is read by the input device, and then converted into a mechanical code that the computer can understand, so that the Linux kernel can execute the command.
Second, the summary of knowledge points
1. sh script

Shell programming is the same as JavaScript and php programming, as long as there is a text editor that can write code and a script interpreter that can interpret and execute.

There are many types of Linux shells, the common ones are:

Bourne Shell(/usr/bin/sh or/bin/sh)
Bourne Again Shell(/bin/bash)
C Shell(/usr/bin/csh)
K Shell(/usr/bin/ksh)
Shell for Root(/sbin/sh)
First sh script
Open a text editor(can use vi/vim command to create the file),create a new file test.sh,extension is sh(sh represent shell). 
!/bin/bash
echo "Hello World !"
! is a convention tag that tells the system what interpreter this script needs to execute, i.e. which one to use Shell. 
echo Commands are used to output text to a window.

2. sh variable

When defining a variable, the variable name does not add a dollar sign ($, required for variables in the PHP language), such as:
your_name="runoob.com"
Note that there can be no spaces between the variable name and the equals sign, which may be different from all programming languages ​​you are familiar with. At the same time, the naming of variable names must follow the following rules:

·Name can only use English letters, numbers and underscores, the first character cannot start with a number.
·No spaces in between, you can use underscore _.
· Punctuation marks cannot be used.
·Cannot use keywords in bash (reserved keywords can be viewed with the help command).
Examples of valid Shell variable names are:
RUNOOB LD_LIBRARY_PATH _var var2

Use variables:

To use a defined variable, just prefix the variable name with a dollar sign, for example:
your_name="qinjx"
echo $your_name
echo ${your_name}
The curly braces around the variable name are optional and can be added or not. The curly braces are added to help the interpreter identify the boundaries of the variable, such as the following:
for skill in Ada Coffe Action Java; do
echo "I am good at ${skill}Script"
done
If you don't add curly braces to the skill variable and write it as echo "I am good at \(skillScript", the interpreter will treat \)skillScript as a variable (its value is empty), and the code execution result is not what we expect.
Curly braces are recommended for all variables, which is good programming practice.
variable type

When running the shell, three variables exist at the same time:

Local variables Local variables are defined in a script or command, only in the current shell Valid in instance, other shell The started program cannot access local variables.
Environment variables All programs, including shell Started programs can access environment variables, and some programs require environment variables to ensure their normal operation. when necessary shell Scripts can also define environment variables.
shell variable shell variable is made by shell Special variables set by the program. shell Some of the variables are environment variables and some are local variables. These variables ensure that shell of normal operation.

3, sh wildcard

Asterisk wildcard: The most useful wildcard in sh is *, which expands to all files in the current directory.

file :List information about all files in the current directory.
·ls .c:List all files in the current directory starting with.c ending file.
?wildcard:Query characters in a file name
·file ???:All filenames with 3 characters.
·ls ??:a dot.All filenames with 2 characters after.
[]wildcard:Query a pair of filenames[characters in .
·file[ab]:contains characters a or b of all filenames.
·ls[xyz]:list all containing x,y or z filename.
·ls[a-m]*:list contains a arrive m All filenames with characters in the range.
other:
represent『 0 or infinitely many』any character
? represent『there must be one』any character
[ ] also represent『There must be one in brackets』character of(non-arbitrary characters). E.g [abcd] represent『There must be a character, possibly a,b,c,d any of these four』
[-] If there is a minus sign in square brackets, it means『all characters in encoding order』. E.g [0-9] Represents all numbers between 0 and 9, because the language encoding of numbers is continuous!
[^] If the first character in square brackets is an exponent symbol (^) ,that means『Invert Selection』,E.g [^abc] means there must be a character, only if not a,c The other characters are accepted.
Comment notation: This is most often used in script Among them, the regulations are instructions! The following data are not executed. Notes
\ Escape symbol: will『Special characters or wildcards』Restore to normal characters
| pipeline (pipe): Definition of split two pipeline commands
; Delimiter for continuous command release: Definition of continuous command

3. The most rewarding content

The most rewarding thing for me in this chapter is to understand and master the application of primary sh script.

·Save the commands to be executed in sequence to a text file
 Give the file executable permission to run
 Can be combined with various Shell Control statements for more complex operations

The repetitive work of Linux operation and maintenance is all completed through scripts, which improves work efficiency without making mistakes.
4. Problems and solutions

1. Have you studied Python,C,Java and other languages, what are the essential elements and skills of a programming language? How are these elements and skills presented in shell scripting?

Answer: In my opinion, the elements and skills required to learn a programming language are: innovation, carefulness, simplifying problems, more hands-on, more understanding (don't memorize them by rote). Because when we are programming, the same piece of code will have different functions in different environments. We should understand that the usage of the code matches the environment, and choose the appropriate method according to the needs. It is very inefficient to memorize it by memorization. In addition, you can only find your own problems by doing more. There is a big gap between a program in your mind and reality. Don't be a giant in thought and a dwarf in action. There is also the ability to simplify problems when learning a programming language. The code given in the book is easy to understand, but it is not necessarily the most efficient and concise. We need to learn to simplify complex problems when programming, which requires us to innovate on the original basis and use it to explore simpler methods. We should not think that it can be run and it will be over. We should also pay attention to the length and efficiency of the program. exist shell In the script, these elements can be reflected, such as the ability to simplify the problem, shell Many of the application scenarios are doing some repetitive work, sh Scripts make the program more automated, concise and accurate, which reduces labor consumption and saves time and effort.

2. How to shield the output of the command?

linux A device file is created by default/dev/null(empty device), all output to this device will be blocked. By redirecting the output of the command to the device/dev/null,The output of the command can be masked.
Order > /dev/null
 Error output of mask command
 Command 2> /dev/null
 Normal and error output of mask commands
 Order > /dev/null 2> /dev/null
 For example: to shell used in code grep command to find whether a certain keyword exists in a file, but you want the screen grep output of the command.
if grep jack /etc/passwd > /dev/null
then
echo "jack found"
fi
 if /etc/passwd file has jack keyword information, will be displayed jack found,but will not output grep The result of the execution of the command.

5. Practical content

Posted by gmwebs on Sun, 18 Sep 2022 02:19:56 +0930