bash sed 未终止的 's' 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17110087/
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 Zac Powell
I have the below sed command:
我有以下 sed 命令:
Ctimezone="$shortName = exec('date +%Z');\
$longName = timezone_name_from_abbr($shortName);\
date_default_timezone_set($longName);"
sed -i 10s@.*@$Ctimezone@ /home/file.php
However this gives me the error:
但是,这给了我错误:
sed -e expression #1, char 7: unterminated 's' command
My understanding of sed is not great so im sure its an easy fix but couldn't work it out myself.
我对 sed 的理解不是很好,所以我确信它很容易解决,但我自己无法解决。
If you need any more info let me know
如果您需要更多信息,请告诉我
Changed to:
变成:
sed -i "10s@.*@$Ctimezone@" /home/file.php
but what is put into file.php is:
但放入 file.php 的是:
= exec('date +%Z'); = timezone_name_from_abbr(); date_default_timezone_set();
= exec('日期 +%Z'); = timezone_name_from_abbr(); date_default_timezone_set();
not
不是
$shortName = exec('date +%Z'); $longName = timezone_name_from_abbr($shortName); date_default_timezone_set($longName);
$shortName = exec('日期 +%Z'); $longName = timezone_name_from_abbr($shortName); date_default_timezone_set($longName);
回答by Karoly Horvath
Variables get expanded in double quotes, and since $shortNameisn't set, it becomes an empty string.
变量在双引号中展开,由于$shortName未设置,它变成了一个空字符串。
Escape the dollar signs: \$.
转义美元符号:\$.
Or, if the file you're trying to modify allows exec("date +%Z");try to switch 'and ". There's no substitution within single quotes.
或者,如果您尝试修改的文件允许exec("date +%Z");尝试切换'和". 单引号内没有替换。

