I have a very strange behaviour with the script below. It executes properly, but after that anything I type at the prompt is recognized, but not displayed. So I can type another command or use the arrow buttons to select one from the history, but the prompt remains empty. If I execute the new, invisible command, the command output is displayed correctly, but the problem remains. Only with a new terminal instance the problem is gone.


getAspect () {
        mplayer -vo null -nosound "$1" 2>&1 |
        while read line; do  # wait until mplayer prints aspect infos or starts to play
            [[ $line =~ Aspe[ck]t.is.*1\.33:1 ]] && echo 1 && break
            [[ $line =~ Aspe[ck]t.is.*0\.56:1 ]] || [[ $line =~ Aspe[ck]t.is.*1\.78:1 ]] && echo 2 && break
            [[ $line == "VO: [null]"* ]] && echo 0 && break
    pkill -n mplayer
    return $aspectR  # returns 1 (4:3), 2 (16:9) or 0 (no aspect ratio)

getAspect "./NameOfAMovieFile"

I could narrow down the problem to the following:

Using the mplayer output (see below) I inserted another test [[ $line =~ such ]] && echo 9 && break in the loop to sequentially check at which line the problem started.

Result: Line 4 (LIRC). If the test matched any line before, the problem didn't occur, else it did.

Saving the mplayer output to file and replacing mplayer with cat "saved_output" in the function didn't raise the problem.

So where does the error lie?

Is it a bug in the function, mplayer producing some weird output, bash or my terminal programs (tested with Konsole and Yakuake)?

How can I fix this?

# mplayer -vo null -nosound "./NameOfAMovieFile" 2>&1
MPlayer 1.2.r38008-Packman-8 (C) 2000-2017 MPlayer Team
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing ./NameOfAMovieFile.
libavformat version 58.12.100 (external)
libavformat file format detected.
[lavf] stream 0: video (h264), -vid 0
[lavf] stream 1: audio (vorbis), -aid 0, -alang eng
VIDEO:  [H264]  1280x720  0bpp  29.970 fps    0.0 kbps ( 0.0 kbyte/s)
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
libavcodec version 58.18.100 (external)
Selected video codec: [ffh264] vfm: ffmpeg (FFmpeg H.264)
Clip info:
 COMPATIBLE_BRANDS: iso6avc1mp41
 ENCODER: Lavf57.71.100
Load subtitles in ./
Audio: no sound
Starting playback...
Movie-Aspect is 1.78:1 - prescaling to correct movie aspect.
VO: [null] 1280x720 => 1280x720 Planar YV12  [zoom]
V:   0.0   0/  0 ??% ??% ??,?% 0 0 [J
V:   0.0   0/  0 ??% ??% ??,?% 0 0 [J

MPlayer interrupted by signal 2 in module: sleep_timer
V:   3.1   0/  0 19%  0%  0.0% 0 0 [J

Exiting... (Quit)

My system: OpenSuse TumbleWeed, KDE


It's because when you kill the script by Ctrl+C, terminal send SIGINT to foreground process group leader (in your case, bash process is the leader), but it doesn't pass to subshell process by default.

You can put trap "kill 0" SIGINT; on top of script, e.g.:

trap "kill 0" SIGINT;
mplayer -vo null foo.mkv 2>&1 | while read line; do echo "$line"; done

Or do this in one-liner command:

bash -c 'trap "kill 0" SIGINT; mplayer -vo null foo.mkv 2>&1 | while read line; do echo "$line"; done' 

Killing 0 sends the signal to all processes in the current process group. Credit.

When mplayer subshell terminated without SIGINT then its shell will not reset stty echoing. You can type stty echo(invisible in your case) and press Enter to enable back echoing visibility manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.