4

GoLang语言filepath.Clean功能在AIX脚本中的实现

 1 year ago
source link: https://mryqu.github.io/post/shell-substitutes-for-filepath.clean-of-golang/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

GoLang语言filepath包Clean函数功能如下:

  1. Replace multiple Separator elements with a single one.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace “/..” by “/” at the beginning of a path, assuming Separator is ‘/’.

1 perl等价功能

File::Spec 模块的canonpath函数与GoLang语言filepath包Clean函数功能基本类似,都不进行文件系统物理检查仅完成路径逻辑清理。
No physical check on the filesystem, but a logical cleanup of a path.

$cpath = File::Spec->canonpath( $path ) ;

Note that this does not collapse x/../y sections into y. This is by design. If /foo on your system is a symlink to /bar/baz, then /foo/../quux is actually /bar/quux, not /quux as a naive ../-removal would give you. If you want to do this kind of processing, you probably want Cwd’s realpath() function to actually traverse the filesystem cleaning up paths like this.

Cwd 模块的realpath函数跟GNU realpath命令相同,会处理符号链接和相对路径。

2 ksh等价实现

GNU realpath和readlink命令可以完成类似功能,但是AIX默认情况下是不会有GNU 命令的。

标题:GoLang语言filepath.Clean功能在AIX脚本中的实现
作者:mryqu
声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK