Introduction to shell programming basics

What is a shell script?

Shell script (English: Shell script) is a computer program and text file, the content consists of a series of shell commands, through the Unix Shell literal translation of its content and then operate. It is designed as a scripting language. It works in the same way as a literal translation. The Unix shell plays the role of a command-line interpreter. After reading the shell script, it runs the shell commands in sequence, and then outputs the result. Use the shell script for system management, file operations, and more.

Introduction to shell programming basics

Shell scripts exist on Unix and all Unix-like systems, such as Linux and FreeBSD. According to the different types of Unix shells, Shell scripts also have different dialects. Batch files in DOS, OS/2, and Microsoft Windows have similar functionality to shell scripts.

1) Record command history

The command we have typed, linux will have a record, the preset can record 1000 historical commands. These commands are saved in the .bash_history file in the user's home directory. One thing you need to know is that commands that are run in the current shell will only be saved to the .bash_history file when the user exits the current shell normally.

One interesting character related to the history of the command is "!". There are several applications that are commonly used: (1)! ! (two consecutive!!)), indicating the execution of the previous instruction; (2)! n (where n is a number), indicating the nth instruction in the execution command history, for example, "!100" means the 100th command in the execution command history; (3)! String (string greater than or equal to 1), for example! Ta, indicates the last command in the command history that starts with ta.

Introduction to shell programming basics

2) Instruction and file name completion

At the beginning of this tutorial, the author introduced this feature, remember? Right, just press the tab key, it can help you complete an instruction, or you can complete a path or a file name for you. Press tab twice in succession and the system will list all the commands or file names.

3) Alias

There has also been an introduction to alias in the past, this is one of the features unique to bash. We can alias a commonly used and very long instruction by alias to a simple and easy to remember instruction. If you don't want to use it, you can use unalias to remove the alias function. Directly knocking on alias will see the current system default alias:

Introduction to shell programming basics

See it, the system's default alias command is just a few, you can also customize the instruction alias you want. The alias syntax is very simple, alias [command alias] = ['specific command'].

4) Wildcards

Under bash, you can use * to match zero or more characters, and use? Match one character.

Introduction to shell programming basics

5) Input and output from orientation

Input redirection is used to change the input of the command, and output redirection is used to change the output of the command. Output redirection is more common, and it is often used to enter the results of a command into a file instead of on the screen. The command to enter the redirect is ", the output redirect command is", there is also error redirect 2", and additional redirects", which will be described in detail later.

6) Pipe character

I have already mentioned the pipe character "|", which is to throw the result of the previous command run to the following command.

7) Job control.

When running a process, you can make it pause (Ctrl+z), then use the fg command to restore it, use the bg command to get it to run in the background, or you can terminate it (Ctrl+c).

【variable】

In the previous chapter, I have introduced the environment variable PATH. This environment variable is a variable preset by the shell. Usually, the variables preset by the shell are uppercase. Variables, to put it simply, use a simpler string instead of some special settings and data. Take the PATH, this PATH replaces the absolute path setting of all commonly used commands. Because we have the PATH variable, we will not enter the global path when we run a command, just type the command name. You can use the echo command to display the value of a variable.

Introduction to shell programming basics

In addition to PATH, HOME, LOGNAME, what are the system default environment variables?

Introduction to shell programming basics

Use the env command to list all system variables preset by the system. However, the values ​​of these environment variables are different for users who log in. Currently showing the environment variable of the root account. The author briefly introduces the common environment variables:

PATH determines which directories the shell will look for in commands or programs.

HOME current user home directory

HISTSIZE history

LOGNAME The current user's login name

HOSTNAME refers to the name of the host

SHELL front user shell type

LANG language-related environment variables, multi-language can modify this environment variable

MAIL current user's mail storage directory

PWD Current Catalog

The variables displayed by the env command are only environment variables. There are actually a lot of variables preset by the system. You can use the set command to display all the variables preset by the system.

Introduction to shell programming basics

Due to space limitations, the author did not take a screenshot of all the results in the above example. The set not only displays the variables preset by the system, but also displays them along with user-defined variables. User-defined variables? Yes, the user can define variables as well.

Arts And Crafts Casting

Arts And Crafts Casting,Hpdc Die Casting,Aluminum Die Casting Air,Die Casting Aluminium Alloys

Dongguan Metalwork Technology Co., LTD. , https://www.diecast-pro.com