views:

65

answers:

2

Hi guys,

more than a problem, this is a request for "another way to do this"

actually, if a want to use the result from a previous command I into another one, I use:

R1=$("cat somefile |  awk '{ print $1 }'" )
myScript -c $R1 -h123

then, a "better way"is:

myScript -c $("cat somefile |  awk '{ print $1 }'" ) -h123

but, what if I have to use several times the result, let's say: using several times $R1, well the 2 options: option 1

R1=$("cat somefile | awk '{ print $1}'")

myScript -c $R1 -h123 -x$R1

option 2

myScript -c $("cat somefile |  awk '{ print $1 }'" ) -h123 -x $("cat somefile |  awk '{ print $1 }'" )

do you know another way to "store" the result of a previous command/script and use it as a argument into another command/script?

thanks

A: 

Sure, there are other ways. They're just not better ways.

First, you could store the answer in a file, and then cat the contents of the file multiple times.

Second, you could pass the results to a bash function like:

callMyScript() {
   myScript -c "$1" -h123 -x "$1" 
}

invoked thusly:

callMyScript "$(awk '{ print $1; }' somefile)"

which is almost precisely identical to just saving off into a local variable.

So you're interested in using awk? You could have awk generate the line for you, and have bash run it:

eval $(awk '{ printf "myScript -c %s -h123 -x %s\n", $1, $1; }' somefile)

but now we're just getting silly, and even that's no different conceptually from simply saving off into a variable.

My advice: Use the variable.

Owen S.
A: 

Another not perfect way:

source script #1, then when script #2 runs all of the variables declared in #1 are available to #2.

#!/bin/ksh
.  myScript -c $("cat somefile |  awk '{ print $1 }'" ) -h123 
myScript2 

Your best choice is a wrapper script that keeps all your variables for you, as everyone else already noted.

In bash the source builtin command is the same as the 'dot'

jim mcnamara