Bash 脚本变量和参数传递
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用这样的变量传递参数时,单引号被视为参数值的一部分。因此,您的应用程序实际上得到的是字符串
'(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 tolangstr="(French)"
.为什么需要在变量中使用 cd 命令?就这样吧
Why do you need the cd command in a variable? Just do it as it is