linux - pass filename(with space in the path) as argumant to a wine app in a bash script

07
2014-07
  • ToX

    I want to luanch an wine App with passing a file as argument. I've two file, with the first I don't have any problem but when there is a space in the path wine can't handle the address properly. here is these two files:

    /home/op/Doc/test.pdf

    /home/op/Doc/test vs space.pdf

    from the command line I can successfully launch the app with both files using this command:

    [/home/op@box ~]: wine "C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe" "z:Docs/test.pdf"
    
    [/home/op@box ~]: wine "C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe" "z:Docs/test vs space.pdf"
    

    but when I want to put this in a script (to luanch later with another program, ranger) I can't launch the program with test files. The first script works well with those which don't have any space in the address but the second doesn't work and just lunch the App without the pdf file opened:

    Script 1: work with no space case:

    #!/bin/bash
    Filename="z:${1//\//\\}"
    
    wine "C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe" $Filename 
    

    Script 2: Doesn't work in either of cases

    #!/bin/bash
    Filename="z:${1//\//\\}"
    Filename='"'$Filename'"'
    
    wine "C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe" $Filename 
    
    echo $Filename
    

    I'm can't get where is wrong with the script (in comparision to the commands issued manually in the shell). The second script doesn't open any type of file, with or without a space in address.

    p.s. there is also similar scripts here but they are suffer from the same problem i.e. they can't launch the pdf with a space in the address either.

  • Answers
  • psimon

    I see several typos, errors in your script which might prevent it from executing it correctly:

    #!/bin/bash
    Filename="z:${1//\//\\}"
    # Filename='"'$Filename'"'
    # you can just leave this line, the triple quoting is unnecessary anyway
    
    wine "C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe" "$Filename" # it's better to quote variables containing strings
    
    echo "$Filename" # the same here
    

  • Related Question

    Handle filename with spaces inside Bash-script
  • ifischer

    In my Bash-script i have to handle filenames with spaces. These are the important lines inside my script:

    mp3file="/media/d/Music/zz_Hardcore/Sampler/Punk-O-Rama\ Vol.5\ \[MP3PRO\]/01\ -\ Nofx\ -\ Pump\ up\ the\ Valium.mp3"
    echo "Command: mp3info -x `echo $mp3file`"
    mp3info -x `echo $mp3file`
    

    Unfortunately, the command does not work, because the filename is splitted:

    mp3info: invalid option -- '\'
    mp3info: invalid option -- '\'
    Error opening MP3: /media/d/Music/zz_Hardcore/Sampler/Punk-O-Rama\: No such file or directory
    Error opening MP3: Vol.5\: No such file or directory
    Error opening MP3: \[MP3PRO\]/01\: No such file or directory
    Error opening MP3: Nofx\: No such file or directory
    Error opening MP3: Pump\: No such file or directory
    Error opening MP3: up\: No such file or directory
    Error opening MP3: the\: No such file or directory
    Error opening MP3: Valium.mp3: No such file or directory
    

    I also tried to add a custom IFS as i read on some forums:

    SAVEIFS=$IFS
    IFS=$(echo -en "\n\b")
    # Script like above
    IFS=$SAVEIFS
    

    But this way, i'm getting the error

    Error opening MP3: /media/d/Music/zz_Hardcore/Sampler/Punk-O-Rama\ Vol.5\ \[MP3PRO\]/01\ -\ Nofx\ -\ Pump\ up\ the\ Valium.mp3: No such file or directory
    

    I tried quite a while now but i cannot get my script to work. What is strange is that if i'm running the same command that my script should create manually (echoing it inside my script) on the Shell, it actually works. But not inside my script. Any hints?


  • Related Answers
  • Cry Havok

    Wrap the variable names in quotes:

    mp3file="/media/music/Punk-O-Rama Vol.5 \[MP3PRO\]/01 - Nofx - Pump up the Valium.mp3"
    echo "Command: mp3info -x \"$mp3file\""
    mp3info -x "$mp3file"
    

    I also removed the superfluous echo commands and escaping that isn't necessary.