CommandNotFound.py writes some messages (such as "Please ask your administrator.") to standard output instead of standard error. This can break scripts that run commands and expect standard output to be empty if the command is not installed. Patch attached.
226: print(_("Please ask your administrator."))
240: print(_("Please ask your administrator."))
375: print("You also have python3 installed, you can run 'python3' instead.")
376: print("")
```
# test
$ ruptime
Command 'ruptime' not found, but can be installed with:
apt install rwho
Please ask your administrator.
$ ruptime 2>/dev/null
Please ask your administrator.
```
For the last command, the expected behaviour is that no output will be written to the screen (all output should be written to stderr, which the last command redirects to /dev/null).
CommandNotFound.py writes some messages (such as "Please ask your administrator.") to standard output instead of standard error. This can break scripts that run commands and expect standard output to be empty if the command is not installed. Patch attached.
The main bug was introduced in revision 216 (https:/ /bazaar. launchpad. net/~ubuntu- core-dev/ command- not-found/ ubuntu/ revision/ 216). The print() call in line 255 was split into two calls (lines 262 and 263), but the `file=` parameter was omitted from the second call.
The following lines in the latest revision write to standard output and I think should be changed to write to standard error by adding `file=self. output_ fd` (see attached patch). The line numbers refer to https:/ /bazaar. launchpad. net/~ubuntu- core-dev/ command- not-found/ ubuntu/ view/278/ CommandNotFound /CommandNotFoun d.py:
226: print(_("Please ask your administrator."))
240: print(_("Please ask your administrator."))
375: print("You also have python3 installed, you can run 'python3' instead.")
376: print("")
Full steps to reproduce:
```
# setup
docker run --rm -it ubuntu:22.04
apt update
apt install -y command-not-found bash-completion
apt update # populate command-not-found database
su -s /bin/bash nobody
```
```
# test
$ ruptime
Command 'ruptime' not found, but can be installed with:
apt install rwho
Please ask your administrator.
$ ruptime 2>/dev/null
Please ask your administrator.
```
For the last command, the expected behaviour is that no output will be written to the screen (all output should be written to stderr, which the last command redirects to /dev/null).
``` command- not-found --version commandnotfound commandnotfound 22.04.0 ...
# versions
$ /usr/lib/
0.3
$ dpkg -l command-not-found python3-
...
ii command-not-found 22.04.0 ...
ii python3-
```