Bash - /etc/profile,登录时过多的只读变量消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11248287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:38:13  来源:igfitidea点击:

Bash - /etc/profile, excessive read only variable messages on login

bashshellvariablesreadonly

提问by jonschipp

On Ubuntu Linux, with Bash, I have /etc/profile set with read-only variables on login. Here's my /etc/profile ( my additions are toward the bottom of this file ):

在 Ubuntu Linux 上,使用 Bash,我在登录时使用只读变量设置了 /etc/profile。这是我的 /etc/profile(我添加的内容在此文件的底部):

# Check for interactive bash and that we haven't already been sourced.
[ -z "$BASH_VERSION" -o -z "$PS1" -o -n "$BASH_COMPLETION" ] && return

# Check for recent enough version of bash.
bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
if [ $bmajor -gt 3 ] || [ $bmajor -eq 3 -a $bminor -ge 2 ]; then
    if shopt -q progcomp && [ -r /etc/bash_completion ]; then
        # Source completion code.
        . /etc/bash_completion
    fi
fi
unset bash bmajor bminor

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

TZ='America/Kentucky/Louisville'; export TZ

if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
  . $i
fi
done
unset i
fi

if [ "$PS1" ]; then
if [ "$BASH" ]; then
PS1='\u@\h:\w$ '
if [ -f /etc/bash.bashrc ]; then
    . /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
  PS1='# '
else
  PS1='$ '
fi
fi
fi

 **# My Additions**

umask 077
shopt -s histappend
shopt -s histverify

export HISTFILE=~/.bash_history
export HISTFILESIZE=1000000000
export HISTSIZE=5000
export HISTCONTROL=""
export HISTIGNORE=""
export HISTTIMEFORMAT="%F %T"

readonly HISTFILE
readonly HISTFILESIZE
readonly HISTSIZE
readonly HISTCONTROL
readonly HISTIGNORE
readonly HISTTIMEFORMAT
readonly HISTCMD
readonly HOME
readonly PATH

echo -e "Subject: Login from $(/usr/bin/whoami) on $(/bin/hostname) at          $(/bin/date)\n\n$(/usr/bin/last -n 10 -F)\n" \
| /usr/sbin/ssmtp [email protected]

export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo "$$ $USER $(history    1)"|/usr/bin/logger -p user.alert -t shell.log'
readonly PROMPT_COMMAND

And here is the bash_completion file that is located in /etc/profile.d:

这是位于 /etc/profile.d 中的 bash_completion 文件:

# Check for interactive bash and that we haven't already been sourced.
[ -z "$BASH_VERSION" -o -z "$PS1" -o -n "$BASH_COMPLETION" ] && return

# Check for recent enough version of bash.
bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
if [ $bmajor -gt 3 ] || [ $bmajor -eq 3 -a $bminor -ge 2 ]; then
if shopt -q progcomp && [ -r /etc/bash_completion ]; then
    # Source completion code.
    . /etc/bash_completion
fi
fi
unset bash bmajor bminor

My problemis that when I login I am "flooded" with lots of bash messages before the prompt is delivered:

我的问题是,当我登录时,在发送提示之前,我被大量 bash 消息“淹没”:

....
-bash: PATH: readonly variable
-bash: PATH: readonly variable
-bash: PATH: readonly variable
-bash: PATH: readonly variable
-bash: HISTFILE: readonly variable
-bash: HISTFILESIZE: readonly variable
-bash: HISTSIZE: readonly variable
-bash: HISTCONTROL: readonly variable
-bash: HISTIGNORE: readonly variable
-bash: HISTTIMEFORMAT: readonly variable
-bash: PROMPT_COMMAND: readonly variable

My first questionis why is there so many PATH: readonly variable messages 15+ with full output? My second questionis how can I get stop these messages from displaying on login.

我的第一个问题是为什么有这么多 PATH: readonly variable messages 15+ with full output?我的第二个问题是如何阻止这些消息在登录时显示。

Thanks in advance for any help!

在此先感谢您的帮助!

回答by Paused until further notice.

Patient: "Doctor, it hurts when I do this."
Doctor: "Don't do that."

病人:“医生,我做这个的时候很痛。”
医生:“别这样。”

Don't set those variables as readonly.

不要将这些变量设置为只读。

The reason that you're getting those error messages is that those variables are being modified in files that execute after /etc/profile(e.g. ~/.bashrc).

您收到这些错误消息的原因是这些变量在之后执行的文件中被修改/etc/profile(例如~/.bashrc)。