Q: What is a shell and what is a shell script? The
A:shell is a program written in C that is a bridge between the user and the Linux operating system. Users can either enter command execution and use shell scripting to accomplish more complex operations. It is a general designation of command Language, command interpreter program and programming language. Its role is to follow a certain syntax to interpret the input commands and pass to the system.
the command interpreter : The shell is a command language interpreter that has its own built-in shell command set, and the shell can be invoked by other applications in the system. Commands entered by the user at the prompt are interpreted by the shell before being passed to the Linux core. It reads the user input command from the input device, converts it to a machine-readable code, and executes it.
Command Language : It interactively interprets and executes commands entered by the user. When a normal user logs on successfully, a program called a shell is executed. It is the shell process that provides the command line prompt. Use "$" as a prompt for the average user, and "#" as the prompt for the superuser (root). Once a shell prompt appears, you can type the command name and the parameters required by the command. The shell will execute these commands. If a command takes a long time to run, or produces a large amount of output on the screen, you can interrupt it by pressing CTRL + C from the keyboard (and aborting its execution before the normal end). When the user is ready to end the logon dialog process, you can type the logout command, Exit command, or file Terminator (EOF) (implemented by Ctrl+d) to end the login.
programming language (that is, Shell script) : Another important feature of the shell is that it is itself an interpretive programming language, and the Shell programming language supports most of the program elements that can be seen in high-level languages, such as functions, variables , arrays, and program control structures. Any command you can type at the prompt can be placed in an executable shell program. As a programming language, it defines a variety of variables and parameters, and provides many control structures that are available in higher-order languages, including loops and branches. Although it is not part of the Linux system kernel, it invokes most of the system kernel's functions to execute programs, create documents, and coordinate the operation of each program in a parallel manner. Simply put, a shell script is a file that contains several lines of shell or Linux commands. For a large number of commands that are written and used multiple times, you can save them with a separate file. For future reuse of
The first shell program, which is saved as a mayuan.sh file (usually a shell script with the. sh suffix):
#! : This is a convention tag that tells the system what interpreter the script needs to execute, even with which shell. such as: #!/bin/sh,#!/bin/bash,#!/bin/csh,#!/bin/tcsh, #!/bin/ksh and so on.
The second line begins with a # and represents the comment line, which is ignored by the interpreter. In addition to the first line, the other lines begin with a comment line with a #; there is no multiline comment in sh, only one # for each line. What if a large segment of code needs to be temporarily annotated during the development process, and then the annotation is canceled after a while? Each line with a # symbol is too laborious, you can put this paragraph to annotate the code in a pair of curly braces, defined as a function, no place to call the function, the code will not be executed, to achieve the same effect as the annotation.
The third line defines a variable name, which is: Mayaun. Note the variable name (name) and = cannot have spaces between them, otherwise the command not found is prompted when the script is executed.
Line four echo: The command is used to output text to the window. The $ (dollar sign) represents a variable substitution, which replaces the variable with the value of the variable that is specified later.
There are two ways to execute a shell script:
1. As an executable procedure
Save the code as mayuan.sh and CD to the appropriate directory:
chmod +x./mayuan.sh #使脚本具有执行权限
./mayuan.sh #执行脚本
Note that you must write./mayuan.sh, rather than mayuan.sh, run other binary programs as well, directly written mayuan.sh,linux system will go to path to find there is no call mayuan.sh, and only/bin,/sbin,/usr/ Bin,/usr/sbin wait in path, your current directory is usually not in path, so write mayuan.sh will not find the command, to use. /mayuan.sh told the system that it was looking in the current directory. As shown in the following illustration:
2, as an interpreter parameter
This is run by running the interpreter directly, whose argument is the file name of the shell script, such as:
/bin/bash mayuan.sh
The script that runs this way does not need to specify the interpreter information in the first line, and it is useless to write it.