in reply to Escape the apostrophe in sed command in perl script
You can try to escape the apostrophe, but due to the inherent pain in the butt and security considerations due to trying to escape every special shell metacharacter, I would recommend using the list call to system instead:
no warnings 'qw'; # Silence comma warning in qw my $foo = "foo'bar'"; system qw(echo sed -n -e /log-passed/,/log-end/p), "/var/log/app1/$foo +"; __END__ sed -n -e /log-passed/,/log-end/p /var/log/app1/foo'bar'
The reason this works is that when system() is called with a single scalar containing metacharacters, that expression is passed to the shell, which will interpret shell metacharacters such as quotes, output redirection, semicolons, etc. However when system() is called with a list, Perl bypasses the shell and invokes the command directly, passing each additional list item as arguments to the command, so there is no shell involved at all, and, hence, no shell metacharacters.
|
|---|