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 execute Shell script in Linux system

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

Share

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

Editor to share with you how the Linux system executes the Shell script, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1) bash script-name or sh script-name this is the method often used when the script file itself does not have executable permissions (that is, the file permission attribute x bit is the-sign), or when the interpreter is not specified at the beginning of the script file. This method is recommended.

(2) path/script-name or. / script-name means to execute the script under the current path (the script requires permission to execute), and you need to change the permission of the script file to executable (that is, the file permission attribute is x bit). The specific method is: chmod aquix script-name. The script can then be executed by executing either the absolute path or the relative path. Note: in a production environment, an error occurs when the operator forgets to set the executable permission for the script and then uses it directly. Therefore, the first bash script-name is recommended.

(3) source script-name or. Script-namesource or "." The function of the command is to read and execute the script, that is, to execute source or "." in the current Shell. Loads and executes the commands and statements of the related script file, rather than generating a child Shell to execute the commands in the file.

Note: this is the biggest difference from several other ways of performing shell.

Take Chestnut for example: create a new file test_sh.sh without giving it any executable permissions x.

The editing content is as follows:

The file and the contents of the file differ from the common shell in that the file here does not have executable permissions and does not add #! / bin/bash to the first line of the file content. Let's now test the effect in several ways described above.

The second method. / script-name, the execution effect is as follows:

Report permission denied, Permission denied. At this point, we just need to change the attribute of the file to executable.

Execute the first bash script-name, and the effect is as follows:

Can be executed successfully, output: hello. However, we enter the command: echo $name and find the following:

The value of name is empty. It is not difficult to understand that bash script-name produces a child process shell, and our current operation is still in the parent shell, so we cannot get the value of this variable.

Implement the third method. Test_sh.sh, the effect is as follows:

The value of the variable can be successfully output. This is because source script-name and. Script-name loads the contents of script-name directly into the current shell, so you can output the value of the current variable.

Just now we failed to execute the second method, and now we use chmod aquix script-name, and then execute the above command again to see the effect:

Note: this approach also produces a child process Shell, so echo $name still cannot find the variable.

Additional knowledge points:

A standard Shell script indicates which program (interpreter) executes the contents of the script on the first line, which is typically programmed in Linux bash:

#! / bin/bash

Or

#! / bin/sh

Note:

(1) in Shell, if the first letter of a line is #, it is a comment, but the above two are written on the first line, so it is not a script comment line, and if written after a command, it becomes a comment line.

(2) sh is a bash soft link. In most cases, there is no difference between "#! / bin/bash" and "#! / bin/sh" at the beginning of the script, but a more standardized way of writing is to use "#! / bin/bash" at the beginning of the script.

The above is all the contents of the article "how the Linux system executes the Shell script". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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