Will O'the Wisp ([info]wotw) wrote,
@ 2009-05-18 14:44:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Unix question
I have a unix script (called, say, doit) that contains
variables $1, $2, $3.

I also have a file (called, say, stringlist) that looks
like this:

a
b
c

I want to write a new script (called, say, newscript)
such that invoking the command "newscript stringlist"
is equivalent to invoking the command "doit a b c".

And of course if I have a file called otherstringlist
with contents

d
e
f

then "newscript otherstringlist" should be equivalent
to "doit d e f".

How do I do this?

(PS: In case it matters, I prefer a solution that works
under tcsh.)

(Double PS: What I'm currently doing is editing the file
stringlist to replace the line "a" with a sed command
that changes "$1" to "a" in doit, and likewise for all
other lines (i can co this with a 1,$s/^/.... and a
1,$s/$/ ...), then running the resulting script to create
a new script without any variables that I can just run. But
surely there's a better way?)



(6 comments) - (Post a new comment)


[info]qwrrty
2009-05-18 07:19 pm UTC (link)
Note: all quotes in this message are "backticks", the backwards single quote that appears next to the ESC key on most keyboards.

Running doit `cat stringlist` will invoke doit a b c. similarly, doit `cat otherstringlist` will invoke doit d e f. So you could write newscript to do this:

#!/bin/sh
argfile=$1

doit `cat $argfile`

to run doit with arguments from whatever file you name. It may not even be necessary to have newscript at all, depending on how easy it is for you just to run doit `cat foo`

(Reply to this) (Thread)


[info]dr_memory
2009-05-18 07:58 pm UTC (link)
One slight dissension: if any of the line in stringlist contain spaces (ie: multiple words), that won't quite work, e.g. if stringlist is:
foo
bar baz
moof

...then running doit `cat stringlist` will be equivilant to running doit foo bar baz (and moof gets ignored). So if you're feeling paranoid, you'd say:

doit `cat stringlist | sed -e 's/[^$]/"/g'`

...but of course THAT will get bolluxed up if any of the lines in stringlist contain their own quotation marks. If that's a worry, you might have to play around with whatever tcsh's equivalent of the input field separator is, or give up and use perl/python.

(Reply to this) (Parent)


[info]wotw
2009-05-18 09:12 pm UTC (link)
Thanks very much for this, though for a number of reasons related
to the idiosyncracies of my environment, I prefer [info]frobzwiththingz's
solution. I'm very grateful, though.

(Reply to this) (Parent)


[info]dr_memory
2009-05-18 07:33 pm UTC (link)
oops, ignore the pre-edit version of htis answer; I see I misread your question...

Edited at 2009-05-18 07:35 pm UTC

(Reply to this)


[info]frobzwiththingz
2009-05-18 07:52 pm UTC (link)
xargs -n 3 doit < argfile

will do it.


(Reply to this) (Thread)


[info]wotw
2009-05-18 09:12 pm UTC (link)
You are my hero.

(Reply to this) (Parent)


(6 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…