In most cases, it's simpler to just avoid the shell and exec the program directly. In this case, this can be done by using the multi-argument pipe-open (unless you have a very ancient perl). It would come out something like this (untested):

my $resultLdapSearch; { open my $P, "-|", "ldapmodify", "-h", $ldap_server, "-D", "cn=Director +y Manager", "-w", $dir_mgr_pwd, "-f", $ldap_modify_file or die "cannot exec ldapmodify: $!"; local $/ = undef; defined($resultLdapSearch = <$P>) or die "error reading from pipe: $!"; close $P or die $! ? "error closing pipe: $!" : "ldap_server exitted with a non +zero exit status: $?"; }

If you really need to quote strings for a bash-like shell, try one of these (untested). The last one only works in newer bash shells, but it has the advantage that it also quotes control and meta characters to characters readable on any terminal. The first three probably works in any shell.

sub shell_quote1 { local($_) = @_; s/([^\w\n])/\\$1/g; s/\n/\"\n\"/g; +length or $_ = q/""/; $_ } sub shell_quote { local($_) = @_; s/([\\\$\"\`])/\\$1/g; qq/"$_"/ } sub shell_quote2 { local($_) = @_; s/'/'\\''/g; qq/'$_'/ } sub bash_quote3 { local($_) = @_; s/(\W)/sprintf("\\x%02x", ord($1))/g +e; qq/\$'$_'/ }

The shell_quote1 function is similar to quotemeta except that it quotes newline characters properly, whereas quotemeta translates them to "\\\n" which the shell will unquote as nothing. I also quotes empty strings to an empty shell word.

Finally, note that you can't quote nul characters for the shell in any way, because the shell cannot pass those as arguments to programs. Also, these functions except byte strings, so encode utf-8 strings before passing to these functions.


In reply to Re^4: How to capture error messages... (shell quoting recipe) by ambrus
in thread How to capture error messages... by fabster01

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.