Get the App
SLTechnology News&Howtos  ›  Development  › 

How to configure Shell script to automatically install JDK in Linux system

Shulou Source: shulou.com Published: 2022-06-03 09:20:58 09月17日 Update

This article mainly introduces "how to configure Shell script to automatically install JDK in Linux system". In daily operation, I believe many people have doubts about how to configure Shell script to automatically install JDK in Linux system. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "how to configure Shell script to automatically install JDK in Linux system". Next, please follow the editor to study!

A: the machine on which this script runs, Linux

B: the machine to be installed with JDK, Linux

First, on machine A where the script is running, make sure that ssh can log in without a password to machine B where you want to install jdk, and then you can run this script on A:

The code is as follows:

$. / IP of install-jdk.sh B

Or:

The code is as follows:

$. / install-jdk.sh "B's IP"JDK's URI"

You can install JDK on machine B. The tar package used by jdk requires users to set their own DEFAULT_JDK_SRC=? It is guaranteed that it can be obtained by wget. Here is the full script:

The code is as follows:

#! / bin/bash

#

# @ file

# install-jdk.sh

#

# @ date

# 2013-12-19

#

# @ author

# cheungmine

#

# @ version

# 0.0.1pre

#

# @ usage:

#. / install-jdk.sh 192.168.122.206

#

#

. Common.sh

# *

# install_jdk

# install jdk on machine: / usr/local/lib

#

# Parameters:

# machine-root@ipaddr

# jdkUri-uri for fetching tarball

#

# Example:

#

# install_jdk root@192.168.122.206 ftp://vm-ftp/pub/tarball/jdk-7u67-linux-x64.tar.gz

#

# *

. Common.sh

# YOU MIGHT CHANGE BELOW LINE TO GET YOUR JDK TARBALL:

DEFAULT_JDK_SRC= "ftp://vm-ftp/pub/tarball/jdk-7u67-linux-x64.tar.gz"

# DO NOT CHANGE BELOW TWO LINES:

INSTALL_DIR= "/ usr/local/lib/java"

LOCAL_DIR= ". / .tmp"

Function install_jdk () {

Echo-e "install jdk on machine: $1"

Local DEST_LOGIN=$1

Local JDK_URI=$2

Local TAR=$ (basename $JDK_URI)

Echo-e "jdk:'$JDK_URI'"

Wget-c $JDK_URI-P $LOCAL_DIR-O $LOCAL_DIR/$TAR

$(is_empty_dir "$LOCAL_DIR/jdk_untar")

Local ret=$?

Case $ret in

$DIR_NOT_EXISTED)

Mkdir-p $LOCAL_DIR/jdk_untar

$DIR_IS_EMPTY)

$DIR_NOT_EMPTY)

Rm-rf $LOCAL_DIR/jdk_untar/*

*)

Exit $ERR_FATAL_ERROR

Esac

# untar to jdk_untar

Tar-zxf $LOCAL_DIR/$TAR-C $LOCAL_DIR/jdk_untar

$(is_empty_dir "$LOCAL_DIR/jdk_untar")

Local ret=$?

If ["$ret"-eq "$DIR_NOT_EMPTY"]; then

Local jdk_home= `ls $LOCAL_DIR/jdk_untar 2 > / dev/ null`

Echo $jdk_home

Else

Exit $ERR_FATAL_ERROR

Fi

Echo-e "create folder on: $DEST_LOGIN:$INSTALL_DIR"

Local ret= `ssh $DEST_LOGIN "mkdir $INSTALL_DIR" `

Echo-e "copy $jdk_home/ to: $DEST_LOGIN:$INSTALL_DIR/"

Local ret= `scp-r $LOCAL_DIR/jdk_untar/$jdk_home $DEST_LOGIN:$INSTALL_ Dir`

# remove local tar

Rm-rf $LOCAL_DIR/jdk_untar

Local DEST_JAVA_HOME=$INSTALL_DIR/$jdk_home

Echo-e "remove old settings for install_jdk in / etc/profile"

Echo-e "set / etc/profile: JAVA_HOME=$DEST_JAVA_HOME"

Local ret= `ssh $DEST_LOGIN "echo'> > / etc/profile" `

Local ret= `ssh $DEST_LOGIN "echo'#! {{install_jdk@hgdb.net== >'> > / etc/profile" `

Local ret= `ssh $DEST_LOGIN "echo 'export JAVA_HOME=$DEST_JAVA_HOME' > > / etc/profile" `

Local ret= `ssh $DEST_LOGIN "echo 'export CLASSPATH=.:\\ $JAVA_HOME/lib/tools.jar:\\ $JAVA_HOME/lib/dt.jar' > > / etc/profile" `

Local ret= `ssh $DEST_LOGIN "echo 'export PATH=\\ $JAVA_HOME/bin:\\ $JAVA_HOME/jre/bin:\\ $PATH' > > / etc/profile" `

Local ret= `ssh $DEST_LOGIN "echo'#!

Tags: Scripts machines systems configuration learning code running more help utility next content passwords articles methods theories users knowledge articles websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info vpn MySQL Redmi