java: 批量添加文件xx.jar到环境变量CLASSPATH中(linuxos-shell脚本)
一、版本1
1、脚本内容:/etc/profile
[root@openeuler libs]# cat /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`/usr/bin/id -u`
UID=`/usr/bin/id -ru`
fi
USER="`/usr/bin/id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi
HOSTNAME=$(/usr/bin/hostnamectl --transient 2>/dev/null) || \
HOSTNAME=$(/usr/bin/hostname 2>/dev/null) || \
HOSTNAME=$(/usr/bin/uname -n)
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i
unset -f pathmunge
# Source global bash config, when interactive but not posix or sh mode
if test "$BASH" &&\
test -z "$POSIXLY_CORRECT" &&\
test "${0#-}" != sh &&\
test -r /etc/bashrc
then
# Bash login shells run only /etc/profile
# Bash non-login shells run only /etc/bashrc
# Check for double sourcing is done in /etc/bashrc.
. /etc/bashrc
fi
# david 2022-04-12
export JAVA_HOME=/usr/local/java/jdk17
export CLASSPATH=.:$JAVA_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:/root/tools
# 去除PATH中的重复值
export PATH=$(echo $PATH | tr ':' '\n' | sort | uniq | tr -s '\n' ':' | sed 's/:$//g')
[root@openeuler libs]#
2、脚本内容:load-java-jar
[root@openeuler libs]# cat load-java-jar
#!/usr/bin/bash
# 脚本名称: load-java-jar
#
#
# 脚本功能: 将特定路径下的所有的xx.jar包,添加到环境变量"CLASSPATH"中。
# 这种添加"xx.jar"包的方法,是临时起作用;
#
# 每次电脑重启或者使用命令"source /etc/profile",恢复系统默认配置。
# 在这里,自己想恢复到哪个配置就选择哪个配置文件,不必强行使用
# 文件“/etc/profile”。
#
# 若想重新生效,需要重新运行脚本“load-java-jar”。
#
#
# 脚本运行方法: [root@openeuler libs]# source load-java-jar
#
#
# 脚本环境信息:
# [root@openeuler libs]# uname -a
# Linux openeuler 5.10.0-60.18.0.50.oe2203.x86_64 #1 SMP Wed Mar 30 03:12:24 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
# [root@openeuler libs]# java -version
# java version "17.0.1" 2021-10-19 LTS
# Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
# Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
# [root@openeuler libs]#
# 这里是恢复系统默认配置;“/etc/profile”文件是可选项,你可以选择自己想要恢复的配置文件。
# 防止CLASSPATH多次重复设置;因为"CLASSPATH变量存在递归定义( CLASSPATH=${CLASSPATH}${jar_path} )",会导致CLASSPATH重复设置。
source /etc/profile
# 注意:插件使用者必须配置这个目录。jar_files=$( ls /绝对路径/*.jar )
# 设置xxx.jar文件所在的目录;注意这里要设置成为“绝对路径”。
jar_files=$( ls /root/user/lidawei/tmp/libs/*.jar )
# jar_path: 用于存储所有的xx.jar 文件的绝对路径。
jar_path=""
# 拼接字符串,并将这个字符串存储在变量“jar_path”之中
for x in ${jar_files}
do
jar_path=${jar_path}":"${x}
done
# 设置环境变量
export CLASSPATH=${CLASSPATH}${jar_path}
echo "CLASSPATH_value := " ${CLASSPATH}
3、脚本应用:load-java-jar
[root@openeuler libs]# uname -a
Linux openeuler 5.10.0-60.18.0.50.oe2203.x86_64 #1 SMP Wed Mar 30 03:12:24 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[root@openeuler libs]# java --version
java 17.0.1 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
[root@openeuler libs]# pwd
/root/user/lidawei/tmp/libs
[root@openeuler libs]# ls
load-java-jar temp.jar test.sh
[root@openeuler libs]# env
SHELL=/bin/bash
HISTCONTROL=ignoredups
HISTSIZE=1000
HOSTNAME=openeuler
JAVA_HOME=/usr/local/java/jdk17
PWD=/root/user/lidawei/tmp/libs
LOGNAME=root
MOTD_SHOWN=pam
HOME=/root
LANG=en_US.UTF-8
SSH_CONNECTION=10.10.10.12 49440 10.10.10.16 22
SELINUX_ROLE_REQUESTED=
TERM=xterm-256color
USER=root
SELINUX_USE_CURRENT_RANGE=
SHLVL=1
CLASSPATH=.:/usr/local/java/jdk17/lib
SSH_CLIENT=10.10.10.12 49440 22
PATH=/root/bin:/root/tools:/usr/bin:/usr/local/bin:/usr/local/java/jdk17/bin:/usr/local/sbin:/usr/sbin
SELINUX_LEVEL_REQUESTED=
MAIL=/var/spool/mail/root
SSH_TTY=/dev/pts/0
OLDPWD=/root
_=/usr/bin/env
[root@openeuler libs]# source load-java-jar
Welcome to 5.10.0-60.18.0.50.oe2203.x86_64
System information as of time: Tue Apr 19 09:29:10 PM CST 2022
System load: 0.11
Processes: 225
Memory used: 8.5%
Swap used: 0%
Usage On: 7%
IP address: 10.10.10.16
IP address: 192.168.122.1
Users online: 1
CLASSPATH_value := .:/usr/local/java/jdk17/lib:/root/user/lidawei/tmp/libs/temp.jar
[root@openeuler libs]# env
SHELL=/bin/bash
HISTCONTROL=ignoredups
HISTSIZE=1000
HOSTNAME=openeuler
JAVA_HOME=/usr/local/java/jdk17
PWD=/root/user/lidawei/tmp/libs
LOGNAME=root
MOTD_SHOWN=pam
HOME=/root
LANG=en_US.UTF-8
SSH_CONNECTION=10.10.10.12 49440 10.10.10.16 22
SELINUX_ROLE_REQUESTED=
TERM=xterm-256color
USER=root
SELINUX_USE_CURRENT_RANGE=
SHLVL=1
CLASSPATH=.:/usr/local/java/jdk17/lib:/root/user/lidawei/tmp/libs/temp.jar
SSH_CLIENT=10.10.10.12 49440 22
PATH=/root/bin:/root/tools:/usr/bin:/usr/local/bin:/usr/local/java/jdk17/bin:/usr/local/sbin:/usr/sbin
SELINUX_LEVEL_REQUESTED=
MAIL=/var/spool/mail/root
SSH_TTY=/dev/pts/0
OLDPWD=/root
_=/usr/bin/env
[root@openeuler libs]#
[root@openeuler libs]#
二、版本2
1、脚本内容:load-java-jar
[root@openeuler libs]# cat load-java-jar
#!/usr/bin/bash
# 脚本名称: load-java-jar
#
#
# 脚本功能: 将当前路径下的所有的xx.jar包,添加到环境变量"CLASSPATH"中。
# 这种添加"xx.jar"包的方法,是临时起作用;
#
# 每次电脑重启或者使用命令"source /etc/profile",恢复系统默认配置。
# 在这里,自己想恢复到哪个配置就选择哪个配置文件,不必强行使用
# 文件“/etc/profile”。
#
# 若想重新生效,需要重新运行脚本“load-java-jar”。
#
# version = 2.0
#
#
# 脚本运行方法: [root@openeuler libs]# source load-java-jar
#
#
# 脚本环境信息:
# [root@openeuler libs]# uname -a
# Linux openeuler 5.10.0-60.18.0.50.oe2203.x86_64 #1 SMP Wed Mar 30 03:12:24 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
# [root@openeuler libs]# java -version
# java version "17.0.1" 2021-10-19 LTS
# Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
# Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
# [root@openeuler libs]#
# 这里是恢复系统默认配置;“/etc/profile”文件是可选项,你可以选择自己想要恢复的配置文件。
# 防止CLASSPATH多次重复设置;因为"CLASSPATH变量存在递归定义( CLASSPATH=${CLASSPATH}${jar_path} )",会导致CLASSPATH重复设置。
source /etc/profile
# 注意:插件使用者必须配置这个目录。jar_files=$( ls /绝对路径/*.jar )
# jar_files=$( ls /root/user/lidawei/tmp/libs/*.jar )
current_path=$(pwd)
echo current_path = ${current_path}
jar_files=$( ls ${current_path}/*.jar )
# jar_path: 用于存储所有的xx.jar 文件的绝对路径。
jar_path=""
# 拼接字符串,并将这个字符串存储在变量“jar_path”之中
for x in ${jar_files}
do
jar_path=${jar_path}":"${x}
done
# 设置环境变量
# echo "jar_path := " ${jar_path}
export CLASSPATH=${CLASSPATH}${jar_path}
echo "CLASSPATH_value := " ${CLASSPATH}
[root@openeuler libs]#
[root@openeuler libs]#
[root@openeuler libs]#
[root@openeuler libs]#
2、应用示例:
[root@openeuler libs]# uname -a
Linux openeuler 5.10.0-60.18.0.50.oe2203.x86_64 #1 SMP Wed Mar 30 03:12:24 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[root@openeuler libs]# java --version
java 17.0.1 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
[root@openeuler libs]# pwd
/root/user/lidawei/program/java/libs
[root@openeuler libs]# ls
last.jar lidawei.java load-java-jar temp.jar
[root@openeuler libs]# env
SHELL=/bin/bash
HISTCONTROL=ignoredups
HISTSIZE=1000
HOSTNAME=openeuler
JAVA_HOME=/usr/local/java/jdk17
PWD=/root/user/lidawei/program/java/libs
LOGNAME=root
MOTD_SHOWN=pam
HOME=/root
LANG=en_US.UTF-8
SSH_CONNECTION=10.10.10.12 58123 10.10.10.16 22
SELINUX_ROLE_REQUESTED=
TERM=xterm-256color
USER=root
SELINUX_USE_CURRENT_RANGE=
SHLVL=1
CLASSPATH=.:/usr/local/java/jdk17/lib
SSH_CLIENT=10.10.10.12 58123 22
PATH=/root/tools:/usr/bin:/usr/local/bin:/usr/local/java/jdk17/bin:/usr/local/sbin:/usr/sbin:/root/bin
SELINUX_LEVEL_REQUESTED=
MAIL=/var/spool/mail/root
SSH_TTY=/dev/pts/0
_=/usr/bin/env
OLDPWD=/root
[root@openeuler libs]# source load-java-jar
Welcome to 5.10.0-60.18.0.50.oe2203.x86_64
System information as of time: Sun May 1 01:48:45 AM CST 2022
System load: 0.08
Processes: 225
Memory used: 8.6%
Swap used: 0%
Usage On: 7%
IP address: 10.10.10.16
IP address: 192.168.122.1
Users online: 1
current_path = /root/user/lidawei/program/java/libs
CLASSPATH_value := .:/usr/local/java/jdk17/lib:/root/user/lidawei/program/java/libs/last.jar:/root/user/lidawei/program/java/libs/temp.jar
[root@openeuler libs]#
[root@openeuler libs]#
[root@openeuler libs]# env
SHELL=/bin/bash
HISTCONTROL=ignoredups
HISTSIZE=1000
HOSTNAME=openeuler
JAVA_HOME=/usr/local/java/jdk17
PWD=/root/user/lidawei/program/java/libs
LOGNAME=root
MOTD_SHOWN=pam
HOME=/root
LANG=en_US.UTF-8
SSH_CONNECTION=10.10.10.12 58123 10.10.10.16 22
SELINUX_ROLE_REQUESTED=
TERM=xterm-256color
USER=root
SELINUX_USE_CURRENT_RANGE=
SHLVL=1
CLASSPATH=.:/usr/local/java/jdk17/lib:/root/user/lidawei/program/java/libs/last.jar:/root/user/lidawei/program/java/libs/temp.jar
SSH_CLIENT=10.10.10.12 58123 22
PATH=/root/bin:/root/tools:/usr/bin:/usr/local/bin:/usr/local/java/jdk17/bin:/usr/local/sbin:/usr/sbin
SELINUX_LEVEL_REQUESTED=
MAIL=/var/spool/mail/root
SSH_TTY=/dev/pts/0
OLDPWD=/root
_=/usr/bin/env
[root@openeuler libs]#
[root@openeuler libs]#
[root@openeuler libs]#
[root@openeuler libs]#
[root@openeuler libs]#
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/16138415.html