Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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