This article mainly introduces the Linux Shell Script series (i): Introduction to the shell, this article explains the shell introduction, Shell basic operations, how to open the shell terminal, Shell script concept, how to run shell script, shell script comments, and other basic knowledge , the need for friends can refer to the following
First, Shell introduction
The design of many Unix-like operating systems is stunning. Even today, the UNIX-style operating system architecture remains one of the best designs ever. One of the most important features of this architecture is the command line interface or shell. The shell environment allows users to interact with the core functions of the operating system. The term script is more about this environment. Scripting usually uses some kind of interpreter based programming language. The shell script is essentially a text file, and we can write a series of commands that need to be executed and then execute them through the shell.
Here we introduce the Bash shell (Bourne Again Shell), which is the default shell environment for most of the current Gun/linux systems. All the experiments in the book were done under the Ubuntu14.04 LTS environment.
Second, the basic operation
1. Open Terminal
In the ubuntu14.04 LTS system, a terminal has been installed by default, and we can open the terminal in a number of ways. Here are two kinds of:
Method one: Through the system with the retrieval system, we can easily find the terminal (Terminal), click to open. The retrieval system can be started by a button in the upper-right corner of the Quick Launch bar.
Method Two: In order to easily open the terminal, it is recommended to fix the terminal in the Quick Launch bar. The method is: by means of opening a terminal, the Quick Launch bar will show a terminal chart, right-click on the chart, select "Fixed in the boot bar" to fix the terminal in the Quick Launch bar.
2. Terminal Initialization interface
By default, the terminal prompt is: Username@hostname or root@hostname#. On behalf of ordinary users, #代表root用户.
For example: After I open the terminal, the prompt is: wxb@ubuntu:~$.
Root is the most privileged user in the Linux system, and the ability to use the root user as the default user of the login system is not a big risk.
3. Switch users
Typically, for a personal version of the Linux operating system, there will be two users, the user himself and the root user. For the consumer, there are two ways to switch users when there is a need to switch users to perform actions that ordinary users cannot do.
Method One: temporarily switch. As the name suggests, this switching method is only temporary, when the instruction is completed, it will switch to the original user. The switch instruction is: sudo command, sudo is shorthand for super user do.
Method Two: Long-term switching. As the name suggests, after using this method to switch, the instruction does not return to the normal user after the execution completes. The Switch command is: SU,SU is a shorthand for switch user, and then prompts for a password and so on to complete user Switching.
The following script is used to print the Hello world! to the terminal String.
The code is as follows:
echo "Hello world!"
The starting line of the shell script is usually #!/bin/bash, where/bin/bash is the path to the interpreter, which explains the execution of subsequent commands. Each command is separated by a newline character or a semicolon interval.
5. Run the script
In Ubuntu, there are many ways to run scripts.
Method One: Bash test.sh, in which case the first line of the script file may not need to be "#!/bin/bash" because the interpreter has been specified under this method.
Method Two: First modify the permissions of the script file chmod a+x test.sh, this instruction is to give script file executable permissions. Then execute the file./test.sh, or you can execute the script through the full path.
6. Script annotations
In the case of the shell script, we need to provide the comment line in some places, the code is easy to understand. #后面的内容为注释内容, will not be interpreted for execution. Note: #是单行注释符.