3

What I want to do: Start my script with a list of files as arguments. From within my script I want to start xterm that starts mplayer with these files.

I tried several things. I call my script like this:

myscript.sh *

The files are named like

"aaa AAA"
"bbb BBB"
"ccc DDD"

(i.e. filenames with spaces) and are playable sounds, music or videos with mplayer.

This is what I tried

All of the following commands were written inside my script!

I tried:

  1. The following works. Files are printed.

    ls -l $@
    
  2. The following works. Files are printed.

    IFS='\n'
    ls -l $*
    
  3. The following works. Files are printed. Strangely I do not need the IFS='\n' here. Why?

    xterm -e 'ls -l $*; read'
    
  4. This does not find the files.

    FILES=$@
    ls -l $FILES
    
  5. This does not find the files. Here all filenames are treated as one long filename. (???)

    IFS='\n'
    FILES=$@
    ls -l $FILES
    
  6. This does not find the files.

    export FILES=$@
    ls -l $FILES
    
  7. But the following works. Files are printed.

    IFS='\n'
    export FILES=$@
    ls -l $FILES
    

Now with mplayer.

  1. This works.

    IFS='\n'
    export FILES=$@
    mplayer $FILES
    
  2. This does not work. But why does number 2 for ls -l work then? Here mplayer exits printing it’s help as if I just start mplayer with no arguments.

    xterm -e 'mplayer $*; read'
    
  3. This does not work. It fails for all the files with spaces in the filename.

    IFS='\n'
    FILES=$@
    xterm -e 'mplayer $FILES; read'
    

Question: How can I start mplayer from its own xterm with the files saved in the $FILES variable?

1 Answer 1

2

If you want to pass the arguments to your script to mplayer, in an xterm, this should work:

#!/bin/sh
xterm -e mplayer "$@"

This works because of how "$@" expands: one parameter per item. xterm does not attempt to interpret the arguments to the command in any way; it just passes them through.

If you need actually put your file list in a variable, your best bet is to use an array variable. For example:

#!/bin/bash
declare -a FILES
for f; do
    if [ -r "$f" ]; then
        FILES+=("$f")
    fi
done

xterm -e mplayer "${FILES[@]}"

The above filters the files to only pass ones which are readable. This is just an example, normally you'd not do this type of filtering. Also, typically, I'd use a lowercase variable name, unless I were planning on exporting it.

PS: Depending on what you're doing, are you aware of mplayer's -slave mode?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .