search
HomeJavajavaTutorialA brief introduction to shell programming

A brief introduction to shell programming

Jun 26, 2017 am 11:20 AM
shellIntroductionprogramming

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
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool