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 get path operation by Shell

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

Share

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

This article will explain how to obtain the path operation of Shell for you in detail. 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.

Pwd usage

Pwd: pwd [- LP]

Print the name of the current working directory.

Options:

-L print the value of $PWD if it names the current working directory

-P print the physical directory, without any symbolic links

Pwd:

Print out the current work path, pay attention to the "work path", and the path in which the script is executed, which is the work path of the script, as shown in the figure:

Pwd-L:

Print out the value of the environment variable $PWD. If PWD is assigned to the current work path, pwd defaults to pwd-L.

Pwd-P:

Print the real path, not the path of the link, as shown in the figure:

Basename usage

Examples:

  basename / usr/bin/sort-> "sort"

  basename include/stdio.h .h-> "stdio"

  basename-s .h include/stdio.h-> "stdio"

  basename-an any/str1 any/str2-> "str1" followed by "str2"

Basename:

Print the base file name except the upper path; when there is a suffix after the file name, remove the suffix, such as # basename include/stdio.h. H will only print stdio.

Basename-s:

The suffix character to be removed is specified after the-s parameter, that is, # basename-s .h include/stdio.h prints only stdio like # basename include/stdio.h .h

Basename-a:

The-a parameter can append multiple file paths, take the base file name of each path and print it. The usage is as follows:

Dirname usage

Examples:

  dirname / usr/bin/-> "/ usr"

  dirname dir1/str dir2/str-> "dir1" followed by "dir2"

  dirname stdio.h-> "."

Dirname:

Remove the non-directory part of the file name, delete the path after the last "\", and display the parent directory

Dirname-z:

The output does not wrap

As shown in the figure:

The combined use parameter $0:

In shell, $0 is specified as the 0th parameter of the command line argument, that is, the file name of the current script, and $1 $2 refers to the 1st and 2nd argument of the passed-in script

Dirname and $0:

If you often see $(dirname $0), what is stored in this variable, that is, the parent directory of the current script file? note that $0 is the script path name passed in when the script is executed, as follows:

Generally speaking, an absolute path is used to execute a file in shell, but if a relative path is used, the file must be guaranteed to exist relative to the target path under the current working path, otherwise bash:... / shell/demo.sh: No such file or directory . That is, if you pass the wrong script path, dirname will not be able to get a valid parent directory!

Usually we need to use the path of the current script as the working path to execute some relative path files, so we need to get the absolute path of the parent directory of the currently executed script, and the variable $(cd $(dirname $0); pwd) is used to hold the absolute path of the parent directory of the current script, as shown below:

The procedure for getting the absolute path of the current script parent directory by executing # $(cd $(dirname $0); pwd) is as follows:

This is the end of the article + dirname shell/demo.sh++ cd shell++ pwd+ echo / home/shell/home/shell on "how to get the path operation of Shell". 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 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

Development

Wechat

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

12
Report