Understanding of Shell Scripting

batch processing

Batch ( Batch), also known as batch script . As the name implies , batch processing is the batch processing of an object, which is usually regarded as a simplified scripting language , which is used in DOS and Windows systems. Batch files have the extension bat  . There are two types of batch processing that are more common at present: DOS batch processing and PS batch processing . PS batch processing is based on the powerful picture editing software Photoshop , which is used to batch process pictures ; while DOS batch processing is based on DOS commands, and is used to automatically execute DOS commands in batches to achieve specific operations.

For more complex situations, you need to use commands such as if, for, goto to control the running process of the program, just like high-level languages ​​such as C and Basic . If more complex applications need to be implemented, it is necessary to use external programs, including external commands provided by the system itself and tools or software provided by third parties. Although batch programs run in the command line environment, not only command line software can be used, but any programs that can be run under the current system can be run in batch files.

Some people think that the meaning of batch language is broader than the above description, and also includes the functions of batch language built in many software, such as  Microsoft Office , Visual Studio , Adobe Photoshop  . The corresponding software performs automated actions (such as resizing all PSD files in a folder). And most of these batch languages ​​also provide the function of recording a series of operations as batch files, so that users can get batch programs without writing programs.

DOS command

DOS command, computer terminology, refers to the command of the DOS operating system, which is a kind of disk-oriented operation command , mainly including directory operation commands, disk operation commands, file operation commands and other commands.

Shell script

Shell Script, Shell script is similar to batch processing under Windows/Dos, that is, a program file that is pre-loaded into a file with various commands , which is convenient for one-time execution , mainly for administrators to set or manage. But it is more powerful than batch processing under Windows, more efficient than programs edited with other programming programs , and it uses commands under Linux/Unix.

In other words, a shell script is a program written by using the functions of the shell. This program uses a plain text file , writes some shell syntax and instructions in it, and then uses regular notation, pipe commands and data streams. Redirection and other functions to achieve the processing purpose we want.

To put it more clearly, shell script is like .bat in the early dos era . The simplest function is to assemble many commands together, so that users can easily execute multiple commands in one operation , and shell script is more Provides important functions such as arrays , loops, conditions, and logical judgments , allowing users to write programs directly in the shell without using the syntax of traditional programming such as C programming languages.

What is the difference between shell and shell scripting

To be precise, Shell is a command line interpreter , its role is to follow a certain syntax to interpret the input command and pass it to the system. It provides users with an interface system-level program that sends requests to Linux to run programs. Users can use the Shell to start, suspend, stop, and even write some programs. Shell itself is a program written in C language, which is a bridge for users to use Linux. Shell is both a command language and a programming language (what you call shell scripting). 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 only found in high-level languages, including loops and branches. Although it is not part of the Linux system kernel , it calls most of the functions of the system kernel to execute programs, create documents and coordinate the execution of various programs in a parallel manner.

example

Open a text editor (you can use the vi/vim command to create a file), create a new file test.sh with the extension sh (sh stands for shell), the extension does not affect the execution of the script, it is good to know the name, if you Use php to write shell scripts, and use php for the extension.

Enter some code, the first line is generally like this: 

#!/bin/bash

echo "Hello World !"

The echo command is used to output text to the window. "#!" is a convention marker that tells the system what interpreter this script needs to execute , i.e. what kind of shell to use. ( The common shell script interpreters on Unix/Linux are bash, sh, csh, ksh, etc., and they are habitually called a kind of shell. We often talk about how many kinds of shells there are, but in fact we are talking about shell script interpreters. )

Bash is widely used in daily work due to its ease of use and free

  • Bourne Shell(/usr/bin/sh或/bin/sh)
  • Bourne Again Shell(/bin/bash)
  • C Shell(/usr/bin/csh)
  • K Shell(/usr/bin/ksh)
  • Shell for Root(/sbin/sh)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325305283&siteId=291194637