如何在脚本中重新加载 bashrc 并且不失败?

如何在脚本中重新加载 bashrc 并且不失败?

我编写了一个脚本,用于下载并安装 anaconda3 和一些其他依赖项,然后使用 安装几个 python 模块pip3
问题是我的source ~/.bashrc似乎不起作用,因此pip3无法识别。如果我使用exec bash,则不会执行此命令后的其余指令。我该怎么办?顺便说一下,这是我的脚本:

#!/bin/bash

## Bash script for setting up a PX4 development environment on Ubuntu LTS (16.04 and above).
## It can be used for installing simulators (only) or for installing the preconditions for Snapdragon Flight or Raspberry Pi.
##
## Installs:
## - Common dependencies and tools for all targets (including: Ninja build system, latest versions of cmake, git, anaconda3, pyulog)
## - jMAVSim simulator dependencies
## - PX4/Firmware source (to ~/src/Firmware/)

# Preventing sudo timeout https://serverfault.com/a/833888
trap "exit" INT TERM; trap "kill 0" EXIT; sudo -v || exit $?; sleep 1; while true; do sleep 60; sudo -nv; done 2>/dev/null &

# Ubuntu Config
echo "Remove modemmanager"
sudo apt-get remove modemmanager -y
echo "Add user to dialout group for serial port access (reboot required)"
sudo usermod -a -G dialout $USER

# Update CMake and Git
# Installing latest version of cmake (ref https://askubuntu.com/questions/355565/#865294 )
echo "Installing latest version of CMake"
sudo apt update && \
sudo apt install -y software-properties-common lsb-release && \
sudo apt clean all
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null
sudo apt-add-repository "deb https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main"
sudo apt update
sudo apt install kitware-archive-keyring
sudo rm /etc/apt/trusted.gpg.d/kitware.gpg
sudo apt update
sudo apt install cmake

# Installing the latest version of git
echo "Installing the latest version of git"
sudo add-apt-repository -y ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git -y

# Install anaconda3
type conda >/dev/null 2>&1 || { echo >&2 "Installing anaconda3 (python 3.8.8)"; wget https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh; bash ./Anaconda3-2021.05-Linux-x86_64.sh;}

source ~/.bashrc
. ~/.bashrc

# Common dependencies
echo "Installing common dependencies"
sudo apt-get update -y
sudo apt-get install git zip cmake build-essential genromfs ninja-build exiftool astyle -y
# make sure xxd is installed, dedicated xxd package since Ubuntu 18.04 but was squashed into vim-common before
which xxd || sudo apt install xxd -y || sudo apt-get install vim-common --no-install-recommends -y
# Required python packages
pip3 install argparse empy toml numpy
pip3 install pandas jinja2 pyserial pyyaml
pip3 install pyulog

# jMAVSim simulator dependencies
echo "Installing jMAVSim simulator dependencies"
sudo apt-get install ant openjdk-8-jdk openjdk-8-jre -y

答案1

我必须使用evalsource~/.bashrc才能成功。我这样做了:

eval "$(cat ~/.bashrc | tail -n +10)"

引自这里

此行为特定于 Ubuntu(也可能是大多数派生发行版),因为你的默认~/.bashrc文件以短路 Ubuntu 18.04 开头,

例子:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

...如果文件在非交互式 shell 中运行,它将停止对文件的评估,这是您的脚本的情况,因为所有脚本都在非交互式 shell 中运行,随后您提供的每个文件都将继承此属性。

一切顺利。看来,由于脚本在非交互模式下运行,因此bashrc由于在 bashrc 开头进行了检查(至少在基于 ubuntu 的发行版中),source 没有任何效果。因此,使用 eval,我们可以规避这个问题。

答案2

尝试在之后运行以下代码source ~/.bashrc,这会让 Bash 忘记所有记住的完整路径:

$ hash -r

来自 Bash 手册

hash [-r] [-p 文件名] [-dt] [名称]

每次调用 hash 时,它都会记住指定为名称参数的命令的完整路径名,因此在后续调用中无需搜索它们。 [...] -r 选项导致 shell 忘记所有记住的位置。

相关内容