前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MSYS2下:unix路径和window路径之间的转换

MSYS2下:unix路径和window路径之间的转换

作者头像
10km
发布2019-07-31 14:32:27
2.5K0
发布2019-07-31 14:32:27
举报
文章被收录于专栏:10km的专栏

今天在写MYSYS2下的脚本(bash shell)遇到一个问题:MSYS2环境下获取到的路径都是’/'开头的unix路径,需要把它转为’C:\Windows\system’这样的windows路径。

万能的google给了我答案,找到stackflow上这篇文章: 《msys path conversion (or cygpath for msys?)》

由文中可知,MSYS提供了一个程序cygpath用于unix path和windows path之间的转换,

convert unix path to windows style

使用cygpath转将unix路径转为window路径很简单,使用-w参数将指定的路径转为windows路径,示例如下:

代码语言:javascript
复制
# 当前路径(pwd)转为windows路径
$ cygpath -w $(pwd)
J:\facelog-install\sql

convert windows path to unix style

反过来则是用-u参数,示例如下:

代码语言:javascript
复制
# wp变量保存当前路径(windows风格路径)
$ wp=$(cygpath -w $(pwd))
$ cygpath -u $wp
/j/facelog-install/sql
# 直接将字符串转为 unix路径时,'\'要转义
$ cygpath -u d:\\tmp
/d/tmp

进一步研究cygpath的命令行参数发现cygpath所做的不仅是这些,还可以输出系统路径信息 比如-S显示系统文件夹(system32)

代码语言:javascript
复制
$ cygpath -S
/c/Windows/System32
# 如果要显示windows路径就加上-w
$ cygpath -S -w
C:\Windows\system32

-D显示桌面(Desktop)路径

代码语言:javascript
复制
$ cygpath -D
/c/Users/guyadong/Desktop

下面为cygpath命令行帮助说明:

代码语言:javascript
复制
Usage: cygpath (-d|-m|-u|-w|-t TYPE) [-f FILE] [OPTION]... NAME...
       cygpath [-c HANDLE]
       cygpath [-ADHOPSW]
       cygpath [-F ID]

Convert Unix and Windows format paths, or output system path information

Output type options:

  -d, --dos             print DOS (short) form of NAMEs (C:\PROGRA~1\)
  -m, --mixed           like --windows, but with regular slashes (C:/WINNT)
  -M, --mode            report on mode of file (binmode or textmode)
  -u, --unix            (default) print Unix form of NAMEs (/cygdrive/c/winnt)
  -w, --windows         print Windows form of NAMEs (C:\WINNT)
  -t, --type TYPE       print TYPE form: 'dos', 'mixed', 'unix', or 'windows'

Path conversion options:

  -a, --absolute        output absolute path
  -l, --long-name       print Windows long form of NAMEs (with -w, -m only)
  -p, --path            NAME is a PATH list (i.e., '/bin:/usr/bin')
  -U, --proc-cygdrive   Emit /proc/cygdrive path instead of cygdrive prefix
                        when converting Windows path to UNIX path.
  -s, --short-name      print DOS (short) form of NAMEs (with -w, -m only)
  -C, --codepage CP     print DOS, Windows, or mixed pathname in Windows
                        codepage CP.  CP can be a numeric codepage identifier,
                        or one of the reserved words ANSI, OEM, or UTF8.
                        If this option is missing, cygpath defaults to the
                        character set defined by the current locale.

System information:

  -A, --allusers        use `All Users' instead of current user for -D, -O, -P
  -D, --desktop         output `Desktop' directory and exit
  -H, --homeroot        output `Profiles' directory (home root) and exit
  -O, --mydocs          output `My Documents' directory and exit
  -P, --smprograms      output Start Menu `Programs' directory and exit
  -S, --sysdir          output system directory and exit
  -W, --windir          output `Windows' directory and exit
  -F, --folder ID       output special folder with numeric ID and exit

Other options:

  -f, --file FILE       read FILE for input; use - to read from STDIN
  -o, --option          read options from FILE as well (for use with --file)
  -c, --close HANDLE    close HANDLE (for use in captured process)
  -i, --ignore          ignore missing argument
  -h, --help            output usage information and exit
  -V, --version         output version information and exit
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年07月15日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • convert unix path to windows style
  • convert windows path to unix style
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档