5

我的/etc/environment如下所示:

cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

我希望使用一个命令(sed、awk、python 等等......)让它看起来像这样:

cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JAVA_HOME="/usr/lib/jvm/java-6-sun"

现在要注意的是,我宁愿它是一个 1 班轮(在 sed -XYZ /DoMagic/ /etc/environment 的字段中),它需要包含合并逻辑,即 - 附加新的配置记录或更新现有的. 最重要的是,它应该防止文件看起来像这样:(由经验丰富的 shell 脚本编写者在每次调用时调用 echo >> 引起)

cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JAVA_HOME="/usr/lib/jvm/java-5-sun"
JAVA_HOME="/usr/lib/jvm/java-6-sun"
JAVA_HOME="/usr/lib/jvm/java-6-sun"
JAVA_HOME="/usr/lib/jvm/java-6-sun"

我想这是一个技巧问题,因为我试图避免使用自定义脚本,例如

/usr/local/bin/PropUpdate /etc/environment JAVA_HOME "/usr/lib/jvm/java-6-sun"

/usr/local/bin/PropUpdate是以下脚本(为了示例而编写,可能包含错误。感谢评论)

#!/bin/bash

# Append/Update a configuration record in a file
#
# Usage example:
# /usr/local/bin/PropUpdate /etc/environment JAVA_HOME "/usr/lib/jvm/java-6-sun"
#
# Author Maxim Veksler <maxim@vekslers.org>
# Version 0.5-2010-07-27


EXPECTED_ARGS=3
E_BADARGS=3
E_BADFILE=4

if [[ $# -ne ${EXPECTED_ARGS} ]]; then
  echo "Usage: `basename $0` /path/to/config.conf ParameterName newValueText" >&2
  exit $E_BADARGS
fi

CONFIGURATION_FILE="$1"
CONFIGURATION_PARAMETER="$2"
CONFIGURATION_VALUE="$3"

if [[ ! -e "${CONFIGURATION_FILE}" ]]; then
        echo "Configuration file ${CONFIGURATION_FILE} does not exist" >&2
        exit $E_BADFILE
fi

if [[ ! -w "${CONFIGURATION_FILE}" ]]; then
        echo "Can't modify ${CONFIGURATION_FILE}" >&2
        exit $E_BADFILE
fi



#########################################
## Decide what parameter we are adding ##
#########################################
__param_found=0

# First check CONFIGURATION_PARAMETER supplied by use that contains "="
if [[ ${CONFIGURATION_PARAMETER} == *=* ]]; then
        # It should exist in the file, plain
        if grep -qE "^${CONFIGURATION_PARAMETER}" "${CONFIGURATION_FILE}"; then
                __param_found=1
                SUFFIX_REGEX='[[:space:]]*'
        fi
else
        # OK, sophisticated user, did not send "=" with the parameter...
        if grep -qE "^${CONFIGURATION_PARAMETER}[[:space:]]*=" "${CONFIGURATION_FILE}"; then
                # Let's check if such configuration with Parameter + "=" exists
                __param_found=1
                SUFFIX_REGEX='[[:space:]]*=[[:space:]]*'
        elif grep -qE "^${CONFIGURATION_PARAMETER}[[:space:]]+" "${CONFIGURATION_FILE}"; then
                # If such parameter exists, at all
                __param_found=1
                SUFFIX_REGEX='[[:space:]]\+'
        fi
fi


if [[ $__param_found == 1 ]]; then
        #echo sed -i "s|^\(${CONFIGURATION_PARAMETER}${SUFFIX_REGEX}\).*$|\1${CONFIGURATION_VALUE}|g" "${CONFIGURATION_FILE}"
        sed -i "s|^\(${CONFIGURATION_PARAMETER}${SUFFIX_REGEX}\).*$|\1${CONFIGURATION_VALUE}|g" "${CONFIGURATION_FILE}"

else
        if [[ ${CONFIGURATION_PARAMETER} == *=* ]]; then
                # Configuration parameter contains "=" in it's name, good just append
                echo "${CONFIGURATION_PARAMETER}${CONFIGURATION_VALUE}" >> "${CONFIGURATION_FILE}"
        else
                # Try to guess if this file is a "param = value" or "param value" type of file.
                if grep -qE "^[[:alnum:]]+[[:space:]]*=" "${CONFIGURATION_FILE}"; then
                        # Seems like a "param = value" type of file
                        echo "${CONFIGURATION_PARAMETER}=${CONFIGURATION_VALUE}" >> "${CONFIGURATION_FILE}"
                else
                        # Seems like a "param  value" type of file
                        echo "${CONFIGURATION_PARAMETER} ${CONFIGURATION_VALUE}" >> "${CONFIGURATION_FILE}"
                fi
        fi
fi

#cat $CONFIGURATION_FILE

谢谢你,马克西姆。

-- 更新:我实际上有点喜欢这个脚本,所以我对它进行了一些改进。它现在似乎已准备好生产。享受。

4

3 回答 3

4

正如我在对相关问题的回答中所描述的,您可以代替尝试解析/etc/environment文件,而是在 中创建一个具有您自己名称的文件。然后你可以在安装过程中复制它,因为它只包含你的内容。更不用说它会使您的脚本更短。/etc/profile.d/

于 2010-07-25T18:45:10.633 回答
2
grep -q JAVA_HOME /etc/environment || echo 'JAVA_HOME="/usr/lib/jvm/java-5-sun"' >> /etc/environment

如果在文件中找到该模式,该grep命令将返回 0 (true)。所以,上面写着:

检查文件中是否设置了
JAVA_HOME 或在文件中设置了 JAVA_HOME

0-15:49 root@noneedto ~# cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
0-15:49 root@noneedto ~# grep JAVA_HOME /etc/environment && echo true 
1-15:49 root@noneedto ~# grep -q JAVA_HOME /etc/environment || echo 'JAVA_HOME="/usr/lib/jvm/java-5-sun"' >> /etc/environment 
0-15:49 root@noneedto ~# grep JAVA_HOME /etc/environment && echo true
JAVA_HOME="/usr/lib/jvm/java-5-sun"
真的
0-15:49 root@noneedto ~# grep -q JAVA_HOME /etc/environment || echo 'JAVA_HOME="/usr/lib/jvm/java-5-sun"' >> /etc/environment 
0-15:49 root@noneedto ~# cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JAVA_HOME="/usr/lib/jvm/java-5-sun"

如您所见,如果您多次调用此单行代码,则后续调用不会添加到文件中,因为 grep 在您尝试附加文件之前返回 true。

于 2010-07-26T19:53:21.013 回答
0

在我的 Ubuntu 系统中,我的 JAVA_HOME 如下所示:

JAVA_HOME=/usr/lib/jvm/default-java

看着那个文件,ls -l /usr/lib/jvm/default-java我注意到了这一点:

lrwxrwxrwx 1 root root 24 Apr 27  2012 /usr/lib/jvm/default-java -> java-1.7.0-openjdk-amd64

换句话说,软链接中的路径是你唯一需要改变的。

要查看已安装 Java 环境的列表,我使用了以下ls -l ...命令:

prompt$ ls -l /usr/lib/jvm
total 20
lrwxrwxrwx 1 root root   24 Apr 27  2012 default-java -> java-1.7.0-openjdk-amd64
drwxr-xr-x 4 root root 4096 Feb 23 17:54 java-1.5.0-gcj-4.8-amd64
lrwxrwxrwx 1 root root   20 Sep  2  2012 java-1.6.0-openjdk-amd64 -> java-6-openjdk-amd64
lrwxrwxrwx 1 root root   20 Jul  3  2013 java-1.7.0-openjdk-amd64 -> java-7-openjdk-amd64
drwxr-xr-x 5 root root 4096 Oct  7  2012 java-6-openjdk-amd64
drwxr-xr-x 3 root root 4096 Oct  7  2012 java-6-openjdk-common
drwxr-xr-x 5 root root 4096 Sep 21 20:06 java-7-openjdk-amd64
drwxr-xr-x 8 root root 4096 Sep 18 21:18 java-7-oracle

所以现在我可以切换到另一个默认值:

sudo rm /usr/lib/jvm/default-java
sudo ln -s java-7-oracle /usr/lib/jvm/default-java

JAVA_HOME 变量将从 Oracle 运行 Java 7。

于 2014-02-25T00:04:52.153 回答