Sourcing a script file in bash before starting an executable

Posted by abigagli on Stack Overflow See other posts from Stack Overflow or by abigagli
Published on 2010-04-09T10:48:29Z Indexed on 2010/04/09 10:53 UTC
Read the original article Hit count: 355

Filed under:
|
|

Hi, I'm trying to write a bash script that "wraps" whatever the user wants to invoke (and its parameters) sourcing a fixed file just before actually invoking it.

To clarify: I have a "ConfigureMyEnvironment.bash" script that must be sourced before starting certain executables, so I'd like to have a "LaunchInMyEnvironment.bash" script that you can use as in:

LaunchInMyEnvironment <whatever_executable_i_want_to_wrap> arg0 arg1 arg2

I tried the following LaunchInMyEnvironment.bash:

#!/usr/bin/bash
launchee="$@"
if [ -e ConfigureMyEnvironment.bash ];
     then source ConfigureMyEnvironment.bash;
fi

exec "$launchee"

where I have to use the "launchee" variable to save the $@ var because after executing source, $@ becomes empty.

Anyway, this doesn't work and fails as follows:

myhost $ LaunchInMyEnvironment my_executable -h
myhost $ /home/me/LaunchInMyEnvironment.bash: line 7: /home/bin/my_executable -h: No such file or directory
myhost $ /home/me/LaunchInMyEnvironment.bash: line 7: exec: /home/bin/my_executable -h: cannot execute: No such file or directory

That is, it seems like the "-h" parameter is being seen as part of the executable filename and not as a parameter... But it doesn't really make sense to me. I tried also to use $* instead of $@, but with no better outcoume.

What I'm doing wrong?

Andrea.

© Stack Overflow or respective owner

Related posts about bash

Related posts about wrapping