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 increase the presentation of custom scripts in Linux

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to increase the display of custom scripts in Linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Principle

Before answering the above question, let's first consider the following question: why do many of the instructions we use do not require the file name of the absolute path?

I'll take the most commonly used ls instruction as an example. The first thing to look at is the location of the instruction's execution file: which ls. The output is: / bin/ls.

Why don't ls instructions need to be called with an absolute pathname?

This is because the environment variable PATH specifies the default retrieval path. So how do you look at the environment variables?

1. Output the built-in variable PATH:echo $PATH

two。 View all variables: env

But sometimes we can call ls instructions with parameters using ll, but we can't find the execution file with which ll. This is the alias of the instruction specified by the alias alias directive.

In fact, in addition to this, we can also add a link file to the path of PATH to point to us.

From this, you can specify the path to the script in the following ways:

1. Add a script path to the PATH variable

two。 Set aliases using the alias directive

3. Add linked files to the path directory in PATH

Actual combat

Prepare a custom script

Create script file: touch mysh.sh

Edit the script file (you can use vi if you don't have vim installed): vim mysh.sh

Press insert to enter edit mode, and the contents of the file are as follows:

#! / bin/bash

Echo "Hello World!"

Press ESC to exit edit mode, enter: wq to save and exit

Grant script file execution permissions: chmod + x mysh.sh

Try to execute the script:. / mysh.sh

Modify the environment variable PATH

Modify the profile file: vim / etc/profile, and add the following at the end of the file: export PATH=$PATH:/root. The / root in the content can be replaced with the directory where the script is located.

Make the modified profile file effective: soruce / etc/profile

Check whether the PATH modification is in effect: echo $PATH

Try to execute the script: mysh.sh

Set alias

Modify the profile file: vim / etc/profile and add the alias setting at the end of the file, using the full path name: the / root in the alias mysh=/root/mysh.sh content can be replaced with the directory where the script is located.

Make the modified profile file effective: soruce / etc/profile

Check if the added alias takes effect: alias mysh

Try to execute the script: mysh

Use linked Fil

Create a link file: ln-s / root/mysh.sh / usr/bin/mysh

Ln is the instruction to create a linked file, and the-s parameter is to create a soft linked file. / root/mysh.sh is the target file, that is, the file that needs to create a soft link. / usr/bin/mysh is the soft link file created.

Execute the script through the soft link file name: mysh

Extraneous remarks

Sometimes when you unpack some tools, when you want to try to run it, either execute the full path name, or jump to the directory where the execution file is located, and then execute the file name.

If you are too lazy to type. /, you can add the following at the bottom of the profile file: export PATH=$PATH:./. Note that this paragraph is on the last line of the file as far as possible.

Summary

At the end, summarize the instructions and tools used below:

1. View file information: ls

two。 Jump to a directory: cd

3. Output instruction: echo

4. Text editor: vim

5. View environment variables: env

6. Check the path where the instruction or tool execution file is located: which

7. Alias instruction: alias

8. Configuration refresh: source

9. File authorization: chmod

10. Set the environment variable: export

11. Create a linked file: ln

This is the end of this article on "how to increase the display of custom scripts in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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

Servers

Wechat

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

12
Report