I have a bash script which installs some software with apt-get as well as download and installs miniconda3. Later I would like to utilize conda command without restarting the shell. This script is called with sudo but for all the things related to conda I want to pose as a regular user, see below:
#!/usr/bin/env bash
# we are operating in the user's home dir
sudo -u $SUDO_USER bash Miniconda3-latest-Linux-x86_64.sh -b -p miniconda3
source [path_to_the_user_home]/miniconda3/etc/profile.d/conda.sh
sudo -u $SUDO_USER -H -s eval $(conda shell.bash hook)
sudo -u $SUDO_USER conda --version
However, I get an error that the command conda is not recognized. Interestingly, if the last line would be just conda --version then it is correctly recognised. It seems that the 2nd to last line worked for root, but not the user (which is exactly what I want)
Related
I'm using kubectl from within a docker container running on a Mac. I've already successfully configured the bash completion for kubectl to work on the Mac, however, it doesn't work within the docker container. I always get bash: _get_comp_words_by_ref: command not found.
The docker image is based on ubuntu:16.04 and kubectl is installed via the line (snippet from the dockerfile)
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && \
mv kubectl /usr/local/bin
echo $BASH_VERSION gives me 4.3.48(1)-release, and according to apt, the bash-completionpackage is installed.
I'm using iTerm2 as terminal.
Any idea why it doesn't work or how to get it to work?
Ok, I found it - I simply needed to do a source /etc/bash_completion before or after the source <(kubectl completion bash).
check .bashrc
enable programmable completion features (you don't need to enable
this, if it's already enabled in /etc/bash.bashrc and /etc/profile
sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
A Linux container executed on macOS creates a separate environment and
yes, it looks like a thread from macOS shell, but it is not. Shell history,
properties, functions are a different story.
Moreover, if the container has no persistent volume mounted all of those parameters will be transisten and won’t survive container’s restart.
The approach to have bash completion of both of them - macOS and Ubuntu
Linux are similar, but require different steps to take:
macOS side - permanent support for kubectl bash completion:
use homebrew to install support:
brew install bash-completion
kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl
Ubuntu container’s approach to have kubectl and bash completion support build in:
You can adapt this set of commands and use it in Dockerfile during the image preparation:
apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubectl
echo 'source <(kubectl completion bash)' >> ~/.bashrc
If afterwards you or user executes /bin/bash in running container then you should get completion working.
docker exec -it docker_image_id /bin/bash
this will start bash shell with the bash completion.
I united two top comments for Ubuntu 22.04
edit ~/.bashrc and add
source /etc/bash_completion
before
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k
How do I install the anaconda / miniconda without prompts on Linux command line?
Is there a way to pass -y kind of option to agree to the T&Cs, suggested installation location etc. by default?
can be achieved by bash miniconda.sh -b (thanks #darthbith)
The command line usage for this can only be seen with -h flag but not --help, so I missed it.
To install the anaconda to another place, use the -p option:
bash anaconda.sh -b -p /some/path
AFAIK pyenv let you install anaconda/miniconda
(after successful instalation)
pyenv install --list
pyenv install miniconda3-4.3.30
For a quick installation of miniconda silently I use a wrapper
script script that can be executed from the terminal without
even downloading the script. It takes the installation destination path
as an argument (in this case ~/miniconda) and does some validation too.
curl -s https://gist.githubusercontent.com/mherkazandjian/cce01cf3e15c0b41c1c4321245a99096/raw/03c86dae9a212446cf5b095643854f029b39c921/miniconda_installer.sh | bash -s -- ~/miniconda
Silent installation can be done like this, but it doesn't update the PATH variable so you can't run it after the installation with a short command like conda:
cd /tmp/
curl -LO https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -u
Here -b means batch/silent mode, and -u means update the existing installation of Miniconda at that path, rather than failing.
You need to run additional commands to initialize PATH and other shell init scripts, e.g. for Bash:
source ~/miniconda3/bin/activate
conda init bash
My question is what would be a good way to run two admin accounts for developing in regards to running Homebrew, Zsh and Oh My Zsh and configuring the .zshrc file?
My reasoning for this is that I recently started a new job and would like to use my Mac with two accounts, both of which will be admins and both of which I will require devtools so as to keep my work life/dev and personal life/dev cleanly separated. I just erased my hard drive and cleanly installed OS X 10.12.3 and created two Admin accounts.
I've had a shot at setting it up however keep getting permission errors whenever I switch accounts and run terminal, usually specific to zsh completions.
My steps:
Install Homebrew on both users /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)".
Install zsh with brew brew install zsh and change to it chsh -s /bin/zsh.
Install oh my zsh with curl via sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)".
run sudo chown -R $(whoami):admin /usr/local.
Then I've tried a whole mess of things which I'll refrain from adding as I believe they may only lead others down a dark path.
Although this step is safe to run as root, I still recommend running the installation as non-root user to prevent catastrophic problems with root switching shell while the shell isn't working. superuser shell should never be changed.
Here's the proper way to install oh-my-zsh for multiple users.
Step 1: Ensure umask is not stricter than 022. If not set it to 022.
$ umask 022
Step 2: set and export ZSH with the destination path where the shared oh-my-zsh will be installed.
export ZSH=/usr/local/.zsh/oh-my-zsh
Step 3: Create the parent directory and make it owned by the user installing the scripts.
$ sudo mkdir /usr/local/.zsh
$ sudo chown ${USER} /usr/local/.zsh
Step 4: Run the installer.
$ bash -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Step 5: Change ownership of the installation path back to root.
$ sudo chown -R root /usr/local/.zsh
Step 6: Update location of ZSH in ${ZSH}/templates/zshrc.zsh-template
$ sudo sed -i 's|export ZSH=.*|export ZSH='${ZSH}'|' ${ZSH}/templates/zshrc.zsh-template
(Optional)
Step 7: Disable auto update. This is recommended as the installation is now owned by root.
$ sudo sed -i 's/# DISABLE_AUTO_UPDATE="true"/DISABLE_AUTO_UPDATE="true"/' ${ZSH}/templates/zshrc.zsh-template
From here on, each user can copy ${ZSH}/templates/zshrc.zsh-template as ~/.zshrc
Whilst #alvits answer may have been correct I was unable to get it to work for me. I however, was able to get it to work by doing the following.
Install Homebrew (check https://brew.sh/ for up to date command):
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install Zsh with Homebrew:
brew install zsh
Close terminal and reopen with ZSH as default shell:
Install Oh My Zsh as root user
sudo su
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Close terminal to exit root user mode.
Install Oh My Zsh as each admin (therefore install on both users):
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
That stopped all the auto-completions and permission errors for me. I believe this to be overkill and that there would be a way with symlinks however this way is easy.
I want to automate the installation of perlbrew into the vagrant box. I use .sh file to accomplish this.
provision.sh
apt-get update
sudo -H -u vagrant bash -c " \curl -kL https://install.perlbrew.pl | bash"
sudo -u vagrant bash -c "source ~/perl5/perlbrew/etc/bashrc"
After ssh into the vagrant i expect that
$ which perlbrew
will return
/home/vagrant/perl5/perlbrew/bin/perlbrew
but unfortunately it returns nothing.
There is no way the settings applied by your source ~/perl5/perlbrew/etc/bashrc command would be visible in another bash session (and a SSH session executes a new bash process).
You need to add the command source ~/perl5/perlbrew/etc/bashrc to one of the bash "rc" files.
For a single user with the following command:
echo "source ~/perl5/perlbrew/etc/bashrc" >> ~/.bashrc
For all users with the following command:
echo "source ~/perl5/perlbrew/etc/bashrc" >> /etc/bash.bashrc
This way every time a new bash session is started, it will run source ~/perl5/perlbrew/etc/bashrc and apply the settings.
I am very new to bash scripting. I have the following script:
cp /etc/apt/sources.list /var/chroot/etc/apt/sources.list
chroot /var/chroot/
apt-get update
apt-get --simulate install $a > output
I actually want the last 2 comands to be run in chroot environment but I do not know how to give it to it, I searched but I could not find. I also want chroot to exit after execution of the commands, but it currently hangs. What can I do to prevent this?
EDIT: For future visitors:
cp /etc/apt/sources.list /var/chroot/etc/apt/sources.list
chroot /var/chroot apt-get update > /dev/null
chroot /var/chroot apt-get --simulate install nodejs
The command you want to run in the chroot environment must be given to chroot as an argument. See the manual page.