sed bash 脚本中`s' 的未知选项 [英] sed unknown option to `s' in bash script

查看:17
本文介绍了sed bash 脚本中`s' 的未知选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整理正在处理的 bash 脚本时遇到了一些麻烦

Im having a bit of trouble putting together a bash script im working on

这是我使用的代码.

cat /crawler/bc_daemon.php | sed "s/PORT2/${PORT}/ig" | sed 's/IP2/IPADDRESS/ig' | 
   sed 's/USER2/USER/ig' | sed 's/PASS2/PASSWORD/ig' > bc_daemon.php

cat /crawler/bc_layout.php | sed "s/GITHUB/${REPO}/ig" | sed "s/COINNAME/${NAME}/ig" > bc_layout.php

奇怪的是,这些行在脚本之外单独工作.但是在脚本中时我得到了这个

The odd thing is the lines work individually out of the script. but when inside the script i get this

sed: -e expression #1, char 17: unknown option to `s'

当它可以按字面意思理解时我使用 '',当它需要打印变量时使用 ""

I am using '' when it can take it literally and "" when it needs to print the variable

在我看来这应该有效.但我不知道我的错误在哪里

It seems to me this should be working. But im lost as to where my error is

推荐答案

其中一个值包含斜杠.您需要对其进行转义,或使用不同的分隔符.

One of the values contains a slash. You need to escape it, or use a different delimiter.

此外,您不必要地将多个调用串在一起,而单个调用就可以完成.cat 也没有用.

Additionally, you are needlessly stringing together multiple invocations where a single one would do. The cat is also useless.

sed -e "s@PORT2@${PORT}@ig" 
    -e 's/IP2/IPADDRESS/ig'  
    -e 's/USER2/USER/ig' 
    -e 's/PASS2/PASSWORD/ig'  /crawler/bc_daemon.php > bc_daemon.php

不幸的是,并非所有 sed 方言都兼容.如果您不喜欢多个 -e 选项,请尝试使用单个以换行符分隔的 sed 命令字符串.我提供此语法中的第二个脚本作为示例.

Unfortunately, not all sed dialects are compatible. If yours doesn't like multiple -e options, try a single string of newline-separated sed commands. I'm providing the second script in this syntax as an example.

sed "s!GITHUB!${REPO}!ig
    s!COINNAME!${NAME}!ig"  /crawler/bc_layout.php > bc_layout.php

如果您的值也可以包含 @!,您将需要选择不同的分隔符.任何非字母数字 ASCII 字符都可以,但反斜杠和引号显然有问题.

If your values could contain @ or ! as well, you will need to pick a different delimiter. Any nonalphanumeric ASCII character will do, but backslash and quotes are obviously problematic.

这篇关于sed bash 脚本中`s' 的未知选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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