Bash 脚本参数没有传递给函数?

Bash 脚本参数没有传递给函数?

我试图在调用 bash 脚本时将参数传递给该脚本中定义的函数。在我看来这是微不足道的,但实际上似乎更困难。

脚本是:

#!/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

我用以下命令调用脚本

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

并可以看到我的参数由该行回显echo "$@,但参数似乎没有传递给函数,因为程序执行时就好像没有给出参数一样。因此程序执行有效,但参数似乎没有被传递。

但是,如果我在 shell 中执行“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 -batch -x /folder/project.pts

上面的命令在我的 shell 中运行得很好。

我是否错误地逃避了某些事情?

编辑:bash -x 输出

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

答案1

我不保证这会起作用,但这里的问题是您没有正确构建路径。总路径应包含wine + Path to WineApp + FileToOpen.我将你的脚本划分为更多变量,以便这个概念有意义,但不一定有效......

#!/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

答案2

您的函数调用根本不使用参数。你的意思:

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 "$@"
}

(最后有"$@")?

相关内容