Is there any possible way I can install maven on any of the linux machine using a bash script ?
According to Mkyong, you can install Maven using sudo:
sudo apt-get install maven
then, run
mvn -version
to check that it installed.
Yes you can install mvn with bash script, example below
mvn_version=${mvn_version:-3.8.5}
url="http://www.mirrorservice.org/sites/ftp.apache.org/maven/maven-3/${mvn_version}/binaries/apache-maven-${mvn_version}-bin.tar.gz"
install_dir="/opt/maven"
mkdir ${install_dir}
curl -fsSL ${url} | tar zx --strip-components=1 -C ${install_dir}
cat << EOF > /etc/profile.d/maven.sh
#!/bin/sh
export MAVEN_HOME=${install_dir}
export M2_HOME=${install_dir}
export M2=${install_dir}/bin
export PATH=${install_dir}/bin:$PATH
EOF
source /etc/profile.d/maven.sh
echo maven installed to ${install_dir}
mvn --version
you need to add source /etc/profile.d/maven.sh to ~/.bash_profile or basrch
Related
Hello is this possible to create a file with these line of codes below and make it as an executable file on single line of code? Currently I'm doing manually. Your response is highly appreciated. Thank you
Manual Steps
-vi + content
-chmod +x filename
This is the file content:
#!/bin/bash
sudo apt-get update
sudo apt install curl -y
sudo apt install -y default-jdk
Screenshot:
CLI Image
Objective to write everything using one line of code
If it is crucial that it be a one-liner and vi is not a requirement:
echo -e '#!/bin/bash\nsudo apt-get update\nsudo apt install curl -y\nsudo apt install -y default-jdk' > test.sh && chmod +x test.sh && ./test.sh
If vi is a requirement you could do something like:
vim file.txt "+i#!/bin/bash" "+osudo apt-get update" "+o..." and so on
in place of the echo, but this seems much less effective to me and I'm less familiar with using vi in this way.
You could generate a test.sh with bash script (we can call it download.sh):
# 1. write script content to test.sh
cat <<EOT >> test.sh
#!/bin/bash
sudo apt-get update
sudo apt install curl -y
sudo apt install -y default-jdk
EOT
# 2. make it executable
chmod +x test.sh
When you execute bash download.sh, it will generate a test.sh automatically.
I am running a debian VM Instance using GCP Compute Engine and I have added a automation script to be executed on startup.
There are few tools which will be downloaded on startup. Only issue is, everything is getting downloaded in / directory.
I need to download everything in $HOME directory.
Different ways I have tried
#!/bin/bash
set -x
cd $HOME
mkdir $HOME/test
cd $HOME/test
apt install wget -y
wget https://download.java.net/openjdk/jdk11/ri/openjdk-11+28_linux-x64_bin.tar.gz
#!/bin/bash
set -x
source $HOME
mkdir $HOME/something
#!/bin/bash
set -x
cd $HOME
mkdir $HOME/something
exec bash
Still it is downloaded in / directory. What else can be done here?
You are trying to make 2 things : install wget package and download another one.
Why don't you tried to install wget manually ?
apt-get install wget
You have then to store the full path for your script, and download the package needed it it. Try this :
#!/bin/bash
homePath=$HOME
mkdir $HOME/test
wget https://download.java.net/openjdk/jdk11/ri/openjdk-11+28_linux-x64_bin.tar.gz -P $homePath/test/
I've been attempting to write a shell script to detect composer and git on a virtual linux = Ubuntu 16.0.4 machine and then install them if needed. + clone the required repository if the machine is ready for it.
Now this is my first attempt to write any kind of script and also sorry if somehow I messed up the question itself, I'm quite now on stackoverflow as well.
Any help is appreciated, thanks in advance.
Here's the original task specification I received initially:
-check if git is installed on server
if so, clone repo with codebase
-check if composer is installed on server
if so, composer install in the root directory of the laravel application
-finally, php artisan migrate --seed
Now this is how I was trying to achieve this:
#!/bin/bash
echo "The installation process is about the begin..."
if ! which git;
then echo "Git has been located on the destination system. Cloning begins..."
git clone <link to the gitlabe repo>
else echo "There is no Git installed on the system. Git installation commences..."
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git
echo "Cloning begins..."
git clone <link to the gitlabe repo>
fi
if ! which composer;
then
echo "Composer has been located on the destination system."
else
echo "There is no Composer installed on the system. Composer installation commences..."
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install composer
fi
sudo apt-get update
sudo apt-get install curl php5-cli git
curl -sS https://getcomposer.org/installer | sudo php -- --install- dir=/usr/local/bin --filename=composer
composer global require "laravel/installer"
sudo apt-get update
'Now the preferred version in the vagrantfile has to be edited to be 1.8.1 instead of 1.9'
'Generate a ssh key'
ssh-keygen -t rsa -b 4096 -C "< e-mail adress that I used >"
'Start ssh agent eval $'
ssh-agent -s
'Add your SSH private key to the ssh-agent'
ssh-add -K ~/.ssh/id_rsa
php artisan migrate --seed
The error message I recieve:
sudo sh ./testscript.sh
[sudo] password for linuxtest:
The installation process is about the begin...
: not foundt.sh: 3: ./testscript.sh:
: not foundt.sh: 4: ./testscript.sh:
: not foundt.sh: 5: ./testscript.sh:
./testscript.sh: 72: ./testscript.sh: Syntax error: end of file unexpected (expecting "then")
The answer that helped me solve my problem was posted by Charles Duffy in a comment:
This looks like your file has DOS rather than UNIX newlines. This will prevent syntax like fi from being recognized, because it's read as fi$'\r', which isn't a keyword.
#!/bin/bash
echo "The installation process is about the begin...";
if [ -x "$(command -v git)" ]; then
echo "Git has been located on the destination system. Cloning; begins..."
git clone <link to the gitlabe repo>;
else
echo "There is no Git installed on the system. Git installation commences...";
sudo apt-get update && sudo apt-get upgrade && sudo apt-get install git;
echo "Cloning begins...";
git clone <link to the gitlabe repo>;
fi
if [ -x "$(command -v composer)" ]; then
echo "Composer has been located on the destination system.";
else
echo "There is no Composer installed on the system. Composer installation commences...";
sudo apt-get update && sudo apt-get upgrade && sudo apt-get install composer;
fi
Hey there
I think this should fix your If condition problem. If you want to know more about how you should check for the case that a programm exists, look here:
Check if a program exists from a Bash script
Its the second answer for a quickfix.
Always remember to chain commands which are depending on the success of the preceding command via "&&". This secures that the next command will just be executed if the preceding doesn't fail.
I recommend doing it the same way with the ssh commands.
#edit
also make sure that you end each command with a semicolon.
hope I could help.
I am using this command.
nvm - curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
This is the error I get:
No command 'nvm' found, did you mean:
Command 'pvm' from package 'pvm' (universe)
etc.
My .bashrc looks like this at the bottom:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
source ~/nvm/nvm.sh
And my .profile looks like this:
# set PATH so it includes user's private bin directories
[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh
source ~/.nvm/nvm.sh
node -v
v6.10.2
What is the problem here?
All help is appreciated. I can provide more information if needed.
The command you want to run is:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
you cannot run nvm if you haven't install it and is not in the ubuntu repositories.
You need to have curl installed so might need to install it:
sudo apt-get install curl
Curl downloads the install.sh script and pipe it to bash to run it.
Remember to source your .bashrc afte running the script:
source ~/.bashrc
I am trying to install nvm on my Docker image. I originally thought that this Docker image was built on Ubuntu, but it is actually built on Debian. I am installing bash to curl NVM, and subsequently install node, but I get a bad substitution error:
Here's my Dockerfile:
FROM docker
RUN apk add --update bash \
&& touch /root/.bashrc \
&& curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh | bash \
&& source /root/.bashrc \
&& nvm install node \
&& npm install
I think the following error has to do with the line && source /root/.bashrc \
=> Downloading nvm as script to '/root/.nvm'
0
=> Appending source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm
/bin/sh: /root/.nvm/nvm.sh: line 107: syntax error: bad substitution
ERROR: Service 'docker' failed to build: The command '/bin/sh -c apk add --update bash && touch /root/.bashrc && curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh | bash && source /root/.bashrc && nvm install node && npm install' returned a non-zero code: 2
Do you see what is causing this bad substitution error, and is there a more simple way to install nvm on a Debian based Docker image? Thanks for any help.
Docker image is based out of Alpine Linux. Alpine Linux uses the default shell as sh. The error is because of the sh vs bash incompatibilities.
Unfortunately, NVM home page has instructions about Alpine Linux, but quite discouraging:
nvm on Alpine Linux
After some changes, the final version that made nvm work with Alpine:
FROM docker
RUN apk add --update bash coreutils ncurses tar gzip nodejs \
&& touch ~/.bashrc \
&& curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh | sh \
&& LINE=$(cat /root/.nvm/nvm.sh | grep -in '{BASH_SOURCE\[0\]}' | awk -F: '{print $1}') \
&& sed -i "${LINE}s/BASH_SOURCE\[0\]\}/BASH_SOURCE\}\$\{0\}/" /root/.nvm/nvm.sh \
&& source ~/.bashrc \
&& nvm ls \
&& nvm install node \
&& nvm use --delete-prefix v6.3.1 \
&& npm install
A little inconvenience being, you need to use the nvm use --delete-prefix v6.3.1 every time you need to work with it.
I suggest to try #BMitch's updated answer as well.
FROM docker bases your image on the "Docker in Docker" Alpine image. Unless you have a special use case that requires Docker in Docker, this probably isn't the base image you want.
If you want a node image, consider using the premade node image. This is based on Debian jessie.
If you need to base your node install another version of Debian or Ubuntu, you can pick from multiple versions of those images, e.g. FROM debian:jessie.
Edit: it's pretty easy to add Docker to another image. Here are my Dockerfile entries for a Debian based image (appuser is a user added elsewhere that the container would normally run as, hence the Docker group addition):
ARG DOCKER_GID=999
USER root
RUN curl -sSL https://get.docker.com/ | sh
RUN groupmod -g ${DOCKER_GID} docker && \
usermod -aG docker appuser