0

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?

5
  • possible duplicate of Csh adding strings to an array, whitespace troubles Commented Dec 4, 2014 at 19:56
  • @EtanReisner Didn't see that question, but thanks for the link. I don't think it's quite a duplicate -- these are two different questions (albeit with similar underlying principles), so there are two different goals and two different answers. I'll try to adapt the answer given there again to this application. Commented Dec 4, 2014 at 19:59
  • The answer there specifically covers the foreach case too. Commented Dec 4, 2014 at 19:59
  • @EtanReisner Whoops - my fault. I was using the brace syntax: ${array}:q does not work. I'll add that as a comment and my answer as another option. This should be closed as a dupe. Thanks :) Commented Dec 4, 2014 at 20:04
  • Follow-up question: stackoverflow.com/q/27305142/1443496 Commented Dec 4, 2014 at 22:14

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.