tags:

views:

44

answers:

4

I have a proprietary command-line program that I want to call in a bash script. It has several options in a .conf file that are not available as command-line switches. I can point the program to any .conf file using a switch, -F.

I would rather not manage a .conf file separate from this script. Is there a way to create a temporary document to use a .conf file?

I tried the following:

echo setting=value|my_prog -F -

But it does not recognize the - as stdin.

+2  A: 

When writing this question, I figured it out and thought I would share:

exec 3< <(echo setting=value)
my_prog -F /dev/fd/3

It reads the #3 file descriptor and I don't need to manage any permissions or worry about deleting it when I'm done.

User1
+3  A: 

You can try /dev/stdin instead of -.

You can also use a here document:

my_prog -F /dev/stdin <<OPTS
opt1 arg1
opt2 arg2
OPTS

Finally, you can let bash allocate a file descriptor for you (if you need stdin for something else, for example):

my_prog -F <(cat <<OPTS
opt1 arg1
opt2 arg2
OPTS
)
derobert
I went for `my_prog -F <(echo setting=value)`. It worked and this is a simple one-liner. I always thought `<(echo setting=value)` meant go to stdin..nope.
User1
Also, I like this solution since I don't have to worry about if `fd=3` was used elsewhere like my answer.
User1
+1  A: 

It sounds like you want to do something like this:

#!/bin/bash

MYTMPFILE=/tmp/_myfilesettings.$$

cat <<-! > $MYTMPFILE
    somekey=somevalue
    someotherkey=somevalue
!
my_prog -F $MYTMPFILE
rm -f $MYTMPFILE

This uses what is known as a "here" document, in that all the contents between the "cat <<-!" up to ! is read in verbatim as stdin. The '-' in that basically tells the shell to remove all leading tabs.

You can use anything as the "To here" marker, e.g., this would work as well:

cat <<-EOF > somewhere
     stuff
     more stuff
EOF
Chris J
Actually the hyphen only tells the shell to remove contiguous leading tabs (not spaces). `Tab-tab-space-tab-foo` would become `space-tab-foo` and `space-space-bar` would become `space-space-bar`.
Dennis Williamson
That's a fair point. Edited accordingly :-)
Chris J
+2  A: 

You can use process substitution for this:

my_prog -F <(command-that-generates-config)

where command-that-generates-config can be something like echo setting=value or a function.

Bart Sas