在bash脚本中将sed未知选项设置为s [英] sed unknown option to `s' in bash script

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

问题描述

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

这是我正在使用的代码.

  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猫/crawler/bc_layout.php |sed"s/GITHUB/$ {REPO}/ig" |sed"s/COINNAME/$ {NAME}/ig">bc_layout.php 

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

  sed:-e表达式#1,字符17:`s'的未知选项 

在使用"时,我可以使用它,而在需要打印变量时,则使用".

在我看来,这应该是可行的.但是我迷失了我的错误所在

解决方案

其中一个值包含斜杠.您需要对其进行转义,或使用其他定界符.

此外,您不必要地将多个调用串联在一起,而单个调用会执行多个调用. 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 

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

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

如果您的值也可以包含 @ ,则需要选择其他定界符.任何非字母数字的ASCII字符都可以,但是反斜杠和引号显然是有问题的.

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

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

解决方案

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.

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 "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.

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

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