我怎样才能逃避正斜杠在用户输入变量在bash? [英] How can I escape forward slashes in a user input variable in bash?

查看:172
本文介绍了我怎样才能逃避正斜杠在用户输入变量在bash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个脚本,以建立一个网站Statamic更有效。我遇到的问题是,可变我使用替换文件中的字符串已经转义正斜杠,是用户输入。我怎样才能确保 _site_url:http://statamic.com 将成为 _site_url:http://example.com

下code将工作只要没有斜杠present。

 回声什么是你的网站的网址,不要忘了协议?!(如HTTP://)
阅读-e SITE_URL回声%s / _site_url:HTTP:\\ / \\ / statamic.com/_site_url:$ SITE_URL / G
W¯¯
q
|恩_config / settings.yaml


解决方案

问题是在你的命令的分隔符。我们可以把任何东西,我们希望不是,我用 @ 这里:

试着这样做:

  SED -iS @ _site_url:http://statamic.com/_site_url:$ @ SITE_URL G_config / settings.yaml

或与命令:

 回声什么是你的网站的网址,不要忘了协议?!(如HTTP://)
阅读-e SITE_URL回声%S @ _site_url:HTTP://statamic.com@_site_url:$ @ SITE_URL摹
W¯¯
q
|恩_config / settings.yaml

I'm working on a script to make setting up a Statamic site more efficient. The problem I'm running into is that the variable I'm using to replace a string in a file has unescaped forward slashes and is user input. How can I make sure that _site_url: http://statamic.com will become _site_url: http://example.com?

The code below will work as long as there are no forward slashes present.

echo "What's your site URL? Don't forget the protocol (ex. http://)!"
read -e SITE_URL

echo "%s/_site_url: http:\/\/statamic.com/_site_url: $SITE_URL/g
w
q
" | ex _config/settings.yaml

解决方案

The problem is the delimiter in your ex command. We can put anything we'd like instead, I use @ here :

Try doing this :

 sed -i "s@_site_url: http://statamic.com/_site_url: $SITE_URL@g" _config/settings.yaml

Or with your ex command :

echo "What's your site URL? Don't forget the protocol (ex. http://)!"
read -e SITE_URL

echo "%s@_site_url: http://statamic.com@_site_url: $SITE_URL@g
w
q
" | ex _config/settings.yaml

这篇关于我怎样才能逃避正斜杠在用户输入变量在bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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