简体   繁体   English

PHP shell_exec 不同的输出

[英]PHP shell_exec different output

How it's possible that shell_exec give a different results than when the command is manually triggered? shell_exec 怎么可能给出与手动触发命令时不同的结果? I'm executing我正在执行

shell_exec('/usr/bin/wc --words ' . $filepath). 

My PHP is 7.4.3 , I have also tried wc -w .我的 PHP 是 7.4.3 ,我也试过wc -w It gives a different output when is triggered manually from shell.当从 shell 手动触发时,它会提供不同的输出。 I have tested it with webmaster user from shell and its running under webmaster user on web.我已经使用 shell 中的 webmaster 用户对其进行了测试,并且它在 web 上的 webmaster 用户下运行。 I have tested this with running whoami .我已经通过运行whoami对此进行了测试。 It just doesn't make any sense.它只是没有任何意义。 It doesn't give any errors or anything, it just gives different outputs.它不会给出任何错误或任何东西,它只是给出不同的输出。 When I run it with webmaster from shell it gives a regular ouput, eq.当我从 shell 与 webmaster 一起运行它时,它会提供一个常规的输出,eq。 exact number of words.确切的字数。 Anyone have any idea where to look since I have already tried users and permissions?任何人都知道在哪里看,因为我已经尝试过用户和权限?

Make sure to use the same locale, especially for the LC_CTYPE character encoding setting.确保使用相同的语言环境,尤其是对于LC_CTYPE字符编码设置。 Different languages have different ideas of what a word and non-word character is:不同的语言对单词和非单词字符有不同的理解:

gnu/linux$ echo 'الدروس المستفادة من' | LC_CTYPE=C wc -w
0
gnu/linux$ echo 'الدروس المستفادة من' | LC_CTYPE="en_US.UTF-8" wc -w
3

You may also see this differ between versions of wc , here on macOS:您可能还会看到wc版本之间的差异,在 macOS 上:

macos$ echo 'الدروس المستفادة من' | LC_CTYPE=C wc -w
   3
macos$ echo 'الدروس المستفادة من' | LC_CTYPE="en_US.UTF-8" wc -w
   5

It can sometimes be hard to predict which locale ends up being used, because it can be set by individual applications, by the user configuration, by system defaults, or even by settings on the system the user SSH'd from.有时很难预测最终会使用哪种语言环境,因为它可以由单个应用程序、用户配置、系统默认设置,甚至是用户通过 SSH 访问的系统上的设置进行设置。

To see the current locale in effect, you can run locale .要查看当前有效的语言环境,您可以运行locale Copy the settings from there to make the numbers match up.从那里复制设置以使数字匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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