简体   繁体   中英

How to access environment variables in custom script extension script on Azure

I have created a RHEL Linux VM and installed Oracle JDK in it.

Post creation of VM, I am executing script using Custom Script extension in VM. Below is my script.

Myscript.sh

echo "$$ $JAVA_HOME $$" >> output.log
echo `env` >> output.log

output.log

$$ $$
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin PWD=/var/lib/waagent/custom-script/download/1 LANG=en_US.UTF-8 SHLVL=4 _=/usr/bin/env

When I ran the above script, I don't see JAVA_HOME is set. But I see JAVA_HOME value when I logged into the machine.

Update:-

During installation of Oracle JDK, I set JAVA_HOME in /etc/profile.

script1.sh

javaHomeValue=$(cat /etc/profile | grep JAVA_HOME | awk -F= '{print $2}')

echo " -- $javaHomeValue -- " >> output.log

output.log

-- /usr/java/jdk1.8.0_172-amd64 --

I am able to get JAVA_HOME using the above script1.sh

Is there any reason, Why JAVA_HOME is not set in environment variables during execution of Custom script?

For your issue, if you want to set JAVA_HOME as environment variable in file /etc/profile, you can add code export JAVA_HOME=/usr/java/jdk1.8.0_172-amd64 into /etc/profile. Then you can see the environment variable JAVA_HOME in a new terminal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM