shonster88
shonster88

Reputation: 84

PHP shell_exec different output

How it's possible that shell_exec give a different results than when the command is manually triggered? I'm executing

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

My PHP is 7.4.3 , I have also tried wc -w. It gives a different output when is triggered manually from shell. I have tested it with webmaster user from shell and its running under webmaster user on web. I have tested this with running 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. exact number of words. Anyone have any idea where to look since I have already tried users and permissions?

Upvotes: 1

Views: 128

Answers (1)

that other guy
that other guy

Reputation: 123410

Make sure to use the same locale, especially for the LC_CTYPE character encoding setting. 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:

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.

To see the current locale in effect, you can run locale. Copy the settings from there to make the numbers match up.

Upvotes: 1

Related Questions