Search code examples
bashevalmplayer

Cannot execute a command with eval


I have a text file (test.txt) whose contents are:

mplayer -fs video.avi -vf mirror

If I execute:

eval "$(cat test.txt)"

I get:

 doesn't exist.or
Error parsing option on the command line: -vf
MPlayer 1.1-4.8 (C) 2000-2012 MPlayer Team

But if I execute:

mplayer -fs video.avi -vf mirror

the video will be played.

Why does eval (or mplayer?) fail in this case?

I ran:

$ file test.txt
test.txt: ASCII text, with CRLF line terminators

The text file seems to be created under Windows. I copied its contents and created a new file under Linux. Now it works.

But how to get it working with the original Windows file? Do I have to replace some characters?


Solution

  • Just use tr to strip off the windows CRLF file endings and convert it to UNIX line termination strings

    tr -d '\015' <file_with_DOS_endings >file_UNIX_endings
    

    To run your original command in with the above logic invoke an explicit sub-shell with the -c flag,

    bash -c "$(tr -d '\15\32' < "test.txt")"