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 is the format and operation of the Shell script?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, Xiaobian will bring you about the format of Shell scripts and how they run. The article is rich in content and analyzes and narrates them from a professional perspective. After reading this article, I hope you can gain something.

Did you find home? The echo command can be used directly in Shell scripts. In fact, not only echo command, all Linux commands can be directly invoked in Shell scripts. This feature makes Shell scripts more tightly integrated with Linux systems and makes it easier to write Shell scripts. Let me explain the structure of this script.

Basic structure of Shell

The first line of shell basic structure "#!/ bin/bash"In Linux, comments that begin with a"#"are generally comments, but this sentence is an exception. The effect of this sentence is that the script I write below uses Bash syntax, and any Shell script based on Bash should start like this. This is like embedding PHP programs in HTML, PHP programs must be included with. However, there are some people who prefer to study will also have doubts, they write Shell scripts, do not add "#!/ Bin/bash", Shell scripts can also execute correctly.

What if the default environment for the script is not Bash?

That's because we wrote the script in Linux, where the default Shell is Bash, and the script is pure Bash script to execute correctly. If the script is run in an environment where the default environment is not Bash, or if the script is not written in pure Bash, but embedded in another language (such as Tcl), then the script will not execute correctly.

So, remember that all our Shell scripts must start with "#!/ bin/bash"begins.

Second line: comments in Shell scripts except "#!/ All lines except bin/bash that begin with a #are comments. The second line is our script comments, we recommend that you write programs to include clear and detailed comments, these are good programming specifications should pay attention to the problem.

Line 3: The body of the program Since the echo command can print "c.biancheng.net" directly, it is also possible to put this sentence into the Shell script correctly, because Linux commands can be executed directly in the script. The Shell script is written, so how should this script run?

In Linux, there are two main ways to run scripts: to grant execution permissions, directly run this method is the most commonly used Shell script running method, but also the most direct and simple. That is, after giving execution permission, it runs directly. Of course, run-time can use absolute or relative paths.

Bash is used directly to interpret the content of the script, so the script can also work properly. To run scripts this way, you don't even need "execute" permissions on the script file, just "read" permissions to run it. These two Shell scripts run in a way that everyone can use freely according to their personal habits.

The above is what the format and operation mode of the Shell script shared by Xiaobian is. If there is a similar doubt, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

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