alternatives --config java bash script - linux

I am writing a script that installs java on a remote machine. After i run the .bin file for the JRE, how can i set the alternatives --config java without the user having to input anything.
For instance, when you type in "alternatives --config java" you are prompted to select which java version you would like. Due to the way i installed java ("/usr/sbin/alternatives --install /usr/bin/java java /location/of/jdk1.6/bin/java 2") the #"2" option should always be the java that i want selected.
So, using an ssh command execution, how can i select the second option for java alternatives without the user having to choose the option. I want it fully automated.
This is in a bash script.
Thanks
Below is the code (working correctly now):
#install the jre
sshRetValue=`ssh -p "22" -i $HOME/sshids/idrsa-1.old ${1} " /home/geiser/jms_adapter/jre-6u25-linux-i586.bin "`;
sshRetValue=`echo $?`;
if [ "$sshRetValue" -eq 0 ];then
echo "java jre installed successfully";
#set the alternative and stuff if needed
ssh -p "22" -i $HOME/sshids/idrsa-1.old ${1} " /usr/sbin/alternatives --install /usr/bin/java java /root/jre1.6.0_25/bin/java 2 ";
echo 2 | ssh -p "35903" -i $HOME/sshids/idrsa-1.old ${1} " alternatives --config java ";
else
echo "java jre installation failed";
fi

You can run the alternatives command non-interactively too. Instead of --config, use the --set option to specify the path of the alternative directly.
sudo alternatives --set java /location/of/jdk1.6/bin/java

This worked for me with Java 8:
alternatives --install /usr/bin/java java /usr/lib/jvm/jre1.8.0_60/bin/java 3
alternatives --config java <<< '3'

Generally, you can feed any program that expects something on the standard input like this:
echo -e "line 1\nline 2\nline 3" | program

I did it using this script:
tmp=`mktemp`
echo 2 > $tmp
alternatives --config java < $tmp
rm -f $tmp
The < means that the content of the $tmp file will be passed to the input of the alternatives command.
Edit: You could simply use a single pipe as other suggested:
echo 2 | sudo alternatives --config java

I had couple java versions in my /usr/lib/jvm directory.
My script had to first remove old symlink, create new one and then specify the path of the alternative directly with update-alternatives (i did not manage to make alternatives work without update- prefix) My system - Debian 8.11
My script:
#!/bin/bash
echo "Removing old java 8 symlink.."
sudo unlink /usr/lib/jvm/java
echo "Linking new java.."
sudo ln -s /usr/lib/jvm/new_java /usr/lib/jvm/java
echo "Updating alternatives for java and java compiler"
sudo update-alternatives --set java /usr/lib/jvm/new_java/bin/java
sudo update-alternatives --set javac /usr/lib/jvm/new_java/bin/javac

The documentation says:
If you want to configure non-interactively you can use the --set
option instead (see below).
--set name path
Set the program path as alternative for name. This is
equivalent to --config but is non-interactive and thus
scriptable.
So, to set alacritty as the default terminal, use the following command:
sudo update-alternatives --install /usr/bin/x-terminal-emulator x-terminal-emulator $(which alacritty) 50
sudo update-alternatives --set x-terminal-emulator $(which alacritty)
In your case, use the following command:
sudo alternatives --install /usr/bin/java java $(which java) 70
sudo alternatives --set java $(which java)

Related

systemctl start service does not work in SPEC file

I met a problem that the command "sudo systemctl start xxx.service" in my SPEC file does not work when upgrading my RPM package, following is my %post script in SPEC file,
%post
echo "---------------------------- post $1 -------------------------------"
# refresh installation
if [ $1 == 1 ]; then
sudo echo "Installation finished."
# upgrade installation
elif [ $1 -gt 1 ]; then
sudo echo "Starting service xxx.service..."
sudo /usr/bin/systemctl enable xxx.service > /dev/null 2>&1
sudo /usr/bin/systemctl start xxx.service
sleep 10
sudo echo "Finished."
fi
exit 0
I'm sure that the service file already exists in directory /usr/lib/systemd/system, and I can start it manually using the command "sudo systemctl start xxx.service".
And I found that the "sleep 10" command does not work too.
Very appreciated if there is any suggestion about this issue, thanks.
Few issues:
You're not supposed to use sudo in scriplets, because 1) it may not be installed 2) rpm installation runs as superuser anyway
You should use the standard RPM macros for SystemD as opposed to reinventing the wheel.
Essentially that simply goes down to:
%{?systemd_requires}
BuildRequires: systemd
# ...
%post
%systemd_post %{name}.service
%preun
%systemd_preun %{name}.service
%postun
%systemd_postun_with_restart %{name}.service
# ...
Take note that the SystemD macros for CentOS/RHEL are within systemd package, while in Fedora they are now in systemd-rpm-macros.
Placing the service startup command in the scriptlet "%posttrans" resolves my problem, thanks for all your suggestions.

How do I alias python2 to python3 in a docker container?

I am trying to set the default python in my docker container to be python3 and have set the aliases in the dockerfile. When I open the .bashrc file, they show up. As far as I can tell, it should work but the default python version is still 2.7. if I run which python, it will still point to usr/bin/python rather than python3. Same with pip. Can anyone tell me what the problem is? Here is the command I'm using to alias:
RUN \
echo 'alias python="/usr/bin/python3"' >> /root/.bashrc && \
echo 'alias pip="/usr/bin/pip3"' >> /root/.bashrc
Does this look right? I am using ubuntu 17.10
You try to create a symlink for python bin
RUN ln -s /usr/bin/python3 /usr/bin/python & \
ln -s /usr/bin/pip3 /usr/bin/pip
other option is use update-alternatives for more visit this site
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3
and another option is trying source the bashrc file after updating
RUN \
echo 'alias python="/usr/bin/python3"' >> /root/.bashrc && \
echo 'alias pip="/usr/bin/pip3"' >> /root/.bashrc && \
source /root/.bashrc
I recommend seeing all options of python images on Docker Hub
Tip: use anaconda or conda for managing your python versions (conda site)
The answer above is great, except it should be as follows:
RUN ln -s /usr/bin/python3 /usr/bin/python && \
ln -s /usr/bin/pip3 /usr/bin/pip
Perhaps they typo-ed by writing ls which just lists the contents of the directory, rather than using ln which actually creates symlinks.

How to run WP-CLI using bash script in linux

I'm having issue to run bash script setting up WP-CLI by it's own. Keep on getting wp no command found error. Please help.
#!/bin/bash
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
exec bash
wp --info
wp plugin install taxonomy-terms-order --path=/var/www
wp plugin activate taxonomy-terms-order --path=/var/www
It's only running till exec bash line. after that its not installing any plugin. Please help.
Do not hesitate to make small experiments to understand the problem:
$ cat test.sh
#!/bin/bash
echo "Test 1"
exec bash
echo "Test 2"
$ echo $$
6506
$ ./test.sh
Test 1
$ echo $$
6548
exec bash is opening a new blocking process.
So, I think you can remove this line from your script.
If /usr/local/bin is not in your PATH, you can use the complete path of /usr/local/bin/wp instead of wp :
/usr/local/bin/wp --info
/usr/local/bin/wp plugin install taxonomy-terms-order --path=/var/www
/usr/local/bin/wp plugin activate taxonomy-terms-order --path=/var/www
Or you can add this path to the PATH:
export PATH="${PATH}:/usr/local/bin/wp"

Disable sudo for a single command in a sh file on macOS Sierra

I have been looking everywhere for something to disable sudo access for this one command while maintaining sudo for the rest for the script. The script is called using sudo e.g. "sudo ./install.sh". The snippet in question is this (I currently was trying to make a new user to call the command, but to no use):
echo >&2 "Installing Homebrew"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
I only want line 2's command to run as not root/ no sudo. I am very much a noob, so any guidance/alternative methods not including making a new user, would be very appreciated.
If the script was run with sudo, then you can use $SUDO_USER to get the original username:
echo >&2 "Installing Homebrew"
sudo -u "$SUDO_USER" /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
(And don't use sudo on the other commands in the script -- if the script itself is run with sudo, that's completely redundant.)
BTW, this will fail completely if the script was not run with sudo -- for example, if someone logs in as root and runs it, uses su to switch to root and run it, etc. It might be better to check first, something like this:
if [ -n "$SUDO_USER" ]; then
echo >&2 "Installing Homebrew"
sudo -u "$SUDO_USER" /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
echo "No original user found to install Homebrew under" >&2
fi

Docker Bash prompt does not display color output

I use the command docker run --rm -it govim bash -l to run Docker images, but it does not display color output.
If I source ~/.bash_profile or run bash -l again, output will then correctly be output with color.
Bash Prompt Image
My bash_profile and bash_prompt files.
The OP SolomonT reports that docker run with env do work:
docker run --rm -it -e "TERM=xterm-256color" govim bash -l
And Fernando Correia adds in the comments:
To get both color support and make tmux work, I combined both examples:
docker exec -it my-container env TERM=xterm-256color script -q -c "/bin/bash" /dev/null
As chepner commented (earlier answer), .bash_profile is sourced (itis an interactive shell), since bash_prompt is called by .bash_profile.
But docker issue 9299 illustrates that TERM doesn't seem to be set right away, forcing the users to open another bash with:
docker exec -ti test env TERM=xterm-256color bash -l
You have similar color issues with issue 8755.
To illustrate/reproduce the problem:
docker exec -ti $CONTAINER_NAME tty
not a tty
The current workaround is :
docker exec -ti `your_container_id` script -q -c "/bin/bash" /dev/null
Both are supposing you have a running container first, which might not be convenient here.
Based on VonC's answer I adding the following to my Dockerfile (which allows me to run the container without typing the environment variables on the command line every time):
ENV TERM xterm-256color
#... more stuff
CMD ["bash", "-l"]
And sure enough it works with:
docker run -it my-image:tag
For tmux to work with color, in my ~/.tmux.conf I need:
set -g default-terminal "screen-256color"
and for UTF-8 support in tmux, in my ~/.bashrc:
alias tmux='tmux -u'
My Dockerfile:
FROM fedora:26
ENV TERM xterm-256color
RUN dnf upgrade -y && \
dnf install golang tmux git vim -y && \
mkdir -p /app/go/{bin,pkg,src} && \
echo 'export GOPATH=/app/go' >> $HOME/.bashrc && \
echo 'export PATH=$PATH:$GOPATH/bin' >> $HOME/.bashrc && \
mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim \
https://tpo.pe/pathogen.vim && \
git clone https://github.com/farazdagi/vim-go-ide.git \
~/.vim_go_runtime && \
bash ~/.vim_go_runtime/bin/install && \
echo "alias govim='vim -u ~/.vimrc.go'" >> ~/.bashrc && \
echo "alias tmux='tmux -u'" >> ~/.bashrc && \
echo 'set -g default-terminal "screen-256color"' >> ~/.tmux.conf
CMD ["bash", "-l"]
The Dockerfile builds an image based off Fedora 26, updates it, installs a few packages (Git, Vim, golang and tmux), installs the pathogen plugin for Vim, then it installs a Git repository from here vim-go-ide and finally does a few tweaks to a few configuration files to get color and UTF-8 working fine. You just need to add persistent storage, probably mounted under /app/go.
If you have an image with all the development tools already installed, just make a Dockerfile with ENV statement and add the commands to modify the configuration files in a RUN statement without the installation commands and use your base image in the FROM statement. I prefer this solution because I'm lazy and (besides the initial setup) it saves typing when you want to run the image.
Using Vim and plugins within tmux
Adding -t is working for me:
docker exec -t vendor/bin/phpunit
Adding to VonC's answer, I made this Bash function:
drun() { # start container with the specified entrypoint and colour terminal
if [[ $# -lt 2 ]]; then
echo "drun needs 2+ arguments: image entrypoint" >&2
return
fi
docker run -ti -e "TERM=xterm-256color" "$#"
}
I think this is something that you'd have to implement manually. My container has Python, so here's how I print in color using a single line:
Example Docker file:
FROM django:python3
RUN python -c "print('\033[90m HELLO_WORLD \033[0m')"
RUN python -c "print('\033[91m HELLO_WORLD \033[0m')"
RUN python -c "print('\033[92m HELLO_WORLD \033[0m')"
RUN python -c "print('\033[93m HELLO_WORLD \033[0m')"
RUN python -c "print('\033[94m HELLO_WORLD \033[0m')"
RUN python -c "print('\033[95m HELLO_WORLD \033[0m')"
RUN python -c "print('\033[96m HELLO_WORLD \033[0m')"
RUN python -c "print('\033[97m HELLO_WORLD \033[0m')"
RUN python -c "print('\033[98m HELLO_WORLD \033[0m')"
Standard terminal:
You need to add the following line to your Dockerfile:
RUN echo PS1="'"'\[\e]0;\u#\h: \w\a\]${debian_chroot:+($debian_chroot)}\ \033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '"'" >> /app/.bashrc
Change the /app/.bashrc to where your .bashrc file is in the docker.
If you want ls command to have colors too add this line:
RUN echo alias ls="'"'ls --color=auto'"'" >> /app/.bashrc

Resources