Accessing array elements with spaces in TCSH

…衆ロ難τιáo~ 提交于 2019-12-20 07:14:52

问题


I'm trying to create a small little convenience script for our team. Unfortunately, our entire build environment is based on tcsh for historical reasons. In the following script, each element of BUILD_MATRIX is a : delimited pair. I need to separate out each part of this pair for processing, but the array malfunctions for some reason.

#!/bin/tcsh

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

foreach pair ( ${BUILD_MATRIX} )
  echo "pair: ${pair}"
end

gives

pair: makefile.make:make
pair: --jobs
pair: --makefile=makefile.make
pair: Makefile:make
pair: --jobs
pair: --makefile=Makefile
pair: build.xml:ant

As you can see, the array is split on spaces -- completely reasonable, but not what is desired. How can I get pair=makefile.make:make --jobs --makefile=makefile.make?


回答1:


Using the linked duplicate, I was able to find a complete answer:

foreach pair ( $BUILD_MATRIX:q )
  set candidate = `echo $pair | sed 's/\([^:]*\):\(.*\)/\1/'`
  set command   = `echo $pair | sed 's/\([^:]*\):\(.*\)/\2/'`
  echo "pair: ${pair}"
  echo "candidate: ${candidate}"
  echo "command: ${command}"
end


来源:https://stackoverflow.com/questions/27302603/accessing-array-elements-with-spaces-in-tcsh

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