Score:0

How can I reload bashrc in a script and not failing?

cl flag

I have written a script that downloads and installs anaconda3, and some other dependencies, and then it installs a couple of python modules using pip3.
The issue is that my source ~/.bashrc doesn't seem to work, and thus pip3's are not recognized. If I use exec bash, the remaining instructions after this command are not executed. What should I do? This is my script by the way :

#!/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

Hossein avatar
cl flag
Thanks, but it seems it doesn't cause any issues, as it downloads and installs it just fine(though I fixed it already thanks to you). the double sourcing is just there since sourceing once didnt do anything. the two of them also dont do any good either.
Score:0
in flag

Try to run the following code after source ~/.bashrc, which makes Bash forget all remembered full-paths:

$ hash -r

From Bash manual:

hash [-r] [-p filename] [-dt] [name]

Each time hash is invoked, it remembers the full pathnames of the commands specified as name arguments, so they need not be searched for on subsequent invocations. [...] The -r option causes the shell to forget all remembered locations.

Score:0
cl flag

I had to use eval to source ~/.bashrc successfully. I did this :

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

Quoting from here:

This behavior is specific to Ubuntu (and probably most derived distros), since your default ~/.bashrc file starts with a short-circuit, Ubuntu 18.04, for

example:

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

...That will stop the evaluation of the file if it is running in a non-interactive shell, which is the case of your script since all scripts are run in a non-interactive shell, and subsequently every file you source will inherit this property.

and all went just fine. It seems since scripts run in non-interactive mode, sourcing bashrc doesnt have any effect because of a check at the beginning of the bashrc (at least in ubuntu based distros). therefore using eval, we can circumvent this.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.