[Shell programming] 1. Introduction to shell programming and introduction to shell programming
Shell itself is a program written in C language. It serves as a bridge for users to use Unix/Linux, and most of their work is done through Shell. Shell is both a command language and a programming language. As a command language, it interactively interprets and executes user-input commands. As a programming language, it defines various variables and parameters, it also provides many control structures that are available in advanced languages, including loops and branches.
Although it is not part of the Unix/Linux kernel, it calls most of the core functions of the system to execute programs, create files, and coordinate the running of various programs in parallel. Therefore, shell is the most important utility for users. It is the key to making good use of Unix/Linux systems by thoroughly understanding and mastering shell features.
It can be said that the proficiency of shell reflects the user's proficiency in Unix/Linux.
Shell has two methods to execute commands:
- Interactive: Explain and execute a user's command. If you enter a command, Shell will explain and execute a command.
- Batch: you can write a Shell Script in advance. There are many commands in the Script so that the Shell can finish executing these commands at a time without having to repeat the command one by one.
Shell scripts are similar to programming languages, and there are also variables and process control statements. However, Shell scripts are interpreted and executed without compilation, the Shell program reads and executes these commands from one line in the script, which is equivalent to a user hitting the Shell prompt to execute the command line in the script.
For beginners of Shell, it is recommended that you do not use the root account to run Shell in common applications. As a common user, you can't damage the system either intentionally or unintentionally. But if it is root, it is different. If you press a few letters, it may lead to disastrous consequences.
Common Shell script interpreters in Unix/Linux include bash, sh, csh, and ksh. They are used to being called a Shell. We often talk about the number of Shell types. In fact, it refers to the Shell script interpreter.
Bash
Bash is the default shell for Linux. This tutorial is based on bash. Bash is jointly completed by Brian Fox and Chet Ramey. It is short for the BourneAgain Shell and has a total of 40 internal commands.
Linux uses it as the default shell because it has the following features:
- You can use a function similar to doskey under DOS to view and quickly enter and modify commands with the direction keys.
- The command that starts with a string is automatically provided by searching for matching.
- It includes its own help function. You only need to enter help at the prompt to get help.
Sh
Sh is developed by Steve Bourne and is short for the Bourne Shell. sh is the default shell of the Unix standard.
Ash
Ash shell is compiled by kenth almsource. A small shell that occupies the least system resources in Linux contains only 24 internal commands, which makes it inconvenient to use.
Csh
Csh is a large Linux kernel. It is compiled by a total of 47 authors represented by William Joy and contains 52 Internal commands. This shell actually points to a shell like/bin/tcsh, that is, csh is actually tcsh.
Ksh
Ksh is short for the Korn shell. It is written by Eric Gisin and contains 42 Internal commands. The biggest advantage of this shell is that it is almost completely compatible with the commercial release ksh, so that you can try the performance of the commercial version without spending money to buy the commercial version.
Note: bash is the abbreviation of Bourne Again Shell, which is the default shell of linux standard. It is based on the Bourne shell and absorbs some features of C shell and Korn shell. Bash is fully compatible with sh. That is to say, scripts written with sh can be executed in bash without modification.
Compiled language
Many traditional programming languages, such as Fortran, Ada, Pascal, C, C ++, and Java, are compiled languages. This type of language needs to convert the source code we have written into the object code in advance. This process is called "Compilation ".
When running the program, you can directly read the target code (object code ). Because the compiled object code is very close to the bottom layer of the computer, the execution efficiency is very high, which is an advantage of compiled languages.
However, since most compile-type languages operate at the underlying layer, they process objects at the byte, integer, floating point, or other machine level. A simple function requires a lot of complicated code. For example, in C ++, it is difficult to perform simple operations such as "copying all files in one directory to another.
Interpreted language
Interpreted language is also called "script language ". When executing such programs, the interpreter (interpreter) needs to read the source code written by us, convert it into the target code (object code), and then run it on the computer. Because every execution of the program has a compilation process, the efficiency is reduced.
The advantage of using the scripting language is that most of them run at a higher level than the compiled language and can easily process objects such as files and directories; the disadvantage is that they are generally less efficient than compiled languages. However, in balance, it is worthwhile to use Script Programming: A simple script written in an hour. It may take two days to write and implement the same function in C or C ++, in general, the speed of script execution is fast enough to let people ignore its performance problems. Examples of scripting languages include awk, Perl, Python, Ruby, and Shell.
The Shell script is based on:
- Simplicity: Shell is a high-level language, through which you can easily express complex operations.
- Portability: With the features defined by POSIX, scripts can be executed on different systems without modification.
- Easy to develop: You can complete a powerful and usable script in a short time.