问题描述
我一直在高低寻找这个答案,也许这是一条我不知道的死胡同。我正在运行 Ubuntu 20.04,我目前有这个 bash_profile:
export MAG_DIR="/var/www/html/magento-2"
export Nginx_ERR="/var/log/Nginx/error.log"
export MAG_ERR="/var/www/html/magento-2/var/log/system.log"
export XDEBUG_LOG="/var/log/xdebug/xdebug.log"
export PHP_FPM_CONF="/etc/PHP/7.3/fpm/PHP.ini"
export PHP_CLI_CONF="/etc/PHP/7.3/cli/PHP.ini"
export PHP_LOG="/tmp/PHP-error.log"
function wgrep () {
grep -nA 3 -B 3 $1;
}
function findfirst () {
find $1 -name $2 | head -n 1
}
function catfirst () {
cat $(find $1 -name $2 | head -n 2)
}
function t30 {
tail -fn 30 $1;
}
function fac {
git fetch && git checkout $1;
}
#if [ -f ~/.bashrc ]; then
# . ~/.bashrc
#fi
if [ -f ~/.bash_vars ]; then
. ~/.bash_vars
fi
export PS1="\D{%H:%M:%s} \[\e]0;\u@\h: \w\a\] ? ${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "
#local setup
[ ! -f $PHP_LOG ] && touch $PHP_LOG
chmod 777 $PHP_LOG
cd $MAG_DIR
clear
我想将最后几行迁移到一个单独的脚本中,该脚本为新会话设置 shell,该脚本仅在首次创建 shell 时运行。 >
我喜欢在工作时调整我的 /.bash_profile
,它来自 ~/.bashrc
,所以如果我在 ~/.bash_profile 中进行修改并想查看更改,目前我将被重新启动到我的开发目录。它还会对日志文件等文件进行冗余更改。这些登录“设置”操作可能会随着时间的推移而改变
我知道 .bash_login
存在,但我似乎无法让它为我工作。我将终端首选项设置为“以登录名运行命令”或任何设置,然后它会忽略我的 ~/.bash_profile
(我猜这可能是预期行为)
ps: `~/.bash_vars 包含随机变量,我想在不同的 env 之间改变这些随机变量或保存一些秘密,因为这个 bash_profile 是版本化的
解决方法
从 OP 问题来看,目标是让 .bash_profile
的尾部仅在初始 shell 上执行,而不在子 shell 上执行。一种可能的解决方案是跟踪导出变量中的一次性执行。
if [ ! "$FIRST_TIME" ] ; then
export FIRST_TIME=YES
[ ! -f $PHP_LOG ] && touch $PHP_LOG
chmod 777 $PHP_LOG
cd $MAG_DIR
clear
fi
鉴于在此示例中,'.bashrc' 正在采购 '.bash_profile',受“FIRST_TIME”保护的命令将不会在交互式子 shell 上执行。