sed bash 脚本中`s' 的未知选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24705650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sed unknown option to `s' in bash script
提问by user3566292
Im having a bit of trouble putting together a bash script im working on
我在整理一个正在处理的 bash 脚本时遇到了一些麻烦
Here is the code im using.
这是我使用的代码。
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
在我看来这应该有效。但我不知道我的错误在哪里
回答by tripleee
One of the values contains a slash. You need to escape it, or use a different delimiter.
其中一个值包含斜杠。您需要对其进行转义,或使用不同的分隔符。
Additionally, you are needlessly stringing together multiple invocations where a single one would do. The cat
is also useless.
此外,您不必要地将多个调用串在一起,而单个调用就可以完成。在cat
也没用。
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
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
方言都兼容。如果您不喜欢多个-e
选项,请尝试使用单个以换行符分隔的sed
命令字符串。我提供此语法中的第二个脚本作为示例。
sed "s!GITHUB!${REPO}!ig
s!COINNAME!${NAME}!ig" /crawler/bc_layout.php > bc_layout.php
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.
如果您的值可以包含@
或!
,则需要选择不同的分隔符。任何非字母数字 ASCII 字符都可以,但反斜杠和引号显然有问题。
回答by Jonathan Leffler
Leave the cat
s out of this; they're happier curled up in a corner and left in peace. The first sed
in each pipeline can read the file.
把cat
s 排除在外;他们更快乐地蜷缩在角落里,平静地离开。sed
每个管道中的第一个可以读取文件。
You should be able to combine those separate sed
commands into one per pipeline, too. That is, your current command:
您也应该能够将这些单独的sed
命令合并到每个管道中。也就是说,您当前的命令:
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
should be:
应该:
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
This ignores the issue with the unknown option to the s///
command, which is probablymight be referring to the i
flag. The GNU sed
man page on Linux doesn't list that, but the online GNU sed
manual does list both i
and I
as options for case-insensitivity, so that should not be the problem. One other possibility is that the expansion of ${PORT}
contains a slash; that could cause the error you're seeing (or, that the problem is in the second pipeline instead of the first).
这忽略与未知选项的问题s///
命令,这可能是可能是指i
标志。sed
Linux 上的 GNU手册页没有列出,但在线GNUsed
手册确实列出了i
和I
作为不区分大小写的选项,所以这应该不是问题。另一种可能性是 的扩展${PORT}
包含斜线;这可能会导致您看到的错误(或者,问题出在第二个管道而不是第一个管道中)。
If you are not using GNU sed
(and the error suggests that you might not be), you might need to brute-force the patterns.
如果您没有使用 GNU sed
(并且错误表明您可能没有使用),您可能需要对模式进行暴力破解。
sed -e "s/[pP][oO][rR][tT]2/${PORT}/g" \
-e 's/[iI][pP]2/IPADDRESS/g' \
-e 's/[uU][sS][eE][rR]2/USER/g' \
-e 's/[pP][aA][sS][sS]2/PASSWORD/g' \
/crawler/bc_daemon.php > bc_daemon.php
Note that both these pipelines will fail horribly if the current directory is /crawler
because the output redirection will clobber the input file before the command is executed.
请注意,如果当前目录是/crawler
因为输出重定向会在执行命令之前破坏输入文件,那么这两个管道都会严重失败。
回答by konsolebox
One possible thing is that i
may not be recognized by the sed
you use that you have no choice but to remove it:
一种可能的事情是您使用的i
可能无法识别sed
您别无选择,只能将其删除:
sed "s/GITHUB/${REPO}/g"
(kunwar.sangramsuggests that the uppercase I
may be recognized instead.)
(kunwar.sangram建议I
可以识别大写字母。)
Another thing is that some of your variables may contain the delimiter you use to s
, so perhaps trying to use another delimiter may fix it:
另一件事是您的某些变量可能包含您使用的分隔符 to s
,所以也许尝试使用另一个分隔符可能会修复它:
sed "s|GITHUB|${REPO}|ig"
回答by kunwar.sangram
Try
尝试
cat /crawler/bc_layout.php | \
sed "s/GITHUB/${REPO}/gI" | \
sed "s/COINNAME/${NAME}/gI" > bc_layout.php