Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Linux Shell script

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to understand Linux Shell script". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

A brief introduction to Shell

The designs of many kinds of Unix operating systems are 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. The term script is more about this environment. Scripts are usually written in some kind of interpreter-based programming language. Shell scripts are essentially text files in which we can write a series of commands that need to be executed and then execute them through shell.

What we introduce here is Bash shell (Bourne Again Shell), which is the default shell environment for most GUN/Linux systems today. All the experiments in the book are done in the Ubuntu14.04 LTS environment.

2. Basic operation

1. Open the terminal

In the ubuntu14.04 LTS system, a Terminal is installed by default, and we can open the terminal in a variety of ways. Here are two kinds:

Method 1: through the retrieval system that comes with the system, we can easily find the terminal (Terminal) and click to open it. The retrieval system can be started by the button in the upper right corner of the Quick launch bar.

Method 2: in order to open the terminal conveniently later, it is recommended that the terminal be fixed in the quick start bar. The method of operation is as follows: after opening the terminal, a chart of the terminal will appear in the quick launch bar, right-click on the chart and select "fixed in the startup bar" to fix the terminal in the quick launch bar.

two。 Terminal initialization interface

By default, the terminal prompt is username@hostname or root@hostname#. On behalf of ordinary users and # on behalf of root users

For example, after I open the terminal, the prompt is: wxb@ubuntu:~$.

Root is the most privileged user in the linux system, with large capabilities and high risks, so it doesn't mind using root users as the default users to log in to the system.

3. Switch users

In general, for the personal version of the linux operating system, there are two users on the system, namely, the user himself and the root user. For users, it is sometimes necessary to switch users to perform operations that ordinary users cannot perform, and there are two ways to switch users.

Method 1: switch temporarily. As the name implies, this switching method is only temporary, and when the instruction is executed, it will switch to the original user. The switch instruction is: sudo command, sudo is the abbreviation of super user do.

Method 2: switch for a long time. As the name implies, after switching using this method, the instruction will not be returned to the ordinary user after execution. The switching instruction is: su,su is the abbreviation of switch user, and then you will be prompted to enter a password to complete the user switch.

4.Shell script

The following script is used to print Hello world to the terminal! String.

The code is as follows:

#! / bin/bash

Echo "Hello World!"

The starting line of a shell script is usually #! / bin/bash, where / bin/bash is the path to the interpreter and is used to interpret and execute subsequent commands. Commands are separated by line breaks or semicolons.

5. Run the script

In Ubuntu, there are several ways to run scripts.

Method 1: bash test.sh, in which case the first line of the script file does not have to be "#! / bin/bash", because an interpreter has been specified in this method.

Method 2: first modify the permissions of the script file chmod axix test.sh, this instruction is mainly to give the script file executable permissions. Then execute the file. / test.sh; or you can execute the script through the full path.

6. Script comment

This is no exception in shell scripts, where we need to provide comment lines that are easy to understand. The following content is annotated and will not be interpreted and executed. Note: # is an one-line comment character.

That's all for "how to understand the Linux Shell script". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report