The basics of using the shell under Linux

  

What is the meaning of the shellshell is "shell", in fact, has been a very vivid illustration of the role of the shell in the Linux system. The shell is a "shell" program outside the Linux kernel. All tasks performed by the user on the operating system
are implemented through the interaction of the shell with the Linux kernel. We should be familiar with the command.com program in the DOS system. The function of the shell is similar, but the shell is more powerful and more usable.

All operating systems have their own shells. Take DOS as an example. Its shell is the command.com program. There are also many third-party command interpreters under DOS, such as 4DOS, NDOS, etc. These command interpreters can completely replace the standard command.com program. Similarly, in addition to the default Bourne again shell (bash) under Linux, there are many other shells, such as C shell (csh), Korn shell (ksh), Bourne shell (sh), and Tenex C shell (tcsh). Each version of the shell function is basically the same, but each has its own merits. Now Linux distributions generally use bash as the default shell.

The shell itself is a program written in C language, which is the bridge between the user and the operating system kernel. The shell is both a command interpreter and a powerful interpreted programming language. As a command interpreter, the shell interprets the commands entered by the user, submits them to the kernel for processing, and finally returns the results to the user. In order to speed up the operation of the command and customize the shell program more effectively, the shell defines some built-in commands. Generally, the commands that the shell itself interprets and executes are called built-in commands. For example, we will talk about cd, pwd, exit and Echo and other commands are built-in commands that belong to bash. When the user logs in to the system, the shell and built-in commands are loaded into memory by the system and run until the user exits the system. In addition to built-in commands, there are many executable files on Linux systems. Executable files are similar to .exe files under Windows
, and these executables can also be executed as shell commands. In fact, many commands on Linux are not built-in commands of the shell. For example, ls is an executable file and is stored in /bin/ls. These commands are different from the shell built-in commands and are only loaded by the system into memory when they are called.

After logging in to the system, if the login character interface is displayed, a shell command prompt will appear. “#” indicates that the logged in user is the system super user, “*” indicates that the logged in to the system is a normal user. The specific process of the shell to execute the command interpretation is as follows: after the user inputs the command on the command line, the shell program first detects whether it is a built-in command, and if so, interprets the command as a system call through the shell's internal interpreter, and then submits it to the kernel for execution; If it is not a shell built-in command, the shell will look for the corresponding command on the hard disk according to the path given by the user or according to the configuration information of the system environment variable, then transfer it to the memory, and finally interpret it as a system call and submit it to the kernel. carried out.

The final shell is also a powerful interpreted programming language that defines various options and variables, supporting almost all program structures of high-level programming languages, such as variables, functions, expressions, and loops. Shell can be used to write shell scripts, similar to batch files under Windows/DOS, but the shell function is more complete and more powerful.

4.1.2 Syntax Analysis of Shell Commands The main difference between various shells under Linux is the syntax of the command line. For some common commands, the syntax of each shell version is basically the same. Only when writing a shell script or using some advanced features of the shell, the differences of each version of the shell will be displayed.

Shell parsing refers to the shell's scanning process for commands, that is, the operation of decomposing commands or user input into various parts to be processed. Under Linux, shell parsing contains a lot of content, such as redirection, file name extensions, and pipes.

In this section, we use bash as an example to introduce the syntax analysis of the following shell commands.

1. Shell command format After the user logs in to the system, the shell command line starts. The shell follows a certain syntax to analyze and interpret the commands entered by the user and pass them to the system kernel. The general format of the shell command is:

command [options] [arguments]

According to the habit, we generally refer to a string with the above format as the command line. The command line is the basic unit of dialogue between the user and the shell.

— command: indicates the name of the command.

— options: indicates the options for the command.

— arguments: indicates the parameters of the command.

In the command line, an option is a code that contains one or more letters, mainly used to change the way the command is executed. There is usually a “-” symbol in front of the option to distinguish parameters. For example:

[root@WEBServer ~]#ls -a

The ls command adds the -a option to list all files in the current directory (including hidden files). If ls does not add the “-a” option, only the file name and directory in the current directory are displayed (the hidden file is not displayed).

General commands have a number of options, which can be listed separately, or you can list the required options after “-”, for example,

ls –a – l

can also be written as:

ls –al

A lot of commands can accept parameters. A parameter is one or more strings that follow the option. These strings specify the action object of the command, such as a file or directory. For example, to display all the files and information in the /etc directory, use the following command:

[root@WEBServer ~]#ls –al /etc

In some cases, some commands can Without parameters, such as the ls command, and some must take parameters. When the parameters are not enough, the shell will give an error. For example, the mv command requires at least two parameters.

[root@WEBServer ~]#mv mylinux1.txt mylinux.txt

In a command line of the shell, you can also enter multiple commands and separate each command with a semicolon, for example :

[root@WEBServer ~]#ls –al;cp mylinux1.txt mylinux2.txt

Instead, you can also enter a command in multiple lines, using “\\” A command continues to the next line:

[root@WEBServer ~]#cp –i \\

>mylinux1.txt \\

> mylinux2.txt

2. The wildcard wildcards of the shell are mainly for the convenience of the user to describe the file or directory. For example, when the user only needs to end the file with “.sh”, the wildcard can be conveniently implemented. Each version of the shell has a wildcard. These wildcards are special characters that can be used by the user to match file names or path names in the command line arguments. The shell will take all file names or path names that match the matching rules specified on the command line as arguments to the command and then execute this command.

The wildcards commonly used in bash are “*”,“?”,“[]”.

(1)“*”——match any one or more characters

Example:

[root@WEBServer ~]#ls *.txt

This command lists all files ending in “.txt” in the current directory (except for files starting with “.”).

[root@WEBServer ~]#cp doc/* /opt

Copyright © Windows knowledge All Rights Reserved