Home  >  Article  >  Java  >  A brief introduction to shell programming

A brief introduction to shell programming

巴扎黑
巴扎黑Original
2017-06-26 11:20:381290browse

Shell itself is a program written in C language. It is a bridge for users to use Unix/Linux. Most of the user's work is completed through Shell. Shell is both a command language and a programming language. As a command language, it interactively interprets and executes commands entered by the user; as a programming language, it defines various variables and parameters, and provides many control structures found only in high-level languages, including loops and branches.

Although it is not part of the Unix/Linux system kernel, it calls most of the functions of the system core to execute programs, create files, and coordinate the running of various programs in a parallel manner. Therefore, for users, the shell is the most important utility program. In-depth understanding and proficiency in the characteristics and usage of the shell are the keys to making good use of the Unix/Linux system.

It can be said that the proficiency of using shell reflects the user's proficiency in using Unix/Linux.

Shell has two ways to execute commands:

  • Interactive (Interactive): interprets and executes the user's command, the user enters a command, Shell will interpret and execute one.

  • Batch processing (Batch): The user writes a Shell script (Script) in advance, which contains many commands, so that the Shell can execute these commands at once without having to type the commands one by one. .


Shell scripts are very similar to programming languages. They also have variables and flow control statements, but Shell scripts are interpreted and executed and do not need to be compiled. The Shell program reads line by line from the script. Fetching and executing these commands is equivalent to a user typing the commands in the script line by line into the Shell prompt for execution.

Shell beginners please note that in daily applications, it is recommended not to use the root account to run Shell. As an ordinary user, you cannot damage the system whether intentionally or unintentionally; but if you are root, it is different. Just typing a few letters may lead to catastrophic consequences.

Common shell script interpreters on Unix/Linux include bash, sh, csh, ksh, etc. It is customary to call them a kind of Shell. We often talk about how many kinds of Shell there are, but what we are actually talking about is the Shell script interpreter.

bash

Bash is the standard default shell of Linux. This tutorial is also based on bash. bash was jointly completed by Brian Fox and Chet Ramey. It is the abbreviation of 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 functions similar to doskey under DOS, use the arrow keys to browse and quickly Enter and modify commands.

  • Automatically give commands starting with a certain string by searching for matches.

  • contains its own help function. You only need to type help at the prompt to get relevant help.

sh

sh Developed by Steve Bourne, it is the abbreviation of Bourne Shell. sh is the standard default shell of Unix.

ash

ash shell was written by Kenneth Almquist. It is a small shell that takes up the least system resources in Linux. It only contains 24 internal commands, so it is very inconvenient to use.

csh

csh is a relatively large kernel of Linux. It is compiled by a total of 47 authors represented by William Joy and has a total of 52 internal commands. This shell actually points to a shell such as /bin/tcsh. In other words, csh is actually tcsh.

ksh

ksh is the abbreviation of Korn shell, written by Eric Gisin, with a total of 42 internal commands. The biggest advantage of this shell is that it is almost completely compatible with the commercial version of ksh, so 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 standard default shell of Linux. It is based on Bourne shell and absorbs some features of C shell and Korn shell. bash is fully compatible with sh, that is to say, scripts written in 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 wrote into object code (object code) in advance. This process is called "compilation".

When running the program, read the object code directly. Since the compiled object code is very close to the bottom layer of the computer, the execution efficiency is very high, which is the advantage of compiled languages.

However, since most compiled languages ​​operate at the bottom level and deal with bytes, integers, floating point numbers or other machine-level objects, implementing a simple function often requires a large amount of complex code. For example, in C++, it is difficult to perform simple operations such as "copy all files in one directory to another directory".

Interpreted Language

Interpreted language is also called "scripting language". When executing this type of program, the interpreter needs to read the source code we wrote and convert it into object code, which is then run by the computer. Because each time the program is executed, there is an extra compilation process, so the efficiency is reduced.

The advantage of using scripting programming languages ​​is that they mostly run at a higher level than compiled languages ​​and can easily handle objects such as files and directories; the disadvantage is that they are usually not as efficient as compiled languages. However, the trade-off is that it is usually worthwhile to use script programming: a simple script that takes an hour to write may take two days to implement the same function in C or C++, and generally speaking, the script execution speed is fast enough. , fast enough for people to ignore its performance problems. Examples of scripting programming languages ​​include awk, Perl, Python, Ruby, and Shell.

To use Shell scripts is based on:

  • Simplicity: Shell is a high-level language; through it, you can express complex operations concisely.

  • Portability: Using the functions defined by POSIX, the script can be executed on different systems without modification.

  • Easy to develop: a powerful and useful script can be completed in a short time.

The above is the detailed content of A brief introduction to shell programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn