Bareword发现使用包含正向斜杠的变量从Ubuntu Shell运行Perl时出现语法错误

Bareword发现使用包含正向斜杠的变量从Ubuntu Shell运行Perl时出现语法错误,perl,bash,ubuntu,Perl,Bash,Ubuntu,我正在使用Ubuntu Natty 我正在尝试使用命令行perl编辑文本文件。下面是我的代码片段: path_to_php_exec="/usr/local/php/bin/php" path_to_php_prog="/root/run/php_gearman_worker.php" perl -0777 -i -pe "s/(command[ ]*=[ ]*)[^\n]+/\${1}=$path_to_php_exec $path_to_php_prog\n/g" /etc/supervis

我正在使用Ubuntu Natty

我正在尝试使用命令行perl编辑文本文件。下面是我的代码片段:

path_to_php_exec="/usr/local/php/bin/php"
path_to_php_prog="/root/run/php_gearman_worker.php"
perl -0777 -i -pe "s/(command[ ]*=[ ]*)[^\n]+/\${1}=$path_to_php_exec $path_to_php_prog\n/g" /etc/supervisord_program_gearman.conf
但是,当我运行此命令时,我得到以下错误

Bareword found where operator expected at -e line 1, near "s/(command[ ]*=[ ]*)[^\n]+/${1}=/usr"
Backslash found where operator expected at -e line 1, near "php\"
syntax error at -e line 1, near "s/(command[ ]*=[ ]*)[^\n]+/${1}=/usr"
Execution of -e aborted due to compilation errors.
我觉得这和我的shell变量中的斜杠有关,但我不太确定。对于命令行脚本编写,他还是个新手

我希望能得到一些帮助


谢谢。

是的,就是这样。您可以为Perl的///命令选择不同的分隔符。另外,正则表达式是面向行的,所以为什么不逐行解析文件呢

perl -i -pe \
    "$(printf 's{(command\s*=\s*).*}{$1 %s %s}g' "$path_to_php_exec" "$path_to_php_prog")" \
    /etc/supervisord_program_gearman.conf

我假设您不想将输出文件中的等号加倍,因此我将其从替换块中删除。

谢谢Glenn lifesaver。我对语法有点迷茫,但当我需要在另一个上下文中使用此方法时,我会尽最大努力进行推断。干杯