How to restart the shell in script? - bash

I have a script that installs the pyenv package. Installing it requires adding environment variables, then restarting the shell.
apt install -y make build-essential libssl-dev zlib1g-dev \
> libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev\
> libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl\
> git
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
for example after PATH:
exec $SHELL <<EOF1
How can I restart the shell so that the rest of the script continues to run?

Think twice before having a script updating your .bashrc. In particular if you are not the only user of this script, never modify .bashrc automatically. You don't know what's in it, and how the user of your script has carefully handcrafted his .bashrc.
Instead write the settings to a separate file and request the user to source this file from inside his .bashrc (or wherever he wants to do it; perhaps he will prefer .bash_profile for this?). Therefore you do something like
echo 'export PYENV_ROOT="$HOME/.pyenv"' > ~/.pyenv_settings
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.pyenv_settings
source ~/.pyenv_settings
which also ensures that the settings are now performed inside your script.

Related

chmod +x cant find build.sh file

I have been trying to make a VCS in C++ but the build file is not running in my LINUX(Ubuntu).
It is prompting the above message.
my build file is as follows:
#!/bin/bash
sudo apt-get update
udo apt-get install openssl -y
sudo apt-get install libssl-dev -y
mkdir -p ~/imperium/bin
cp imperium.sh ~/imperium
cd ..
make
cd ~/imperium/bin || echo "error"
chmod +x main
cd ..
if grep -q "source $PWD/imperium.sh" "$PWD/../.bashrc" ; then
echo 'already installed bash source';
else
echo "source $PWD/imperium.sh" >> ~/.bashrc;
fi
my imperium.sh file is also as follows:
function imperium(){
DIR=$PWD
export dir=$DIR
cd ~/imperium/bin || echo "Error"
./main "$#"
cd "$DIR" || echo "Error"
}
I will be heavily obliged if any one can solve this problem of mine. After chmod I have been doing:
./build.sh but its prompting that build.sh file does not exists.
For me it seems you have a typo right in the 3rd row "udo" ->
"sudo".
Also, You should avoid using cd .. and use relative paths for
the commands.

How to Install Homebrew on Windows WSL Ubuntu, and fix "zsh: brew command not found" error

Installation was a series of 5 simple steps:
first, install homebrew itself from command in the home page:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
After that, following instructions in this page, and changing ~/.bash_profile to ~/.profile as I am using Ubuntu as my wsl distro, i had to give these commands:
test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
test -r ~/.profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile
echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile
But now, when I try to run brew, I get command not found error.
In a wsl environment, brew is installed at location: /home/linuxbrew/.linuxbrew/ which is not part of the path.
So we simply need to add that to path, and it works. I am using zsh as my shell, so I add these lines to my ~/.zshrc file (in ubuntu file system) :
export BREW_HOME="/home/linuxbrew/.linuxbrew/bin"
export PATH="$PATH:$BREW_HOME"
if you happen to need to share your .zshrc across different OS, you can do some OS check in your zshrc file to see if it is macOS or linux/wsl:
case `uname` in
Linux)
## add brew home to PATH in linux/WSL
brew_home=/home/linuxbrew/.linuxbrew
if [ -d "${brew_home}" ]; then
export PATH=${brew_home}/bin:$PATH
fi
javac_loc=/usr/bin/javac
if [ -x "$javac_loc" ]; then
export JAVA_HOME=$(readlink -f $(dirname $(readlink -f $javac_loc)))
fi
;;
Darwin)
## Do macOS thing...
export JAVA_HOME=$(/usr/libexec/java_home)
;;
esac

Dockerfile: Permission denied when trying to install ruby-build

I'm trying to install ruby-build as a non-root in my Dockerfile but I am getting a permission denied error. How can I give the deploy user access to do so?
error
mkdir: cannot create directory `/usr/local/share/ruby-build': Permission denied
Dockerfile
FROM centos:6.6
RUN yum update -y
RUN yum install git openssl-devel openssh-server sudo openssl readline-devel readline zlib-devel zlib libxml2-devel libxml2 libxslt-devel libxslt nginx tar gcc libaio libaio-devel -y
RUN rpm -Uvh https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.5.1-1.el6.x86_64.rpm
RUN sed -i -e "s/Defaults requiretty.*/ #Defaults requiretty/g" /etc/sudoers
RUN mkdir -p /var/run/sshd
# RUN adduser deploy -g wheel -p Password1
RUN useradd -m -u 1000 -G wheel deploy && sed -ri 's/^(%wheel.*)(ALL)$/\1NOPASSWD: \2/' /etc/sudoers
USER deploy
RUN git clone https://github.com/sstephenson/rbenv.git $HOME/.rbenv/
RUN git clone https://github.com/sstephenson/ruby-build.git $HOME/.rbenv/plugins/ruby-build
RUN $HOME/.rbenv/plugins/ruby-build/install.sh
ENV PATH $HOME/.rbenv/bin:$PATH
RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh
RUN echo 'eval "$(rbenv init -)"' >> $HOME/.bashrc
RUN echo 'eval "$(rbenv init -)"' >> $HOME/.bash_profile
RUN source $HOME/.bash_profile
ENV CONFIGURE_OPTS --disable-install-doc
RUN rbenv install 2.2.3
RUN rbenv global 2.2.3
RUN bash -l -c 'gem update --system'
RUN bash -l -c 'gem update'
RUN bash -l -c 'gem install nokogiri -- --use-system-libraries'
RUN bash -l -c 'gem install bundler rails-api --no-rdoc --no-ri'
RUN touch /etc/sysconfig/network
EXPOSE 3306
EXPOSE 22
EXPOSE 80
EXPOSE 3389
You are trying to do install ruby-build as root using the deploy user. "Installing as a standalone program (advanced)" as per here.
You can try something like this (using sudo):
FROM centos:6.6
RUN yum update -y
RUN yum install git openssl-devel openssh-server sudo openssl readline-devel readline zlib-devel zlib libxml2-devel libxml2 libxslt-devel libxslt nginx tar gcc libaio libaio-devel -y
RUN rpm -Uvh https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chef-12.5.1-1.el6.x86_64.rpm
RUN sed -i -e "s/Defaults requiretty.*/ #Defaults requiretty/g" /etc/sudoers
RUN mkdir -p /var/run/sshd
# RUN adduser deploy -g wheel -p Password1
RUN useradd -m -u 1000 -G wheel deploy && echo '%wheel ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.d/wheel
USER deploy
RUN git clone https://github.com/sstephenson/rbenv.git $HOME/.rbenv/
RUN git clone https://github.com/sstephenson/ruby-build.git $HOME/.rbenv/plugins/ruby-build
RUN sudo $HOME/.rbenv/plugins/ruby-build/install.sh
ENV PATH /home/deploy/.rbenv/bin:$PATH
RUN echo 'eval "$(rbenv init -)"' | sudo tee -a /etc/profile.d/rbenv.sh
RUN echo 'eval "$(rbenv init -)"' >> $HOME/.bashrc
RUN echo 'eval "$(rbenv init -)"' >> $HOME/.bash_profile
RUN source $HOME/.bash_profile
ENV CONFIGURE_OPTS --disable-install-doc
RUN rbenv install 2.2.3
RUN rbenv global 2.2.3
RUN bash -l -c 'gem update --system'
RUN bash -l -c 'gem update'
RUN bash -l -c 'gem install nokogiri -- --use-system-libraries'
RUN bash -l -c 'gem install bundler rails-api --no-rdoc --no-ri'
RUN touch /etc/sysconfig/network
EXPOSE 3306
EXPOSE 22
EXPOSE 80
EXPOSE 3389

Installing rbenv on docker ubuntu/debian

I want to install rbenv on Docker which seems to work but I can't reload the shell.
FROM node:0.10.32-slim
RUN \
apt-get update \
&& apt-get install -y sudo
RUN \
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \
&& groupadd r \
&& useradd r -m -g r -g sudo
USER r
RUN \
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv \
&& echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc \
&& echo 'eval "$(rbenv init -)"' >> ~/.bashrc
RUN rbenv # check if it works...
When I run this I get:
docker build .
..
Step 5 : RUN rbenv
/bin/sh: 1: rbenv: not found
From what I understand, I need to reload the current shell so I can install ruby versions. Not sure if I am on the right track.
Also see:
Using rbenv with Docker
The RUN command executes everything under /bin/sh, thus your bashrc is not evaled at any point.
use this
&& export PATH="$HOME/.rbenv/bin:$PATH" \
which would append rbenv to /bin/sh's PATH.
Full Dockerfile
FROM node:0.10.32-slim
RUN \
apt-get update \
&& apt-get install -y sudo
RUN \
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \
&& groupadd r \
&& useradd r -m -g r -g sudo
USER r
RUN \
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv \
&& echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc \
&& echo 'eval "$(rbenv init -)"' >> ~/.bashrc \
&& export PATH="$HOME/.rbenv/bin:$PATH"
RUN rbenv # check if it works...
I'm not sure how Docker works, but it seems like maybe you're missing a step where you source ~/.bashrc, which is preventing you from having the rbenv executable in your PATH. Try adding that right before your first attempt to run rbenv and see if it helps.
You can always solve PATH issues by using the absolute path, too. Instead of just rbenv, try running $HOME/.rbenv/bin/rbenv.
If that works, it indicates that rbenv has installed successfully, and that your PATH is not correctly set to include its bin directory.
It looks from reading the other question you posted that docker allows you to set your PATH via an ENV PATH command, like this, for example:
ENV PATH $HOME/.rbenv/bin:/usr/bin:/bin
but you should make sure that you include all of the various paths you will need.

Redirecting bash script to /dev/null not executing?

so I'm very new to bash and I was making an installer for build-essential and OpenSSL. The problem is that It always stops after the first exec line. Here's my code:
#!/bin/bash
echo "Installing build-essential"
exec sudo apt-get install build-essential > /dev/null 2>&1
echo "Finished installing build-essential"
echo ""
echo "Installing OpenSSL"
exec sudo apt-get install openssl > /dev/null 2>&1
echo "Finished installing OpenSSL"
echo ""
echo "Updates complete!"
And here's the output:
Installing build-essential
[sudo] password for matthew:
Please keep in mind that I just started a few hours ago. Sorry for the dump question.
exec never returns to the calling script. It replaces the current process with the command following exec. Just remove the exec altogether, and let apt-get run like any other command.
Note: there are uses of exec that do return to the calling script, such as those that only do I/O redirection.

Resources