1

I wrote a bash script to download a specific java binary tar ball, untar it, and then configure the java version Since the java install tool requires manual interaction I'm using "expect" to "send" the answer to the command. I put the java binaries into "/opt"

curl -o /opt/jdk-8u201.tar.gz https://hostname
tar -zxvf /opt/jdk-8u201.tar.gz -C /opt
#!/bin/bash

yum -y install expect
EXPECT=$(which expect)
JAVA_HOME=/opt/jdk-8u201
update-alternatives --install /usr/bin/java java ${JAVA_HOME%/}/bin/java 2000
${EXPECT} <<EOD
spawn update-alternatives --config java
expect "Enter to keep the current selection\[\+\], or type selection number:"
send 3
EOD

When I perform the installation manually and enter: echo $JAVA_PATH it prints the information. If I do it in a shell script, it does not update the path.

Is there something I don't see? Thank you

Cheers, Roland

user2872898
  • 121
  • 1
  • 1
  • 8
  • How do you run the script? Can you print JAVA_PATH inside the script after updating and post the output of the script? – BlackPearl Apr 27 '19 at 08:29
  • 1
    Your script runs in a sub shell. A sub shell can only change the values of variables within itself and other shells it creates (for exported variables), it can't affect the calling shell unless invoked as `. script` or via `source` instead of just `script`. See https://stackoverflow.com/q/16618071/1745001 – Ed Morton Apr 27 '19 at 13:02
  • @BlackPearl thank you. While using a statement ```echo $JAVA_HOME``` I get the following error message: ```invalid command name "echo" while executing "echo /opt/jdk-8u201```, So, it seems that inside the expect call it knows what the ```$JAVA_PATH``` variable is but not afterwards, like what @Ed Morton refers to. – user2872898 Apr 27 '19 at 14:02
  • Yes. I overlooked it. You probably have to use `source`. – BlackPearl Apr 27 '19 at 14:11
  • do you use source within the expect block and what would you actually sourve, the java version? – user2872898 Apr 28 '19 at 13:33
  • On another side not, could I also export the currently installed java version as in ``` export JAVA_HOME=jdk-install-dir export PATH=$JAVA_HOME/bin:$PATH ``` Where the current jdk binaries are under ``` /usr/lib/jvm ``` – user2872898 Apr 28 '19 at 13:58
  • There are multiple java versions listed there and a bit difficult which one to export ``` java -> /etc/alternatives/java_sdk java-1.8.0 -> /etc/alternatives/java_sdk.1.8.0 java-1.8.0_openjdk -> /etc/alternatives/java_sdk.1.8.0_openjdk java-1.8.0_openjdk-1.8.0.111-1.b15.e7_2.x86_64 java-openjdk -> /etc/alternatives/java_sdk.1.8.0_openjdk jre jre-1.8.0 -> /etc/alternatives/jre-1.8.0 jre-1.8.0-openjdk -> /etc/alternatives/jre-1.8.0_openjdk jre-1.8.0-openjdk-1.8.0.111-1.b15.el7_2.x86_64 -> java-1.8.0-openjdk-1.8.0.111-1.b15.e7_2.x86_64 jre-opejdk -> /etc/alternatives/jre-openjdk ``` – user2872898 Apr 28 '19 at 13:59
  • So, would I export the current java version as in ``` export JAVA_HOME=java-1.8.0_openjdk-1.8.0.111-1.b15.e7_2.x86_64 export PATH=$JAVA_HOME/bin:$PATH – user2872898 Apr 28 '19 at 13:59
  • ahh, here is a very simple solution to the initial question, doesn't even require an expect block. ```echo 2 | alternatives --config java``` – user2872898 Apr 28 '19 at 14:07

0 Answers0