如何处理包含有问题(= shell 元和空格)字符的路径名?

发布于 2024-12-01 13:12:48 字数 103 浏览 1 评论 0原文

我经常会遇到名称奇怪的文件,例如 /foo/bar * baka/waka xoxotinh(|).mp3。为了正确引用文件,以任意语言转义这个东西的正确方法是什么?

Often I have files with weird names like /foo/bar * baka/waka xoxotinh(|).mp3. What is the correct way to escape this thing in an arbitrary language in order to correctly reference the file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

神经暖 2024-12-08 13:12:48

使用 \: 转义每个保留字符,

/foo/bar\ \*\ baka/waka\ \*\ xoxotinh\(\|\).mp3

或者将其放在引号中,如下所示:

"/foo/bar * baka/waka * xoxotinh(|).mp3"

Escape each reserved character with a \:

/foo/bar\ \*\ baka/waka\ \*\ xoxotinh\(\|\).mp3

or put it in quotes like this:

"/foo/bar * baka/waka * xoxotinh(|).mp3"
这样的小城市 2024-12-08 13:12:48

您可以通过在 '' 中包含整个路径或通过 \ 转义单个“奇怪”字符来转义它。

这并不总是一件容易的任务,因此如果您直接从 bash 提示符执行此操作,则只需使用 TAB 来完成路径 - bash 将为您转义“奇怪”的字符。

以下是 bash 转义的方法:

cd /foo/bar\ \*\ baka/waka\ xoxotinh\(\|\).mp3/

注意:我通过以下方式创建了路径:

mkdir -p '/foo/bar * baka/waka xoxotinh(|).mp3'

You can escape it by including the whole path in '' or by escaping individual "weird" chars by \.

This is not always an easy task, so if you do it right from bash prompt, then just use TAB to complete the path - bash will escape "weird" chars for you.

Here's how bash escaped it:

cd /foo/bar\ \*\ baka/waka\ xoxotinh\(\|\).mp3/

Note: I've created the path by:

mkdir -p '/foo/bar * baka/waka xoxotinh(|).mp3'
仄言 2024-12-08 13:12:48

在任意语言中,它完全取决于语言及其引用约定。在我熟悉的大多数语言中,您的示例文件名并不是特别有问题,除了在 shell 脚本中;在 sh 和衍生产品中,对此类事情使用单引号。

In an arbitrary language, it depends entirely on the language, and its quoting conventions. In most languages I am familiar with, your example file name is not particularly problematic, except in shell script; in sh and derivatives, use single quotes for this sort of thing.

も让我眼熟你 2024-12-08 13:12:48

您需要在引号之间添加文件名,例如 "asd * sdd||ss.mp3" 或转义字符。转义意味着您需要在每个无效字符前添加 \

在 php 中你有 http://www.php.net/manual/en/function例如.escapeshellarg.php

You need to either add the filename between quotes like "asd * sdd||ss.mp3", or escape characters. Escaping means you need to add a \ before each invalid character.

in php you have http://www.php.net/manual/en/function.escapeshellarg.php for example

握住我的手 2024-12-08 13:12:48

在 BASH 中,您可以使用双引号来删除 *? 的通配含义。反引号稍微麻烦一些。你必须把它们加倍。

事实上,您可以简单地在 Bash 中的任何非字母数字字符前面添加一个反勾号斜杠,并且非常安全。这在 Linux 上的 Bash 中非常有效:

newFileName=$(echo -E "$fileName" | sed 's/[^[:alnum:]]/\\/g')

In BASH, you can use double quotes to remove the globbing meaning of * and ?. Backticks are a bit more troublesome. You'll have to double up those.

In fact, you can simply add a backtickslash in front of any non-alpha numeric character in Bash and be quite safe. This worked quite nicely in Bash on Linux:

newFileName=$(echo -E "$fileName" | sed 's/[^[:alnum:]]/\\/g')
划一舟意中人 2024-12-08 13:12:48

除了用反斜杠引用每个 shell 元字符和空格之外,没有通用解决方案。如果您用“单引号”引用,则会出现包含单引号的名称的问题。如果您用“双引号”引用,则包含双引号和美元符号的名称会出现问题。当然,也有包含两者的名称...但是您可以使用不同的引用样式引用名称的部分内容:

ls "name with spaces & a single quote->'"' and name with "quotes" and 

请注意第二个双引号如何结束第一个部分,下一个字符是引用其余部分的单引号。那里!引用了包含两个引号的名称!一旦 shell 执行了引号删除(始终是最后),ls 将被视为

name with spaces & a single quote->' and name with "quotes" and $

单个参数。

请注意第二个双引号如何结束第一个部分,下一个字符是引用其余部分的单引号。那里!引用了包含两个引号的名称!一旦 shell 执行了引号删除(始终是最后),ls 将被视为

单个参数。

There is no general solution other than to quote each shell meta character and whitespace with a backslash. If you quote with 'single quotes' there's the problem with names containing a single quote. If you quote with "double quotes" there's the problem with names containing double quotes and dollar signs. And of course there are names containing both... But you can quote parts of names with different quoting styles:

ls "name with spaces & a single quote->'"' and name with "quotes" and 

Note how the second double quote ends the first part, and the next character is the single quote that quotes the remainder. There! Quoted a name containing both quotes! Once the shell has performed quote removal (always last), ls will see

name with spaces & a single quote->' and name with "quotes" and $

as a single argument.

Note how the second double quote ends the first part, and the next character is the single quote that quotes the remainder. There! Quoted a name containing both quotes! Once the shell has performed quote removal (always last), ls will see

as a single argument.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文