Csh adding strings to an array, whitespace troubles

孤人 提交于 2019-11-29 07:14:52

Any time you are expanding an entire array and want to keep its individual elements' identities intact, you need the :q (for "quoted") modifier on the expansion. Otherwise, as soon as you do something like set expansionCmdList=($expansionCmdList[*] "$newCmd"), all previous commands in the list are split out into their component words, each of which is now its own array element. Simple demonstration:

% set a = ( a "b c" d )
% echo $a[2]
b c
% set a = ( $a[*] e )
% echo $a[2]
b

Oops, you've messed up the array before you even get to your execution loop. Things go much better with :q:

% set a = ( a "b c" d )
% set a = ( $a:q e )
% echo $a[2]
b c

You need to use the same modifier in the for loop:

foreach exCmd ($expansionCmdList:q) 

Finally, `exCmd` tries to run a command literally named "exCmd", and then take its output and run that as a command. What you probably want to do is simply execute a command equal to the value of the variable. You will likely run into more whitespace woes here, and you can't solve them by making each command an array since csh doesn't support arrays of arrays. Fair warning. But if the commands don't have any quotation needs, this will work:

  $exCmd
Sean Allred

Mark's solution is clearly superior for most applications, but there is another option. Instead of using foreach directly, get the size of the array and iterate through the sequence:

set BUILD_MATRIX = ( "makefile.make:make --jobs --makefile=makefile.make" \
                     "Makefile:make --jobs --makefile=Makefile" \
                     "build.xml:ant" )

foreach i ( `seq ${#BUILD_MATRIX}` )
  echo $i
  echo $BUILD_MATRIX[$i]
end

(copied from Accessing array elements with spaces in TCSH)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!