termux / termux-app

Termux - a terminal emulator application for Android OS extendible by variety of packages.
https://f-droid.org/en/packages/com.termux
Other
36.27k stars 3.81k forks source link

grep only greps visible characters - screen width dependent #540

Closed AngharadH closed 6 years ago

AngharadH commented 6 years ago

Strange behaviour noticed when writing a bash script which grepped for a command in ps x The command was long, and if typed at the terminal would go off the screen when vertical, but not when horizontal. Result, even in the script where no ps screen output was generated, the bash script failed in vertical orientation, but succeeded in horizontal orientation. This suggests that grep is ignoring all characters in an output which are beyond the screen width. Steps to reproduce: Start playing something using mpv Then type: ps x | grep mpv Observe how this succeeds if screen is horizontal, but fails if vertical

fornwall commented 6 years ago

I'm unable to reproduce it, and it sounds really strange.

Could it be something else? The mpv was perhaps not running when in landscape mode?

trygveaa commented 6 years ago

ps limits the output to the terminal width, so it cuts of long lines. Though, this should only happen when it prints directly to a terminal, and not when you pipe the result to something. This works for me, so I can't reproduce the problem either. But I would guess that it's more likely a problem with this detection in ps, than a problem with grep. You could check if the result from ps x | cat is cut off to confirm it.

AngharadH commented 6 years ago

See attached two screenshots. The alias "rsc" which plays the stream shown in the shots via mpv is started. I then grep for mpv with phone vertical (it fails), then with phone horizontal (it succeeds). Clearly it is running in the first instance - (a) the screen shows it has started, (b) I can hear it. In the horizontal image, you can see the output from the first run of the command still. I simply rotated, and re-ran the command. screenshot_2018-01-13-22-57-08 screenshot_2018-01-13-22-57-36

SDRausty commented 6 years ago

@AngharadH Thank you for the screenshots. What is the name of your keyboard? Do you think this keyboard might be causing the error? If the keyboard is open source, can you post the link? See whether the same error occurs with another touchscreen and hardware https://github.com/termux/termux-packages/issues/1145 keyboard.

AngharadH commented 6 years ago

I'm using the Huawei swype keyboard on an Honor 7. The Gboard keyboard exhibits the same problem though. In fact, it is clear from an ssh session from a gnome-terminal into the phone that in fact grep should be showing three results, as below:

26047 pts/0 S< 0:00 /data/data/com.termux/files/usr/bin/sh /data/data/com.termux/files/usr/bin/mpv 26048 pts/0 S<l 0:18 /data/data/com.termux/files/usr/libexec/mpv http://www.radioswissclassic.ch/de 26897 pts/1 S<+ 0:00 grep mpv

So, I tried changing the size of my terminal window (a standard gnome-terminal), and the same behaviour was found. Here, the output from three decreasing window widths, with the screenshot taken after resizing the window back to wider.

honor

Quasic commented 6 years ago

These are apparently the build settings, for reference from https://github.com/termux/termux-packages/blob/master/packages/busybox/busybox.config:

CONFIG_PS=y
# CONFIG_FEATURE_PS_WIDE is not set
# CONFIG_FEATURE_PS_LONG is not set
CONFIG_FEATURE_PS_TIME=y
CONFIG_FEATURE_PS_ADDITIONAL_COLUMNS=y
# CONFIG_FEATURE_PS_UNUSUAL_SYSTEMS is not set
f3flight commented 6 years ago

@AngharadH I think that the problem is in ps, not grep, ps in termux limits output to screen width, and you're not handling that. I recommend to use pgrep instead of ps, it has a flag for full command line search, and you'll only need 1 command instead of 2. I think this can be closed.

Grimler91 commented 6 years ago

@f3flight Thanks.