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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |