开发者

Bash script variables and parameter passing

开发者 https://www.devze.com 2023-04-04 01:46 出处:网络
I\'m trying to pass a string variable to an application in a bash script: # launch app for translator to verify

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 la开发者_JAVA技巧nguage 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.


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)".


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"
0

精彩评论

暂无评论...
验证码 换一张
取 消