pcmanfm arguements; bash

487 views Asked by At

I am using ubuntu, fluxbox, pcmanfm as filemanager, xmms2 as music player.
My goal: add songs to xmms2 playlist easily with pcmanfm.

I have this script that works for single files:

path= $1
if [ -d "$path" ]; then #if directory
    xmms2 radd "$path"
else
    if [ -e "$path" ]; then #if not directory, but file
        xmms2 add  "$path"
    fi
fi

I also want to have ability to add group of files
I mean select all of them and then rigth-click -> open with -> xmms2_add_script

I thougth that same code in loop should work (if pcmanfm passes just more then one argument):

args=("$@") 
for path in $args; do
    if [ -d "$path" ]; then
        xmms2 radd "$path"
    else
        if [ -e "$path" ]; then
            xmms2 add  "$path"
        fi
    fi
done

but it does not work.
(I know that there is some problem running for loop through filenames with whitespaces, so tried only non-whitespaced files.)

I tried loging the output adding this

echo date >> /home/me/output.txt
echo xmms2 radd "$path" >> /home/me/output.txt

in if statements. It seems that program is called only once when I try adding group of files.


Maybe someone knows how pcmanfm opens multiple files with some program?
(I guess other programs does it same way)
Or maybe someone just know how can I achieve my goal with a bash script?

Just in case: to add item to xmms2 playlist "xmms2 radd directory_name" or "xmms2 add file_name"
I have only basic understanding of bash so would be nice if answers wouldn't for expirienced bash programmers :)

1

There are 1 answers

0
Ian Kelling On BEST ANSWER

Change your for loop to this:

for path in "${args[@]}"; do

The for loop loops over its arguments, your args variable is an array, but bash only sees the first element when you do $args.

Greg's Bash FAQ/Wiki: How can I use array variables?

This is actually how I would write that to be more readable:

for x in "$@"; do
    [[ -d $x ]] && xmms2 radd "$x"
    [[ -f $x ]] && xmms2 add  "$x"
done