Chapter II Shell Programming
The following sections are described in this chapter:
What is a shell?
Basic ideas
Subtle syntax: variables, conditional judgments, and program control
Command List
Function
Execution of commands and commands
Here documentation
Debugging
grep commands and regular expressions
Find command
Shell executes shell programs, often called scripts, that are interpreted at run time. This makes debugging easier because the instructions can be executed on a row-by-line basis, and the time to recompile is saved. However, this also makes
the shell is not suitable for time-critical and processor-busy tasks.
The UNIX architecture relies heavily on the highly reusable code, and if you write a small and simple tool, others can make it a link on a chain to form a command.
What is a shell?
The shell is a program that acts as an interface between a user and a Linux system, allowing the user to enter commands that need to be executed to the operating system. This is similar to the Windows command prompt, but the Linxu shell is more powerful.
The shell (bash and CSH) and the X Windows system and other programs surround the Linux kernel. In Linux systems, the standard shell that is always installed as a/bin/sh is bash in the GNU toolset. In most Linux distributions, the default shell program/bin/sh is actually a connection to the program/bin/bash.
You can use/bin/bash--version to view the version number of bash
2.4 Piping and redirection
Describes how to redirect the input and output of a LINXU program
REDIRECT output:
$ ls-1 > Lsoutput.txt
This command
Save the output of the LS command to a file Lsoutput.txtIn
The file descriptor 0 represents the standard input for a program, the file descriptor 1 represents the standard output of a program, and the file descriptor 2 represents the standard error output。
> OperatorsREDIRECT the standard output to a file. By default, if the file already exists, its contents will be
cover。
Use
>> OperatorsThe output content
additionalto a file. For example:
$ ps >> lsoutput.txt
This command appends the output of the PS command to the end of a particular file.
If you want to redirect the standard error output, you need to put the redirected file descriptor number in front of the load > operator. Because the file descriptor for the standard error output is 2, use the 2> operator. This method is useful when you need to discard the error message and prevent it from appearing on the screen.
Pipeline
You can use the pipe operator | To connect to a process, unlike MS-DOS, where the process of connecting through a pipeline can run at the same time, and can be reconciled automatically as the data flow passes between them. For example, you can use the sort command to sort the output of a PS command.
If you do not use pipelines, you must perform this task in several steps, as follows:
$ ps > Psout.txt
$ sort Psout.txt > Pssort.out
A more sophisticated solution is
to connect a process with a pipeline, as shown below:
$ ps | sort > Pssort.out
The shell as a programming language
There are two ways to write a shell script, you can enter a series of commands for the shell to execute them interactively, or you can save the commands to a file and then call the file as a program.
Interactive programs
Typing the shell script directly on the command line is a simple and quick way to test a short code snippet.
If you want to find a file containing the string POSIX from a large number of C language source files. Instead of using the grep command to search for strings in each file, and then listing the files that contain the string separately, use the following interactive script to perform the entire operation:
$ for file in *
> Do
> If grep-1 POSIX $file
> Then
> More $file
> Fi
> Done
In this example, the grep command outputs the file it finds that contains the POSIX string, and then the more command displays the contents of the file on the screen, and finally returns the shell prompt. The shell also provides a wildcard extension, wildcard X to match a string, a wildcard character? To match a single character, while the
[Set] allows you to match any single character in a square bracket, [^set] the contents of the opposing brackets are reversed, that is, characters other than the characters in the given character set are matched. Extended curly Braces
{} allows arbitrary string groups to be placed in a collectionIn For example
$ ls My_{finger, toe}s
This command lists the files My_fingers and my_toes, which use the shell to check each file in the current directory.
Experienced Linux users may use another more efficient way to perform this simple operation. Use the command:
$ More ' grep-l POSIX * '
or an order:
$ More $ (grep-l POSIX *)
Additionally, the following command outputs the file name that contains the POSIX string:
$ grep-1 posix* | More
In the above script, the shell uses other commands, such as grep and more, to do the main work. The shell itself simply allows several existing commands to be combined to form a new powerful command.
If you want to run a series of commands every time, it's very troublesome to go through such a process. So just save these commands in a file, which is what we often call shell scripts so that they can be executed at any time.
Create a script
Use VIM to create a file that contains the command and name it first.
In the program
comments start with a # symboland continues until the end of the line. The first line of #!/bin/sh, which is a special kind of comment,
The #! character tells the system that the parameter that is immediately behind it on the same line is the program used to execute this file.。 /bin/sh is the default shell program.
The function of the Exit command is to ensure that the script can return a meaningful exit code. When a program runs interactively, it is seldom necessary to check its exit code, but to invoke the script from another script and see if it succeeds, it is essential to return an appropriate exit code.
In
in shell programming, 0 means success.。
In general, Linux and Unix rarely use file expansion names to determine the type of file. Most pre-installed scripts do not use any file extensions, and the best way to check whether these files are scripted is to use the file command, such as file first or File/bin/bash.
There are two ways to run a script file, and a simpler approach is to call the shell and take the script file name as a parameter, as follows:
$/bin/sh First
This works, but it's even better to just type in the name of the script as you would with other LINXU commands. You can use
chmod command to change the mode of this file so that this file can be executed by all users, as shown below:
$ chmod +x First
You can then use the following command to execute it:
$ first
You may see an error message telling you that the command was not found. This situation is likely to occur because the shell environment variable path is not set to find the command to execute in the current directory. One way is to enter the command directly on the command line path= $PATH:., then log out and log in again. Alternatively, you can enter it in the directory where the script is saved
command./first, the purpose of this command is to tell the shell the full relative path of the script.
Using./To specify a path has another benefit that ensures that another command with the same name as your script file in your system will not be accidentally executed.
Programming for Linux Programming--shell (chapter II)