Accessing array elements with spaces in TCSH

2019-03-06 01:12发布

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条回答
The star\"
2楼-- · 2019-03-06 01:52

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
查看更多
登录 后发表回答