Cannot execute a command with eval

1k views Asked by At

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?

2

There are 2 answers

4
Inian On BEST ANSWER

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")"
0
Jens On

Shells don't like files with Windows/DOS line endings. They treat the carriage return ('\r') as part of the last word.

Worse, when a carriage return is output as part of an error message, it places the cursor at the beginning of the line, causing previous message text to be overwritten. This explains the garbled message.

To run a shell script stored in a file, you simply make it executable (chmod +x test.txt), then ./test.txt. No need for eval.

You can convert DOS files to Unix files with

dos2unix test.txt

to remove the carriage returns.