Bash 脚本变量和参数传递

发布于 2024-12-04 01:04:54 字数 644 浏览 2 评论 0原文

我试图在 bash 脚本中将字符串变量传递给应用程序:

    # launch app for translator to verify
    DIR="$( cd "$( dirname "$0" )" && pwd )"

    langstr="'(English)'"
    case $language in
    "fr") 
        langstr="'(French)'";;
    esac

    echo $langstr
    #$DIR/../Debug/MyApp.app/Contents/MacOS/MyApp -AppleLanguages '(French)'
    $DIR/../Debug/MyApp.app/Contents/MacOS/MyApp -AppleLanguages $langstr

回显显示 $langstr 正是我所期望的:'(French)' 。带有硬编码语言参数的注释行工作正常(应用程序以法语启动),但是用带有 $langstr 变量的行替换它会以英语启动应用程序,这可能意味着它在某种程度上忽略了它。

我可能需要的是给自己上一堂关于 bash 变量使用的课,但我希望同时能在这里得到一个快速的答案。

I'm trying to pass a string variable to an application in a bash script:

    # launch app for translator to verify
    DIR="$( cd "$( dirname "$0" )" && pwd )"

    langstr="'(English)'"
    case $language in
    "fr") 
        langstr="'(French)'";;
    esac

    echo $langstr
    #$DIR/../Debug/MyApp.app/Contents/MacOS/MyApp -AppleLanguages '(French)'
    $DIR/../Debug/MyApp.app/Contents/MacOS/MyApp -AppleLanguages $langstr

The echo reveals that $langstr is what I expect it to be: '(French)'. The commented line with the hard coded language parameter works fine (app launches in French), but substituting that with the line with the $langstr variable launches the app in English which probably means that it ignored it some how.

What I probably need is to find myself a lesson on bash variable usage, but I was hoping to get a quick answer here in the meantime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧情勿念 2024-12-11 01:04:54

当您使用这样的变量传递参数时,单引号被视为参数值的一部分。因此,您的应用程序实际上得到的是字符串 '(French)',而它可能只需要 (French)。将变量赋值更改为 langstr="(French)"

When you pass the parameter using a variable like that, the single quotes are considered to be part of the parameter's value. So, your application gets literally the string '(French)', while it probably expects just (French). Change the variable assignment to langstr="(French)".

高冷爸爸 2024-12-11 01:04:54

为什么需要在变量中使用 cd 命令?就这样吧

langstr="(English)"
case $language in
"fr") 
    langstr="(French)";;
esac

echo $langstr
DIR=$(dirname "$0")
$DIR/../Debug/MyApp.app/Contents/MacOS/MyApp -AppleLanguages "$langstr"

Why do you need the cd command in a variable? Just do it as it is

langstr="(English)"
case $language in
"fr") 
    langstr="(French)";;
esac

echo $langstr
DIR=$(dirname "$0")
$DIR/../Debug/MyApp.app/Contents/MacOS/MyApp -AppleLanguages "$langstr"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文