我在 perl 文件中有以下代码。该命令的目的是在file.txt的末尾添加一行“--test 0”。
system(" find . -type f -name file.txt | xargs sed -i -e "$ a- - test 0" ");
当我尝试运行脚本时,出现如下错误。
Scalar found where operator expected at timeStampConfig.pl line 24, near "" find . -type f -name file.txt | xargs sed -i -e "$ a"
(Missing operator before $ a?)
Number found where operator expected at timeStampConfig.pl line 24, near "test 0"
(Do you need to predeclare test?)
String found where operator expected at timeStampConfig.pl line 24, near "0" ""
(Missing operator before " "?)
syntax error at timeStampConfig.pl line 24, near "" find . -type f -name file.txt | xargs sed -i -e "$ a"
Execution of timeStampConfig.pl aborted due to compilation errors.
我尝试从命令提示符执行以下行,效果很好。
find . -type f -name file.txt | xargs sed -i -e '$ a- - test 0'
我还尝试使用单引号,如下所示,但最终出现错误。
system("find . -type f -name file.txt | xargs sed -i -e '$ a- - test 0'");
sed: -e expression #1, char 1: unknown command: `-'
我是 Perl 新手,需要一些帮助。
当您想要在双引号字符串中使用双引号时,您需要转义双引号:
"...\"foo\"..."
但在这种情况下,您很可能应该用单引号替换内部双引号:请注意,
$
需要在双引号字符串中进行转义。您还可以分离字符串的构建,以避免必须转义任何内容: