Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to exec a binary and pass it args from a variable?

Status
Not open for further replies.

Fermulator

Programmer
Mar 16, 2007
10
0
0
CA
Having trouble getting exec to work with a dynamic set of arguments for the binary it's execing.

Example:
Code:
exec tar -cvf /tmp/MYFILE.tar /dir/to/tar

This works, and the tar file is created.

However, what if the 'arguments' passed into tar might change based on version?

Code:
# If tar is newer, use this syntax:
set tarArgs "-X /tmp/tar-exclude-file -cvvf /tmp/MYFILE.tar /dir/to/tar"
# If tar is older, use this syntax:
set tarArgs "-Xcvf /tmp/tar-exclude-file /tmp/MYFILE.tar /dir/to/tar"
# Now do the tar creation
exec tar $tarArgs

Always results in "Reason: tar: ... ... : No such file or directory"

Any ideas how to accomplish this?

Thanks!
 
I don't see anything wrong but just for kicks, try:
exec [red]"[/red]tar $tarArgs[red]"[/red]

_________________
Bob Rashkin
 
Trying:
Code:
exec "tar $tarArgs"

Results in:

ERROR: Could not complete tar operation
Reason: couldn't execute "tar -Xcvf /tmp/tar-exclude-file /tmp/MYFILE.tar /dir/to/tar": no such file or directory
 
I'm not familiar with TAR syntax, but I suppose you're sure that /tmp/tar-exclude-file exists?

_________________
Bob Rashkin
 
Yes I'm positive it exists.

I think the problem is that the 'exec' command typically works as:
Code:
exec myexecutable arg0 arg1 arg2

However, when the args are stored as a variable:
Code:
set args "args0 args1 args2"
exec myexecutable $args
it tries to pass "myexecutable" the args as an ENTIRE string instead of individual arguments.

I'm not sure how to 'unstringify' those args so that it's passed to myexecutable properly.
 
Try: exec tar [subst $tarArgs]

_________________
Bob Rashkin
 
Still nothing:

I'll give specific details here:

Code:
exec tar -X /tmp/tar-exclude -cvvf /tmp/myarchive.tar /dir/to/tar

Works.

Code:
set tarArgs "-X /tmp/tar-exclude -cvvf $tmp/myarchive.tar $/dir/to/tar"
exec tar $tarArgs

ERROR: Could not complete tar operation
Reason: tar: /tmp/tar-exclude -cvvf /tmp/myarchive.tar /dir/to/tar: No such file or directory

Trying with
Code:
set tarArgs "-X /tmp/tar-exclude -cvvf $tmp/myarchive.tar $/dir/to/tar"
exec tar [subst $tarArgs]

results in the same error message.

Notice though that the error DOES NOT include the "-X" ... so strange.
 
PS: That error message is slightly massaged.

The whole "ERROR: Could not compelete tar operation" and "Reason: " part are generated by my error catch proc.

The "tar: /tmp/tar-exclude -cvvf /tmp/myarchive.tar /dir/to/tar: No such file or directory" is the raw output from tar failing.
 
Well, I'm stumped. I don't see why it shouldn't work. I don't have a UNIX machine available to play with so I can't really try anything myself.

What happens if you build up the statement incrementally:

Code:
set strCmd "exec "
append strCmd "tar "
append strCmd "-X <and so on>
   ...
eval $strCmd

_________________
Bob Rashkin
 
HA! You ALMOST had it BONG!

Your idea of using "append" had no affect, however, the idea of placing EXEC within the string worked!

Code:
set strCmd "exec tar -X /tmp/tar-exclude -cvvf /tmp/myarchive.tar /dir/to/tar"
eval $strCmd

This is great. Thanks so much both of your for your help. (Obviously the solution isn't ideal, however it is an acceptable workaround.)

I would be interested to know however how to DO what I was trying to do....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top