问题
Using Java (JSch API) I am trying to execute an Unix command on a remote machine. Now, to successfully execute this command I need to use all the environment variables already set on the remote box.
I can use export <variable>
command to set the variables on runtime. But as the number of such variables is quite large I am wondering if there is any better way to use the variables in runtime.
Can anyone help please or should I explain a bit more?
回答1:
You can save variables into a file:
set > /tmp/vars
echo "A=120" >> /tmp/vars
and then "import" the variables with dot in a script like this:
set -a
. /tmp/vars
mycommand
回答2:
The work around I found was to use
set | mycommand
Set will manually call the initialization process and it will add environment variables into the scope.
JSch calls bash with -c. This causes .bashrc not to be initialized inside of the bash scope. Set will read the .bashrc file and other config files.
来源:https://stackoverflow.com/questions/21107520/how-to-use-all-the-unix-environment-variables