传递到UNIX程序Windows路径转义反斜线 [英] Escaping backslash in windows paths passed to unix programs

查看:152
本文介绍了传递到UNIX程序Windows路径转义反斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想逃避cygwin的反斜杠,但似乎几乎是不可能的
我已经尝试了很多东西,但没有工作的权利..

I am trying to escape backslashes in cygwin, but it seems almost impossible I have tried a lot of things, but none work right..

  echo "C:\Users\Ted\Documents\Unix\Scripts" | xargs echo
  echo 'C:\Users\Ted\Documents\Unix\Scripts' | xargs echo

更具体地说,我需要得到一个命令接受在bash输入而不
失去了反斜杠字符。每次我试图通过一个说法,
反斜线总是消失,破坏我的输入。我不知道
我怎么能告诉它刚刚离开反斜线输入孤独。

More specifically I need to get a command to receive input in bash without losing the backslash characters. every time I try to pass an argument, the backslashes always disappear, destroying my input. And I don't know how I can tell it to just leave the backslashes on the input alone.

我曾尝试以下,但似乎没有工作。

I have tried the following but neither seems work

  alias cyg0='cygpath '$*'  '
  alias cyg1='cygpath "$*"  '
  alias cyg2='cygpath "'$*'"'

  alias cyg3='cygpath '$@'  '
  alias cyg4='cygpath "$@"  '
  alias cyg5='cygpath "'$@'"'


  Ted@Machine01 ~
  $ cyg0 C:\Users\Ted\Music\Enigma
  C:UsersTedMusicEnigma

  Ted@Machine01 ~
  $ cyg1 C:\Users\Ted\Music\Enigma
  cygpath: can't convert empty path

  Ted@Machine01 ~
  $ cyg2 C:\Users\Ted\Music\Enigma
  cygpath: can't convert empty path

  Ted@Machine01 ~
  $ cyg3 C:\Users\Ted\Music\Enigma
  C:UsersTedMusicEnigma

  Ted@Machine01 ~
  $ cyg4 C:\Users\Ted\Music\Enigma
  C:UsersTedMusicEnigma

  Ted@Machine01 ~
  $ cyg5 C:\Users\Ted\Music\Enigma
  cygpath: can't convert empty path

顺便说一句,我希望能够输入C:\\用户\\特德\\音乐\\谜无
引号。使用引号时,其中的一个别名作品。

By the way, I want to be able to type C:\Users\Ted\Music\Enigma without quotes. One of those aliases works when using quotes.

泰德

推荐答案

请阅读标题为在bash引述节(1)(男人庆典)。

Please read the section titled QUOTING in bash(1) (man bash).

在你的第一个例子中,

echo "C:\Users\Ted\Documents\Unix\Scripts" | xargs echo

外壳间$ P $点(然后删除)双引号内的反斜杠,所以第一个回声命令只能看到字符串 C: UsersTedDocumentsUnixScripts

在你的第二个例子,

echo 'C:\Users\Ted\Documents\Unix\Scripts' | xargs echo

单引号正确保护反斜杠,您可以通过运行只是第回声命令看,如果没有 | xargs的回声

在您的所有其余的例子,

In all of your remaining examples,

cyg0 C:\Users\Ted\Music\Enigma
cyg1 C:\Users\Ted\Music\Enigma
...

由于参数( C:\\用户\\特德\\音乐\\谜)是不带引号的,再次外壳间$ P $点和命令之前删除反斜杠( cyg0 cyg1 ,...)曾经看到他们。而且......

since the argument (C:\Users\Ted\Music\Enigma) is unquoted, once again the shell interprets and removes the backslashes before the command (cyg0, cyg1, ...) ever sees them. And...

我希望能够输入C:\\用户\\特德\\音乐\\谜不带引号

I want to be able to type C:\Users\Ted\Music\Enigma without quotes.

抱歉,它不能这样做。如果你不把反斜线在单引号,外壳间preT每一个为它后面,然后取出反斜杠字符一个字符引用

Sorry, it can't be done. If you don't put the backslashes in single quotes, the shell will interpret each one as a quoting character for the character that follows it, then remove the backslash.

您有几种选择:


  1. 在命令行中使用单引号,以保护反斜杠:

  1. Use single quotes on the command line, to protect the backslashes:

cyg4 'C:\Users\Ted\Music\Enigma'


  • 报价每一个反斜杠字符分开,通过加倍它,例如

  • Quote each backslash character separately, by doubling it, e.g.

    cyg4 C:\\Users\\Ted\\Music\\Enigma
    


  • 使用正斜杠,而不是:

  • Use forward slashes instead:

    cyg4 C:/Users/Ted/Music/Enigma
    


  • 选项3可能真的是你最好的解决方案。不幸的是,反斜杠字符仅仅是所有Unix外壳一个非常特殊的角色,这导致了很多DOS / Windows路径打交道时引用的麻烦的。但是,什么是不广为人知的是,DOS / Windows的还非常乐于使用正斜杠( / )作为路径分隔符。试试吧。

    Option 3 could really be your best solution. Unfortunately the backslash character is just a very special character for all Unix shells, and this causes a lot of quoting hassles when dealing with DOS/Windows paths. But what's not widely known is that DOS/Windows is also perfectly happy to use the forward slash (/) as its path separator. Try it.

    请注意:你的几个CYG *函数中,只有cyg1和cyg4是正确的。其他人使用不正确引用。 cyg1只有一个参数,因为它加入所有的参数都连成一片引号的字符串是有用的,而cyg4可以接受,并在多个参数传递到cygpath。然而,由于cyg4只传递其报价参数,它不会增加任何价值;这是从刚刚例如真的没有什么不同。

    Note: Of your several cyg* functions, only cyg1 and cyg4 are correct. The others use incorrect quoting. cyg1 is only useful with one argument since it joins all of the arguments together into one quoted string, while cyg4 can accept and pass on multiple arguments to cygpath. However, since cyg4 only passes on its quoted arguments, it doesn't add any value; it's really no different from just e.g.

    cygpath C:/Users/Ted/Music/Enigma
    

    祝你好运。

    这篇关于传递到UNIX程序Windows路径转义反斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆