What does /root/.bashrc do? - bash

echo 'export PYENV_ROOT="/root/.pyenv"' >> /root/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> /root/.bashrc
echo 'eval "$(pyenv init -)"' >> /root/.bashrc
. /root/.bashrc
Can someone please help me understand what this script do? or maybe point me out to a documentation that has a better explanation.
Thank you so much!

You can actually find an explanation here under the section Basic GitHub Checkout:
https://github.com/yyuu/pyenv
Quoted from webpage:
Define environment variable PYENV_ROOT to point to the path where
pyenv repo is cloned and add $PYENV_ROOT/bin to your $PATH for access
to the pyenv command-line utility.
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile.
Ubuntu note: Modify your ~/.bashrc file instead of ~/.bash_profile.
Add pyenv init to your shell to enable shims and autocompletion.
Please make sure eval "$(pyenv init -)" is placed toward the end of
the shell configuration file since it manipulates PATH during the
initialization.
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
If you want to learn more, you should probably read this page at the section Invoked as an
interactive non-login shell:
https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

From bash's manual page (man bash):
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc [...]
Recommended reading: 6.2 Bash Startup Files from Bash Reference Manual

Related

pyenv - environment "activated", but python and pip not found

I suppose there is something wrong with my bash init scripts (like .bashrc or .bash_profile). But let's start from beginning.
I can create and activate pyenv environment, but when I try to use python, I get error: -bash: python: command not found.
It looks like pyenv understands creation and swapping envorinments. I mean, it's probably not malformed. There is preview of my tries:
$ mkdir test-python-project
$ cd test-python-project/
$ pyenv versions
* system (set by /home/vagrant/.pyenv/version)
3.7.10
3.7.10/envs/k-pkb-env
$ pyenv virtualenv 3.7.10 test-env
Looking in links: /tmp/tmpkwojcc1e
Requirement already satisfied: setuptools in /home/vagrant/.pyenv/versions/3.7.10/envs/test-env/lib/python3.7/site-packages (47.1.0)
Requirement already satisfied: pip in /home/vagrant/.pyenv/versions/3.7.10/envs/test-env/lib/python3.7/site-packages (20.1.1)
$ pyenv activate test-env
pyenv-virtualenv: prompt changing will be removed from future release. configure export PYENV_VIRTUALENV_DISABLE_PROMPT=1 to simulate the behavior.
(test-env) $ python
-bash: python: command not found
(test-env) $ pyenv local test-env
(test-env) $ cd ..
(test-env) $ pyenv deactivate
$ cd test-python-project/
(test-env) $ python
-bash: python: command not found
(test-env) $ pip
-bash: pip: command not found
(test-env) $ pyenv version
test-env (set by /home/vagrant/Work/test-python-project/.python-version)
I'm not sure how to configure bash init scripts, because in pyenv readme they suggest using .profile, which I don't have.
So, there are my bash inits:
.bashrc
$ cat .bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific environment
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
then
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
.bash_profile
$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
# PyEnv
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Some additional information:
$PATH variable
$ echo $PATH
/home/vagrant/.pyenv/plugins/pyenv-virtualenv/shims:/home/vagrant/.pyenv/bin:/home/vagrant/.local/bin:/home/vagrant/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
It's a bit strange for me, because this additional paths added by pyenv doesn't seem to contain path to desired virtual environment:
$ ls /home/vagrant/.pyenv/plugins/pyenv-virtualenv/shims
activate deactivate
$ ls /home/vagrant/.pyenv/bin
pyenv
type python
$ type python
-bash: type: python: not found
which python
$ which python
/usr/bin/which: no python in (/home/vagrant/.pyenv/plugins/pyenv-virtualenv/shims:/home/vagrant/.pyenv/bin:/home/vagrant/.local/bin:/home/vagrant/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin)
I tried also pyenv rehash, but also still no effect:
(test-env) [vagrant#centos test-python-project]$ pyenv rehash
(test-env) [vagrant#centos test-python-project]$ python
-bash: python: command not found
With some help from #Simba, I managed to have my configuration correct:
.bash_profile
# .bash_profile
# !!! ITS IMPORTANT THESE LINES MUST BE BEFORE . ~/.bashrc
# PyEnv - only path-related
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
# !!! ITS IMPORTANT THESE LINES ABOVE MUST BE BEFORE . ~/.bashrc
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific environment
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
then
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
# PyEnv - commands
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Solution
READ THE PYENV GUIDE CAREFULLY.
You didn't follow pyenv's README guide correctly. The guide tells you put PATH related operation in .bash_profile or .profile. But eval "$(pyenv init -)" in .bashrc.
Move pyenv init script from .bash_profile to .bashrc.
# Put it in .bashrc
# PyEnv
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Extended Reading
You bash shell is not a login shell. .bash_profile is not sourced at all, which skip pyenv init -.
Bash initialization
login mode:
/etc/profile
~/.bash_profile, ~/.bash_login, ~/.profile (only the first one that exists)
interactive non-login:
/etc/bash.bashrc (some Linux; not on Mac OS X)
~/.bashrc
non-interactive:
source file in $BASH_ENV
The default shell on Linux is a non-login, interactive shell. The default shell on macOS is a login, interactive shell.
There's also a detailed explanation about the shell startup by flowblok
Ref
Unix shell initialization from pyenv wiki
Shell startup scripts

How to resolve Terminal error/messaging somehow related to rbenv

I've been receiving the following error/messaging right after I start up Terminal. This started happening after multiple attempts to install sass (which is another issue on its own).
I've since uninstalled rbenv.
I'm running osx el capitan.
-bash: ‘export: command not found
-bash: /Users/JO/.bash_profile: line 5: syntax error near unexpected token `('
-bash: /Users/JO/.bash_profile: line 5: `‘eval “export PATH="/Users/JO/.rbenv/shims:${PATH}" export RBENV_SHELL=bash source '/usr/local/Cellar/rbenv/1.1.2/libexec/../completions/rbenv.bash' command rbenv rehash 2>/dev/null rbenv() { local command command="${1:-}" if [ "$#" -gt 0 ]; then shift fi case "$command" in rehash|shell) eval "$(rbenv "sh-$command" "$#")";; *) command rbenv "$command" "$#";; esac }”’'
I'm way over my head with Terminal issues. So, any help on this is much appreciated. Cheers.
It is said (Basic Github checkout, part 2) to you to copy and past one of the following options, according to your running terminal:
For Bash: echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
For Ubuntu: echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
For Zsh: echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
Checking your log above, I saw you use 'eval, which you must delete, and let just "one of the options above".
Alternatively, you may run the rbenv doctor to see what you missed:
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
Do not forget to open a new terminal to see your changes running.

Why my file does not get sourced from bash script?

I have a bash script where at some point, I want to source the ${HOME}/.profile file which should add ${HOME}/.local/bin to the $PATH. But when I check the path with echo $PATH, ${HOME}/.local/bin is absent as if the source did not happen. What am I doing wrong?
if command -v pip3 &>/dev/null; then
echo "Pip is already installed."
else
echo "Pip is not installed. Installing Pip..."
cd ${HOME}/Downloads
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
su -c "python3 get-pip.py --user" "$SUDO_USER"
cat <<-'EOT' >> "${HOME}/.profile"
# set PATH so it includes user's private .local/bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi
EOT
source "${HOME}/.profile" #this is not happening!!!
rm ${HOME}/Downloads/get-pip.py
echo "Pip has been installed."
fi
Thanks in advance.
EDIT: Fixed the script syntax as suggest by Kusalananda.
A script can't modify the environment of the shell from whence it was executed.
Sourcing ~/.profile in the script will not set the path in the interactive shell that originally started the script. To do that, you would have to source your script.
Also, your here-document would need to be quoted, or the current values of HOME and PATH would be inserted into the .profile file:
cat <<'PROFILE_END' >> "$HOME/.profile"
# set PATH so it includes user's private .local/bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi
PROFILE_END
Also note that if the user is a bash user with an existing ~/.bash_profile file, then the ~/.profile file will be ignored for that user when a new login shell is started.
I'm further unsure why you su to $USER. It seems like an unnecessary step.

How to quickly setup Spark on YARN in a cluster with a bash script?

During my self paced learing of Hadoop and Spark, I found very tedious the environment setup phase during which we need to setup a small cluster and repeat installation and configuration for all dependencies on all nodes. Java, Hadood, Spark. Is there a way to get this done using a bash script?
I took a first stab at it and bash can definitively do the job. Once SSH is properly setup for the cluster, a bash script like the following is a good start. It needs some refinement but still ... The sample script bellow only takes Hadoop/Yarn into account for now but this is a work in progress. Same approach can be used to setup Java and Spark on all nodes. I will update this answer when I will have it completed ;)
!/bin/bash
for x in hadoop-slave1 hadoop-slave2 hadoop-slave3
do
ssh $x bash -c "'
cd ~
wget https://archive.apache.org/dist/hadoop/core/hadoop-2.7.3/hadoop-2.7.3.tar.gz
tar -xzvf hadoop-2.7.3.tar.gz
ln -s hadoop-2.7.3 hadoop
echo "# HADOOP" >> ~/.bashrc
echo "export HADOOP_PREFIX=/home/hduser/hadoop" >> ~/.bashrc
source ~/.bashrc
echo "export HADOOP_HOME=$HADOOP_PREFIX" >> ~/.bashrc
echo "export PATH=$PATH:$HADOOP_PREFIX/bin:$HADOOP_PREFIX/sbin" >> ~/.bashrc
echo "export HADOOP_COMMON_HOME=$HADOOP_PREFIX" >> ~/.bashrc
echo "export HADOOP_MAPRED_HOME=$HADOOP_PREFIX" >> ~/.bashrc
echo "export HADOOP_HDFS_HOME=$HADOOP_PREFIX" >> ~/.bashrc
echo "export YARN_HOME=$HADOOP_PREFIX" >> ~/.bashrc
source ~/.bashrc
mkdir -p ~/tmp #create tmp dir used and configured in hadoop
# work around an issue I got with JAVA_HOME env var beeing lost.
echo export `env | grep ^JAVA_HOME` >> ~/hadoop/etc/hadoop/hadoop-env.sh
exit
'"
# copy config files to slaves. assuming that master is already setup - to be improved
scp ~/hadoop/etc/hadoop/core-site.xml hduser#$x:~/hadoop/etc/hadoop
scp ~/hadoop/etc/hadoop/hdfs-site.xml hduser#$x:~/hadoop/etc/hadoop
scp ~/hadoop/etc/hadoop/yarn-site.xml hduser#$x:~/hadoop/etc/hadoop
done

How to pip install into a virtualenv via a bash install script?

I recently tried to automate the setup of an Ubuntu VM with a bash script (I am new to bash scripting).
The issue is that the way I set it up, it doesn't work. Particularly the mkvirtualenv and workon commands don't work in the bash script. How do I create a virtualenv in a bash script with passing it a variable and then install into the virtualenv via pip?
#!/bin/bash
VENV_NAME='name_of_virtualenv'
#Setting up virtualenv
mkdir --mode=770 /var/virtualenvs
chown -R www-data:www-edit /var/virtualenvs
chmod 771 /var/virtualenvs
echo '# virtualenv and virtualwrapper' >> ~/.bashrc
echo ' export VIRTUALENV_USE_DISTRIBUTE=1' >> ~/.bashrc # <-- Always use pip/distribute
echo ' export WORKON_HOME=/var/virtualenvs' >> ~/.bashrc
echo ' source /usr/local/bin/virtualenvwrapper.sh' >> ~/.bashrc
echo ' export PIP_VIRTUALENV_BASE=$WORKON_HOME' >> ~/.bashrc
echo ' export PIP_RESPECT_VIRTUALENV=true' >> ~/.bashrc
source ~/.bashrc
mkvirtualenv --distribute '{VENV_NAME}'
workon {VENV_NAME}
pip install psycopg2
pip install --upgrade PIL
You missing dollar before the call, also did not see an export for VENV_NAME
export VIRTUALENV_USE_DISTRIBUTE=1
echo {VIRTUALENV_USE_DISTRIBUTE}
{VIRTUALENV_USE_DISTRIBUTE}
echo ${VIRTUALENV_USE_DISTRIBUTE}
1
Unsure why you need to export out to bashrc and from the looks of it each time you run it, it would add the same exports to bashrc which will end up with a larger and larger bashrc file each time
Why not just make them local variables like
VIRTUALENV_USE_DISTRIBUTE=1
workon $VIRTUALENV_USE_DISTRIBUTE

Resources