开发者

How can I pass a variable/argument into this sed script that I am testing?

I want to be able to pass in $1 into this command that I am running, but I开发者_运维百科 think it is trying it literally as opposed to using the value of $1:

find /test/$1 -type f -name '*.html' | xargs sed -i -r 's/href="http://$1//href="//g'


Yes; single quotes prevent variable expansion. You need to use double quotes, but because you are also using them literally we need to switch quoting styles mid-stream. And I'm unclear as to how you expected slashes to work simultaneously as regex delimiters and data:

find "/test/$1" -type f -name '*.html' | xargs sed -i -r 's,href="http://'"$1"'/,href="/,g'

You will also need to watch out that $1 doesn't contain any regex special characters, and if it might contain a comma then you will probably want to use something else as the regex delimiter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜