简体   繁体   English

Shell脚本使用空格传递参数

[英]Shell script pass arguments with spaces

I want to pass arguments from one shell script ( say script1 ) to another. 我想将参数从一个shell脚本(例如script1)传递给另一个。 Some of the arguments contain spaces. 一些参数包含空格。 So I included quotes in the arguments and before passing to script2, I echoed it. 因此,我在参数中加入了引号,并在传递给script2之前回显了它。 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"

But when I do 但是当我这样做

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

and in the script2 if I do, 然后在script2中,

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

I still get 我仍然得到

"-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. 我应该怎么做才能整体上提到上述论点。

Embedding quotes in a variable's value doesn't do anything useful. 将引号嵌入变量的值没有任何用处。 As @Etan Reisner said, refer to http://mywiki.wooledge.org/BashFAQ/050 . 正如@Etan Reisner所说,请参阅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存储为数组,而不是普通变量:

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

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: 然后用"${FL[@]}"引用它,这使bash将每个元素都当作一个参数:

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

1- Use the following (put "" around FL): 1-使用以下内容(在FL周围加上“”):

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

2- Then inside your script2 use (to extract the variable based on the format that you are aware of): 2-然后在您的script2内部使用(根据您知道的格式提取变量):

for arg; do # default for a for loop is to iterate over "$@"
   case $arg in
    '-filelist'*) input=${arg} ;;
      esac
done

3- Now you can break the input parameter to whatever format you want using awk . 3-现在,您可以使用awk将输入参数分解为所需的任何格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM