我在我的perl脚本中使用以下类型的脚本并期望在第1行进入.我收到一些错误,如下所示; 任何帮助?
plz忽略perl变量....
错误消息 - sed:-e表达式#1,字符22:命令后的额外字符
# Empty file will not work for Sed line number 1
`"Security Concerns Report" > $outputFile`;
`sed '1i\
\
DATE :- $CDate \
Utility accounts with super access:- $LNumOfSupUserUtil \
Users not found in LDAP with super access: - $LNumOfSupUserNonLdap\
' $outputFile > $$`;
`mv $$ $outputFile`;
}
Run Code Online (Sandbox Code Playgroud)
您的直接问题是反斜杠操作符内的Perl解释反斜杠字符,美元字符也是如此.所以你的反斜杠换行序列变成了执行的shell命令的换行符.如果你更换这些反斜杠\\,你将会克服这个障碍,但你仍然会有一个非常脆弱的程序.
Perl正在调用一个调用sed的shell.这需要您未执行的shell的额外引用级别.如果您的文件名和数据不包含特殊字符,您可以使用它,直到有人使用包含'(在许多会破坏您的代码的事物中)的日期格式.
而不是解决这个问题,在Perl中完成所有操作要简单得多.所有sed和shell都可以做到,Perl几乎可以轻松或轻松地完成.从你的问题中你不清楚你要做什么.我将专注于sed调用,但这可能不是编写程序的最佳方式.
如果你真的需要在现有文件中添加一些文本,那么在CPAN上有一个广泛使用的模块已经做得很好.优先使用现有库来重新发明轮子.File::Slurp有一个prepend_file方法就是为了这个.在下面的代码中,我使用here-document运算符作为多行字符串.
use File::Slurp; # at the top of the script with the other use directives
File::Slurp->prepend_file($outputFile, <<EOF);
DATE :- $CDate
Utility accounts with super access:- $LNumOfSupUserUtil
Users not found in LDAP with super access: - $LNumOfSupUserNonLdap
EOF
Run Code Online (Sandbox Code Playgroud)