问题
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