bash sed 未终止的 `s' 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/11425340/
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 unterminated `s' command
提问by David Diaz
I have this bash code
我有这个 bash 代码
REPLACEMENT="s:define('CP_DIRECTORY.*:define('CP_DIRECTORY), ('"${NEWLOCATION}"');:;"
sed -i $REPLACEMENT $INITDATA;
It returns
它返回
sed: -e expression #1, char 9: unterminated `s' command
I am trying to replace a line in initdata.php that looks like this:
我正在尝试替换 initdata.php 中的一行,如下所示:
define( 'CP_DIRECTORY', 'admin' );
to this
对此
define( 'CP_DIRECTORY', 'AJgBCecvPBUnPZCLvKukyzfehWrsF5wI' );
回答by Paused until further notice.
Put single quotes around the entire sedcommand:
在整个sed命令周围加上单引号:
sed -i 's:AdminCP = .*:AdminCP = "/home/pi/forums/AJgBCecvPBUnPZCLvKukyzfehWrsF5wI";:' forums/initdata.php
Otherwise, the command is split on the spaces. Also, you need a dot before the asterisk, otherwise you're just saying "zero or more spaces".
否则,命令会在空格上分开。此外,您需要在星号前加一个点,否则您只是在说“零个或多个空格”。
回答by David Diaz
Got it to work thanks to Dennis Williamson saying that I needed to quote the sed command & http://www.linuxquestions.org/questions/linux-software-2/sed-with-bash-variables-304260/
感谢丹尼斯威廉姆森说我需要引用 sed 命令和http://www.linuxquestions.org/questions/linux-software-2/sed-with-bash-variables-304260/
Final code was
最终代码是
    REPLACEMENT="define( 'CP_DIRECTORY' ), ( '"
    REPLACEMENT=$REPLACEMENT$NEWLOCATION;
    REPLACEMENT=$REPLACEMENT"' );:;";
    sed -i "s:define( 'CP_DIRECTORY.*:$REPLACEMENT" $INITDATA;

