4

Building some software from source and there are a lot of configure switches.

./configure --enable-ftp --with-gettext ...

I have all the switches I want in a text file separated by new lines, e.g.:

--enable-ftp
--with-gettext
etc...

Is there a way I can pass the text file as an argument to configure to set the options? Or some other way to cut and paste the options that I have?

EDIT

I am using $HOME variable in my switches so any solution should accommodate that.

7
  • Sure: ./configure `cat file` . You might also want to look at Comfigure, it basically remembers configure options from previous runs for the same package. Jul 8, 2016 at 19:48
  • 1
    If you have already done the hard part of putting all the switches in a text file, just make it an executable shell script by adding a shebang line and run that.
    – Munir
    Jul 8, 2016 at 19:51
  • @Munir From the question: ... separated by new lines ... Jul 8, 2016 at 19:53
  • @SatoKatsura add a \ at the end of the lines...I mean you are going through the pain of typing out all the switches in a file, why not just make that an executable.
    – Munir
    Jul 8, 2016 at 19:54
  • @SatoKatsura if this is an answer I hope you make it an official answer because I like to see how the community votes for the preferred answer so I can learn the recommended way to do it and the pros/cons of each way
    – User
    Jul 8, 2016 at 20:11

2 Answers 2

2

To expand $HOME in your file you could use envsubst first (be aware this will expand any env variable). Then you could read the result into an array e.g. with zsh

args=(${(f)"$(< <(envsubst <infile))"})

or with bash

readarray -t args < <(envsubst <infile)

and then run

./configure "${args[@]}"

Alternatively, you could use tr to format the result as a single line of options preceded by ./configure, and pipe that to sh:

{ printf %s './configure '; tr '\n' ' ' <infile; } | sh
7
  • ./configure `cat file` works as well, I've been doing that forever. :) Jul 8, 2016 at 19:50
  • @SatoKatsura - it works in this case but I never use command `cat file` Jul 8, 2016 at 20:11
  • I'm using that a lot when I'm editing the names of my music collection. There, it's quite useful. Just about anywhere else, it's dangerous. Jul 8, 2016 at 20:21
  • @don_crissti what's wrong with cat file here?
    – User
    Jul 8, 2016 at 20:47
  • 1
    @User One side-effect of cat is that the shell will expand things like * or $VAR in your file, while my xargs approach will not. That could be a positive or a negative.
    – Fox
    Jul 8, 2016 at 21:04
2

One way to do this is to use xargs, which turns whitespace-separated strings on stdin into command-line arguments. If your file is called switches, this would look like:

xargs ./configure < switches

This will not expand things like * or $VAR. If (as in your edit) you want these to be expanded, then there are a few approaches. We can simply create a command and pass it to sh:

xargs printf '%s ' ./configure < switches | sh

Or, if you have envsubst, this will expand variables (like ${HOME}) but not file globs (*):

envsubst < switches | xargs ./configure
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .