【问题标题】:How to have a part of `~/.bash_profile execute only on shell open?如何让 `~/.bash_profile 的一部分仅在 shell 打开时执行?
【发布时间】:2020-12-30 10:12:58
【问题描述】:

我一直在寻找这个答案,也许这是一条我不知道的死胡同。我正在运行 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 时运行。 p>

我喜欢在工作时调整我的/.bash_profile,它来自~/.bashrc,所以如果我在 ~/.bash_profile 中进行修改并想查看更改,目前我将被引导回我的 dev 目录.它还会对日志文件等文件进行冗余更改。这些登录“设置”操作可能会随着时间而改变

我知道.bash_login 存在,但我似乎无法让它为我工作。我将我的终端首选项设置为“以登录身份运行命令”或任何设置,然后它会忽略我的 ~/.bash_profile(我猜这可能是预期的行为)

p.s: `~/.bash_vars 包含随机变量,我想从 env 到 env 或保存一些秘密,因为这个 bash_profile 是版本化的

【问题讨论】:

  • if [ -z "$skipstartup" ]; then startup; export skipstartup=1; fi

标签: bash authentication workflow rc


【解决方案1】:

从 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 上执行。

【讨论】:

  • 好吧听起来很公平,听起来很安全:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多