用空格shell脚本传递参数 [英] Shell script pass arguments with spaces

查看:484
本文介绍了用空格shell脚本传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个shell脚本(比如SCRIPT1)传递到另一个参数。一些参数包含空格。所以,我包括在参数报价和传递给SCRIPT2之前,我赞同它。这是怎么回事,

I want to pass arguments from one shell script ( say script1 ) to another. Some of the arguments contain spaces. So I included quotes in the arguments and before passing to script2, I echoed it. Here is how it is,

echo $FL gives
-filelist "/Users/armv7/My build/normal/My build.LinkFilelist" -filelist "/Users/arm64/My build/normal/My build.LinkFilelist"

但是当我做

script2  -arch armv7 -arch arm64 -isysroot /Applications/blahblah/iPhoneOS8.1.sdk $FL

和在SCRIPT2如果我这样做,

and in the script2 if I do,

 for var in "$@"
  do
      echo "$var"
  done

我仍然获得

"-arch"
"armv7"
"-arch"
"arm64"
"isysroot"
"/Applications/blahblah/iPhoneOS8.1.sdk"
"-filelist"
""/Users/armv7/My"
"build/normal/My"            // I want all these 3 lines together
build.LinkFilelist"" 
"-filelist"
""/Users/arm64/My"
"build/normal/My"
build.LinkFilelist""

可有人请纠正我的错误?我应该怎么做才能提到的参数作为一个整体。

Can someone please correct my error ? What should I do to get the mentioned argument as a whole.

推荐答案

在一个变量的值嵌入引号没有做什么有用的。由于@Etan赖斯纳表示,请参阅 http://mywiki.wooledge.org/BashFAQ/050 。在这种情况下,最好的回答可能是佛罗里达存储作为数组,而不是一个普通的可变

Embedding quotes in a variable's value doesn't do anything useful. As @Etan Reisner said, refer to http://mywiki.wooledge.org/BashFAQ/050. In this case, the best answer is probably to store FL as an array, rather than a plain variable:

FL=(-filelist "/Users/armv7/My build/normal/My build.LinkFilelist" -filelist "/Users/arm64/My build/normal/My build.LinkFilelist")

注意引号不作为存储数组元素的一部分;相反,它们用于强制的路径被处理单个阵列元件,而不是由空格打破。然后,是指它与$ {FL [@]},这使得bash的对待每一个元素作为参数:

Note that the quotes aren't stored as part of the array elements; instead, they're used to force the paths to be treated single array elements, rather than broken up by the spaces. Then refer to it with "${FL[@]}", which makes bash treat each element as an argument:

script2 -arch armv7 -arch arm64 -isysroot /Applications/blahblah/iPhoneOS8.1.sdk "${FL[@]}"

这篇关于用空格shell脚本传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆