重击删除正斜杠 [英] Bash remove forward slash

查看:55
本文介绍了重击删除正斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的bash脚本中,我有一个变量:

In my bash script I have a variable:

appPath="/Payload/coolApp.app"

我想创建一个包含以下内容的字符串:

I want to create a string containing:

"build/ios/coolApp/"

我认为我可以使用bash字符串替换运算符:

I figured I could use bash string replacement operator:

${string/substring/replacement}

不幸的是,我发现它不能删除子字符串中的正斜杠.

Unfortunately I discovered that it doesn't remove forward slashes inside a substring.

示例脚本:

appPath="/Payload/coolApp.app"

appName=${appPath/"/.app"/}
appName=${appPath/"/Payload"/}

echo "build/ios/$appName/"

我的输出变成这样:

build/ios//Payload/coolApp.app/

我的问题:为什么不删除正斜杠?注意:我当前正在MacOSX 10.11.6上运行

My Question: Why doesn't it remove the forwards slashes? NOTE: I am currently running on MacOSX 10.11.6

测试解决方案脚本:

#!/bin/sh

appPath="/Payload/coolApp.app"

appName=${appPath/"/.app"/}
appName=${appPath/"/Payload"/}
echo "Original:" "build/ios/$appName/"


appName=${appPath/"\/.app"/}
appName=${appPath/"\/Payload"/}
echo "Escape \:" "build/ios/$appName/"


v1="${appPath/\/Payload/build\ios}"
echo "heemayl:" $v1


v2="${appPath##/Payload/}"
v2="build/ios/${appPath%.app}/"
echo "redneb:" $v2

输出:

Original: build/ios//Payload/coolApp.app/
Escape \: build/ios//Payload/coolApp.app/
heemayl: build/ios/coolApp.app
redneb: build/ios//Payload/coolApp/

推荐答案

我认为我可以使用bash字符串替换运算符:

I figured I could use bash string replacement operator:

${string/substring/replacement}

不幸的是,我发现它不能删除正斜杠在子字符串中.

Unfortunately I discovered that it doesn't remove forward slashes inside a substring.

但是当然可以,或者至少可以做到.您只需要将替换语法中的斜线与数据中的斜线区分开即可:

But of course it does, or at least can do. You simply need to distinguish the slashes that are part of the substitution syntax from those that are part of the data:

appPath=/Payload/coolApp.app

echo ${appPath/\/Payload/build/ios}

尤其要注意,只有第一个斜杠或双斜杠和第二个斜杠对替换语法很重要.任何后续的斜杠,例如示例中"build"和"ios"之间的斜杠,都是数据.

Note in particular that only the first slash or double slash and the second slash are significant to the substitution syntax. Any subsequent slashes, such as the one between "build" and "ios" in the example, are just data.

此外,您的报价有些被绊倒了.此表单也将输出您想要的内容:

Furthermore, you are getting a bit tripped up by quotation. This form will output what you want, too:

echo ${appPath/\/Payload/build\/ios}

但这不会:

echo "${appPath/\/Payload/build\/ios}"

那是因为替换中的第二个反斜杠也只是数据,因此替换会生成 build/ios \/coolApp/.当出现 outside 双引号时,反斜杠被视为要在后续的引号删除步骤中删除的引号字符.当它出现在双引号中时,它本身就是引号字符,而后跟需要在该上下文中转义的字符( $ ,`," \ )或换行符,因此,在您的情况下,它只是数据,因此在命令行扩展的引号删除阶段不会被删除.

That's because the second backslash in the substitution is also just data, so the substitution produces build/ios\/coolApp/. When that appears outside double quotes, the backslash is treated as a quote character to be removed during the subsequent quote-removal step. When it appears inside double quotes, it is itself a quote character only when followed by a character that needs escaping in that context ($, `, ", or \), or by a newline. In your case, therefore, it is just data, so it is not removed during the quote removal phase of command-line expansion.

通常,您在很多不需要使用引号的地方都使用引号.与许多语言不同,shell语言不依赖于引号来将字符串与其他语法元素区分开.实际上,它几乎没有这种区别.您只需要使用引号来抑制一个或多个字符的特殊含义.

More generally, you are using quotes in a lot of places where you do not need to do. Unlike many languages, the shell language does not rely on quotation to distinguish strings from other syntactic elements. In fact, it has very little distinction of that kind. You need to use quotation only to suppress what would otherwise be a special meaning of one or more characters.

这篇关于重击删除正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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