简体   繁体   中英

How to process unicode characters (utf8) in shell command executed in php

I have dir function that I use to find all executables files and directories it have code like this:

    $EXEC = 'X';
    $DIR = 'D';
    $FILE = 'F';
    // depend on GNU version of find (not tested on different versions)
    $cmd = "find . -mindepth 1 -maxdepth 1 \\( -type f -executable -printf ".
           "'$EXEC%p\\0' \\)  -o -type d -printf '$DIR%p\\0' -o \\( -type l -x".
           "type d -printf '$DIR%p\\0' \\) -o -not -type d -printf '$FILE%p\\0'";
    $result = $this->command($token, $cmd, $path);

my command function have code like this:

    $pre .= ";export LC_ALL=en_US.UTF-8; export HOME='$home';cd '$path'; $aliases\n";
    $post = ";echo -n \"$marker\";pwd";
    $command = escapeshellarg($pre . $command . $post);
    $command = $this->sudo($token, $username, '/bin/bash -c ' . $command . ' 2>&1');
    $command = $this->unbuffer($token, $command);
    $result = $this->$shell_fn($token, $command);

where $shell_fn is one of the shell exec functions or perl/python cgi invocation using curl.

by adding export LC_ALL=en_US.UTF-8 before command I've resolved the issue of ls, it now show content that have unicode characters like Robert Gawliński but when I do echo ą or try to call dir on directory Robert Gawliński it break. echo ą display Ä and cd Robert Gawliński show error

/bin/bash: line 2: cd: $'Robert GawliÅ\302\204ski/': No such file or directory

It seems that input to shell command is handled by php locale so:

  1. to handle output you need to export LC_ALL inside exec
  2. but for input you need to call:
$locale = 'en_US.UTF-8';
setlocale(LC_ALL, $locale);
putenv('LC_ALL='.$locale);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM