Linux Shell script series tutorial (1): getting started with Shell
This article mainly introduces the Linux Shell script series (1): getting started with Shell, this article describes the basic knowledge of Shell, basic Shell operations, how to open Shell terminals, the concept of Shell scripts, how to run Shell scripts, and how to annotate Shell scripts. For more information, see
I. Introduction to Shell
The Design of many Unix-like operating systems is amazing. Even decades later, the Unix-style operating system architecture is still 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 enables users to interact with the core functions of the operating system. This environment is more involved in the term script. Writing scripts usually uses an interpreter-based programming language. Shell scripts are essentially some text files. We can write a series of commands to be executed and execute them through shell.
Here we will introduce Bash shell (Bourne Again Shell), which is the default shell environment for most GUN/Linux systems. All the experiments in this book are completed in Ubuntu14.04 LTS environment.
Ii. Basic operations
1. Open the terminal
In ubuntu14.04 LTS, a Terminal has been installed by default. You can open the Terminal in multiple ways. Here we will introduce two types:
Method 1: with the built-in retrieval system, we can easily find the Terminal and click it to open it. The search system can be started by clicking the button in the upper-right corner of the Quick Start bar.
Method 2: To enable the terminal conveniently, we recommend that you fix the terminal in the Quick Start column. Method 1: After you open the terminal, a terminal chart appears in the Quick Start column. Right-click the chart and choose, select "fixed in the startup bar" to pin the terminal to the Quick Start bar.
2. Terminal initialization Interface
By default, the terminal prompt is username @ hostname or root @ hostname #. Represents a common user, and # represents a root user.
For example, after I open the terminal, the prompt is: wxb @ ubuntu :~ $.
Root is the user with the highest permissions in linux and has a high capability risk. Therefore, you don't mind using root users as the default user to log on to the system.
3. Switch users
Generally, for a linux operating system of a Personal Edition, there are two users, namely the user himself and the root user. For users, switching users is required to perform operations that common users cannot perform. Here, two methods are provided to switch users.
Method 1: Temporary switchover. As the name suggests, this switching method is only temporary. After the command execution is complete, it will switch to the original user. The switch command is sudo command, which is short for super user do.
Method 2: Perform failover for a long time. As the name suggests, after the command is switched, no common user is returned after the command is executed. The switch command is su, and su is short for switch user. A prompt is displayed, such as entering the password to complete switching.
4. Shell script
The following script is used to print Hello world! String.
The Code is as follows:
#! /Bin/bash
Echo "Hello World! "
The starting line of the shell script is usually #! /Bin/bash, where/bin/bash is the interpreter path, used to explain and execute subsequent commands. Commands are separated by line breaks or semicolons.
5. Run the script
In Ubuntu, there are many ways to run scripts.
Method 1: bash test. sh. In this case, the first line of the script file does not have to be "#! /Bin/bash ", because the interpreter has been specified in this method.
Method 2: First modify the permission of the script file chmod a + x test. sh. This command is mainly used to grant the executable permission to the script file. Then execute the file./test. sh; or you can execute the script through the full path.
6. Script Annotation
It is no exception in shell scripts. We need to provide annotation lines in some locations, and similar code is easy to understand. # The following content is a comment and will not be interpreted and executed. Note: # is a single line annotator.