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

今天在写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路径,示例如下:

# 当前路径(pwd)转为windows路径
$ cygpath -w $(pwd)
J:\facelog-install\sql

convert windows path to unix style

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

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

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

$ cygpath -S
/c/Windows/System32
# 如果要显示windows路径就加上-w
$ cygpath -S -w
C:\Windows\system32

-D显示桌面(Desktop)路径

$ cygpath -D
/c/Users/guyadong/Desktop

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

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券