安装 JAVA(JDK) 后,每当我重新启动系统或切换到普通用户时,都会显示一条消息:
bash: ${AUTO_CLASSPATH_DIR}: invalid variable name
bash: ${AUTO_CLASSPATH_DIR}: invalid variable name
这是配置脚本
cat > /etc/profile.d/openjdk.sh << "EOF"
# Begin /etc/profile.d/openjdk.sh
# Set JAVA_HOME directory
JAVA_HOME=/opt/jdk
# Adjust PATH
pathappend $JAVA_HOME/bin
# Add to MANPATH
pathappend $JAVA_HOME/man MANPATH
# Auto Java CLASSPATH: Copy jar files to, or create symlinks in, the
# /usr/share/java directory. Note that having gcj jars with OpenJDK 8
# may lead to errors.
AUTO_CLASSPATH_DIR=/usr/share/java
pathprepend . CLASSPATH
for dir in `find ${AUTO_CLASSPATH_DIR} -type d 2>/dev/null`; do
pathappend $dir CLASSPATH
done
for jar in `find ${AUTO_CLASSPATH_DIR} -name "*.jar" 2>/dev/null`; do
pathappend $jar CLASSPATH
done
export JAVA_HOME
unset AUTO_CLASSPATH_DIR dir jar
# End /etc/profile.d/openjdk.sh
EOF
这是的输出bash -xv /etc/profile.d/openjdk.sh
pathappend () { pathremove $1 $2;
local PATHVARIABLE=${2:-PATH};
export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1"
}
pathprepend () { pathremove $1 $2;
local PATHVARIABLE=${2:-PATH};
export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}"
}
pathremove () { local IFS=':';
local NEWPATH;
local DIR;
local PATHVARIABLE=${2:-PATH};
for DIR in ${!PATHVARIABLE};
do
if [ "$DIR" != "$1" ]; then
NEWPATH=${NEWPATH:+$NEWPATH:}$DIR;
fi;
done;
export $PATHVARIABLE="$NEWPATH"
}
# Begin /etc/profile.d/openjdk.sh
# Set JAVA_HOME directory
JAVA_HOME=/opt/jdk
+ JAVA_HOME=/opt/jdk
# Adjust PATH
pathappend $JAVA_HOME/bin
+ pathappend /opt/jdk/bin
+ pathremove /opt/jdk/bin
+ local IFS=:
+ local NEWPATH
+ local DIR
+ local PATHVARIABLE=PATH
+ for DIR in ${!PATHVARIABLE}
+ '[' /bin '!=' /opt/jdk/bin ']'
+ NEWPATH=/bin
+ for DIR in ${!PATHVARIABLE}
+ '[' /usr/bin '!=' /opt/jdk/bin ']'
+ NEWPATH=/bin:/usr/bin
+ for DIR in ${!PATHVARIABLE}
+ '[' /opt/jdk/bin '!=' /opt/jdk/bin ']'
+ export PATH=/bin:/usr/bin
+ PATH=/bin:/usr/bin
+ local PATHVARIABLE=PATH
+ export PATH=/bin:/usr/bin:/opt/jdk/bin
+ PATH=/bin:/usr/bin:/opt/jdk/bin
# Add to MANPATH
pathappend $JAVA_HOME/man MANPATH
+ pathappend /opt/jdk/man MANPATH
+ pathremove /opt/jdk/man MANPATH
+ local IFS=:
+ local NEWPATH
+ local DIR
+ local PATHVARIABLE=MANPATH
+ for DIR in ${!PATHVARIABLE}
+ '[' /usr/share/man '!=' /opt/jdk/man ']'
+ NEWPATH=/usr/share/man
+ for DIR in ${!PATHVARIABLE}
+ '[' opt/jdk/man '!=' /opt/jdk/man ']'
+ export MANPATH=/usr/share/man
+ MANPATH=/usr/share/man
+ local PATHVARIABLE=MANPATH
+ export MANPATH=/usr/share/man:/opt/jdk/man
+ MANPATH=/usr/share/man:/opt/jdk/man
# Auto Java CLASSPATH: Copy jar files to, or create symlinks in, the
# /usr/share/java directory. Note that having gcj jars with OpenJDK 8
# may lead to errors.
AUTO_CLASSPATH_DIR=/usr/share/java
+ AUTO_CLASSPATH_DIR=/usr/share/java
pathprepend . CLASSPATH
+ pathprepend . CLASSPATH
+ pathremove . CLASSPATH
+ local IFS=:
+ local NEWPATH
+ local DIR
+ local PATHVARIABLE=CLASSPATH
+ for DIR in ${!PATHVARIABLE}
+ '[' . '!=' . ']'
+ export CLASSPATH=
+ CLASSPATH=
+ local PATHVARIABLE=CLASSPATH
+ export CLASSPATH=.
+ CLASSPATH=.
for dir in `find ${AUTO_CLASSPATH_DIR} -type d 2>/dev/null`; do
pathappend $dir CLASSPATH
done
+ for dir in `find ${AUTO_CLASSPATH_DIR} -type d 2>/dev/null`
+ pathappend find '${AUTO_CLASSPATH_DIR}' -type d '2>/dev/null` CLASSPATH
+ pathremove find '${AUTO_CLASSPATH_DIR}'
+ local IFS=:
+ local NEWPATH
+ local DIR
+ local 'PATHVARIABLE=${AUTO_CLASSPATH_DIR}'
environment: line 4: ${AUTO_CLASSPATH_DIR}: invalid variable name
for jar in `find ${AUTO_CLASSPATH_DIR} -name "*.jar" 2>/dev/null`; do
pathappend $jar CLASSPATH
done
+ for jar in `find ${AUTO_CLASSPATH_DIR} -name "*.jar" 2>/dev/null`
+ pathappend find '${AUTO_CLASSPATH_DIR}' -name '"*.jar"' '2>/dev/null` CLASSPATH
+ pathremove find '${AUTO_CLASSPATH_DIR}'
+ local IFS=:
+ local NEWPATH
+ local DIR
+ local 'PATHVARIABLE=${AUTO_CLASSPATH_DIR}'
environment: line 4: ${AUTO_CLASSPATH_DIR}: invalid variable name
export JAVA_HOME
+ export JAVA_HOME
unset AUTO_CLASSPATH_DIR dir jar
+ unset AUTO_CLASSPATH_DIR dir jar
# End /etc/profile.d/openjdk.sh
在输出文件中,还为 AUTO_CLASSPATH_DIR 写入了无效的变量名称
我正在使用 bash 5.0(BLFS 8.4 环境)
答案1
问题是我无法区分反引号和单引号(因为我在一台旧笔记本电脑上执行此操作,所以我必须输入所有内容)。之后我再次编写了脚本,并且运行良好。