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

What are the entry-level knowledge points of shell

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

Share

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

This article mainly explains "what are the basic knowledge points of shell". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the basic knowledge points of shell".

Background

To sum up: shell programming is the logical processing of a bunch of Linux commands. To take a simple example, we did javaweb development. In the past, if we wanted to package the program locally and then deploy it to a remote server, we used to go through the following steps:

1 get the latest code (git pull)

2 compilation and packaging

Uploading and deploying to a remote server goes through such a stage every time it is packaged, which is inefficient and annoying. Faced with this situation, we can write a shell script, each time only need to run the shell script, we can achieve packaging and deployment of this series of actions, completely free hands, how beautiful.

Getting started #! / bin/bash# 's first shell Mini Program echo hello world!

What a classic helllo world.

The first line indicates that we chose to use bash shell

The second line begins with the # symbol, indicating that the line is a comment and will not be run at run time.

The echo in the third line is the output command in linux, which means to output hello world!

The # symbol in shell indicates a comment. The first line of shell is special, usually with #! Start by specifying the type of shell to use. In linux, besides bash shell, there are many versions of shell, such as zsh, dash and so on. But bash shell is still the one we use the most.

Run the first shell program chmod + x hello_world.sh./hello_world.sh

In linux, suffix names are almost arbitrary or have no suffix names, and shell is generally saved as XXX.sh in order to look more intuitive. If we execute hello_world.sh directly, we will default to look for it in the $PATH environment variable, but we will not find it because we want to configure this file in the environment variable. So, we used "." This symbol means to find it from the current directory. In addition to the above execution method, we can also directly specify shell to run:

/ bin/sh hello_world.sh

Here we specify that / bin/sh is used to execute, and the / bin/bash specified in hello_world.sh will not take effect.

Variable

Shell programming is divided into two kinds of variables, the first is our own defined variables (custom variables), the second is Linux defined environment variables (environment variables, such as: HOME, etc.). We can use such variables directly.

#! / bin/bash# uses the environment variable echo $PATH# custom variable hellohello= "hello world" echo $hello

The above demonstrates the use of custom variables and system environment variables, which is simple to use by using symbols plus variable names. Remember: you don't need symbols to define variables, just add $to use variables. In line 5, we use double quotation marks when customizing variables. In shell programming, if the variable has spaces or quotation marks, it must also be enclosed in quotation marks, otherwise it can be omitted. It is also important to note that when defining variables, there should be no spaces around "=". Assign the execution result of the linux command to the variable

#! / bin/bashpath=$ (pwd) files= `ls-al`echo current path: $pathecho files: $files

Lines 2 and 3 above demonstrate two ways to save the results of Linux command execution to variables. Line 2 assigns the result of the pwd execution (the current directory) to the path variable. Line 3 assigns the result of the ls-al command (listing all files and folders in the current directory) to the variable.

The symbol on the third line is not a single quotation mark, but the key "~" on the keyboard.

Thank you for your reading, the above is the content of "what are the basic knowledge points of shell". After the study of this article, I believe you have a deeper understanding of what the entry knowledge points of shell have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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