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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值