0

I'm trying to pass the arguments when invoking a bash script to a function defined in that script. In my head this is trivial but in practice seems more difficult.

Script is:

#!/bin/bash

function run_wine
{
    WINEPREFIX=/disk1/.wine-ptgui WINEDLLOVERRIDES=mscoree=d /software/wine/1.7.42/linux.centos6.i386/bin/wine /disk1/.wine-ptgui/drive_c/Program\ Files/PTGui/PTGui.exe "$@"
}

# Check if we already have the wineprefix installed
if [ -d /disk1/.wine-ptgui ]; then
    prefixExist=1
    echo "$@"
    run_wine "$@" &
    sleep 5
    exit 0
else
    echo "no wineprefix"
    exit 1
fi

I invoke the script with

./ptgui -batch -x /folder/project.pts

and can see my arguments echoed by the line echo "$@ but the arguments dont appear to be passed to the function, as the program exectutes as if no arguments had been given. So the program execution works, but the arguments don't appear to be passed.

However if i execute in the shell the command called by the 'run_wine' function with arguments, the program launches as I would expect, ie -

$ WINEPREFIX=/disk1/.wine-ptgui WINEDLLOVERRIDES=mscoree=d /software/wine/1.7.42/linux.centos6.i386/bin/wine /disk1/.wine-ptgui/drive_c/Program\ Files/PTGui/PTGui.exe -batch -x /folder/project.pts

The above command in my shell works just fine.

Am I escaping something incorrectly?

Edit: bash -x output

bryce-e@aw42e:dev$bash -x !!
bash -x ./ptgui -batch -x /folder/project.pts
+ '[' -d /disk1/.wine-ptgui ']'
+ prefixExist=1
+ echo -batch -x /folder/project.pts
-batch -x /folder/project.pts
+ sleep 5
+ run_wine
+ WINEPREFIX=/disk1/.wine-ptgui
+ WINEDLLOVERRIDES=mscoree=d
+ /software/wine/1.7.42/linux.centos6.i386/bin/wine '/disk1/.wine-ptgui/drive_c/Program Files/PTGui/PTGui.exe'
{{snipping out some wine messages here}}
+ exit 0

closed as off-topic by muru, cuonglm, jasonwryan, mdpc, Archemar Aug 7 '15 at 8:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – muru, cuonglm, jasonwryan, mdpc, Archemar
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please Read Section 3.2 - How to Run Wine. I believe you're missing the escaped backslash, ie c:\\myapps\\foo.exe – eyoung100 Aug 6 '15 at 22:59
  • no, because the application launches fine, its just that the arguments arent being passed – Party Time Aug 6 '15 at 23:05
  • But the Path is an internal argument... "c:\\Program Files\\PTGui\\PTGui.exe" + the @. Properly escaping the path makes this easier to debug, ie the @ contains the file to open, but not the app to open it with, which is what everyone writing Answers is telling you. – eyoung100 Aug 6 '15 at 23:10
  • @eyoung100 in the document you linked it quite clearly says at the bottom that the way I call the windows binary is fine. – Party Time Aug 6 '15 at 23:25
  • Add echo ${WINEPREFIX} and echo ${WINEDLLOVERRIDES} as the last lines of your function. What gets echo'ed? When those echo correctly, add a Variable EXEPATH, then pass the entire Path + your options contained in the @ array. – eyoung100 Aug 6 '15 at 23:27
2

I am not guaranteeing this will work, but the issue here is that you are not constructing the path properly. The Total Path Should contain wine + Path to WineApp + FileToOpen. I divided your script into more Variables so that the concept makes sense, not necessarily working...

#!/bin/bash

function set_wine_environment
{
    WINEPREFIX='/disk1/.wine-ptgui' 
    WINEDLLOVERRIDES='mscoree=d'
    PATHTOWINE='/software/wine/1.7.42/linux.centos6.i386/bin/wine' 
    PATHTOWINEAPP="/disk1/.wine-ptgui/drive_c/Program\ Files/PTGui/PTGui.exe" 
}

# Check if we already have the wineprefix installed
if [ -d /disk1/.wine-ptgui ]; then
    prefixExist=1
    echo "$@"
    set_wine_environment
    # The path is now fully constructed here
    # instead of inside the function, using
    # string expansion. 
    ${PATHTOWINE} ${PATHTOWINEAPP} "$@"
    sleep 5
    exit 0
else
    echo "no wineprefix"
    exit 1
fi
  • 1
    This almost works (needed to doublequote the PATHTOWINEAPP for proper string expansion) It functionally achieves what I want, even though it doesn't really answer my original question of why passing the command line arguments ($1 $2 etc) to the function don't work. Thanks! – Party Time Aug 7 '15 at 0:16
  • @PartyTime I'll double quote my code per your comment, and try to explain what happened. Your positional array was correct, but your path was improperly constructed. – eyoung100 Aug 7 '15 at 2:17
2

Your function call makes no use of the arguments at all. Did you mean:

function run_wine
{
    WINEPREFIX=/disk1/.wine-ptgui \
      WINEDLLOVERRIDES=mscoree=d \
      /software/wine/1.7.42/linux.centos6.i386/bin/wine \
      /disk1/.wine-ptgui/drive_c/Program\ Files/PTGui/PTGui.exe "$@"
}

(with the "$@" at the end)?

  • when i call the function after defining it, i use run_wine "$@". As an aside, I have tried what you suggested by putting the "$@" at the end of the function definition itself, and the problem remains. – Party Time Aug 6 '15 at 22:48
  • @PartyTime ah, but using run_wine "$@" is meaningless if you don't use the arguments anywhere inside the function. Do you think they get automatically appended to all statements inside the function? And what do you mean, at end of the function definition? Update your question with the changes you made. – muru Aug 6 '15 at 23:03
  • I see, I have edited the original to reflect the $@ being used inside the function. The problem remains. – Party Time Aug 6 '15 at 23:15
  • @PartyTime run your script with bash -x: bash -x ./ptgui -batch -x /folder/project.pts. Add the output to your question. – muru Aug 6 '15 at 23:21
  • added...oddly it seems to be totally ignored? – Party Time Aug 6 '15 at 23:42

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