Change directory in a startup script debian - linux

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/

Related

docker RUN mkdir does not work when folder exist in prev image

the only difference between them is that the "dev" folder exists in centos image,
check the comment in this piece of code(while executing docker build),appreciate it if anyone can explain why?
FROM centos:latest
LABEL maintainer="xxxx"
RUN dnf clean packages
RUN dnf -y install sudo openssh-server openssh-clients curl vim lsof unzip zip
**below works well!**
# RUN mkdir -p oop/script
# RUN cd oop/script
# ADD text.txt /oop/script
**/bin/sh: line 0: cd: dev/script: No such file or directory**
RUN mkdir -p dev/script
RUN cd dev/script
ADD text.txt /dev/script
EXPOSE 22
There are two things going on here.
The root of your problem is that /dev is a special directory, and is re-created for each RUN command. So while RUN mkdir -p dev/script successfully creates a /dev/script directory, that directory is gone once the RUN command is complete.
Additionally, a command like this...
RUN cd /some/directory
...is a complete no-op. This is exactly the same thing as running sh -c "cd /some/directory" on your local system; while the cd is successful, the cd only affects the process running the cd command, and has no effect on the parent process or subsequent commands.
If you really need to place something into /dev, you can copy it into a different location in your Dockerfile (e.g., COPY test.txt /docker/test.txt), and then at runtime via your CMD or ENTRYPOINT copy it into an appropriate location in /dev.

Not working „source ~/.profile“ inside bash script

To permanently update ~/.profile with source, only working on manual input. Also reboot of the whole system won‘t update ~/.profile and i need to update it manual.
Is there a special code style to use it as working code inside a bash/shell script or is this special code not intended to be used in automated scripts?
Need it to automate installation of golang.
In the following code the line "source ~/.profile" won't work, and without any error messages, the rest is working fine:
#!/bin/bash
sudo apt update
sudo apt -y upgrade
cd ~
curl -O https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz
tar xvf go1.12.5.linux-amd64.tar.gz
sudo chown -R root:root ./go
sudo mv go /usr/local
cd ~
sudo rm go1.12.5.linux-amd64.tar.gz
sudo echo "export GOPATH=\$HOME/work" >> ~/.profile
sudo echo "export PATH=\$PATH:/usr/local/go/bin:\$GOPATH/bin" >> ~/.profile
source ~/.profile
Preferred:
Source the script itself rather than running it - then the commands in the script are run in the current shell, including the source ~/.profile.
Alternative (since this replaces the running shell, history, variable values, and other state will be lost. So there should be a very good reason to use this method):
Use exec bash or something similar instead of source ~/.profile - this replaces the currently running Bash with another instance which will itself load the new .profile.
Here is a refactoring which defers the decision to the user and cleans up the script somewhat.
#!/bin/bash
# Put this in a variable so there is only one place to update
tarball='go1.12.5.linux-amd64.tar.gz'
sudo apt update
sudo apt -y upgrade
# cd || why would you?
curl -O "https://dl.google.com/go/$tarball"
tar xvf "$tarball"
sudo chown -R root:root ./go
sudo mv go /usr/local
rm "$tarball"
printf '%s\n' "export GOPATH=\$HOME/work" \
"export PATH=\$PATH:/usr/local/go/bin:\$GOPATH/bin" >> ~/.profile
echo "$0: done. source ~/.profile or exec bash to activate new settings." >&2

How to install Python3.6.0 in centos automatically through shell scripts?

I know clearly how to install python3.6 in centos step by step.How can I do it automatically through shell scripts?
Through step-by-step methods, I use
tar -xvf python-file.tgz
./configure --prefix=/path/to/python3
make && make install
to install python3.6 successfully. What I want to do is to write these commands into a shell script and install it automatically.
My script is as follows:
#!/bin/bash
# creat folder if not exists. That folder if the directory of Python3 programs.
if [ ! -d /usr/python3 ];then
mkdir /usr/python3
else
echo "'/usr/python3' exists"
fi
DIR=`pwd`
# extract file from compressed file to tartget directory
tar -xvf Python-3.6.0.tgz -C /usr
# specify installation location
cd /usr/Python-3.6.0 && ./configure --prefix=/usr/python3
# make file and install
cd /usr/Python-3.6.0 && make && make install
# creat symlink
ln -s /usr/python3/bin/python3 /usr/bin/python3
ln -s /usr/python3/bin/pip3 /usr/bin/pip3
cd ${DIR}
pip3 install --no-index --find-links=./pkgs -r ./requirements.txt
It seems that this script isn't run line by line because /usr/python3 is empty, I want to know why.
Besides, I add && \ in each line except the last one, it seems works! I am really confused about it.

Why do I need to sudo to use leiningen (installed in /bin) on Linux?

I installed leiningen on fedora, I followed the instruction:
download the script
Paste the script(via command line) to ~/bin (PATH)
Execute the script
Run lein(I had to did it with sudo) to self-install
but every time I want to run the "lein" command, I have to do it with "sudo".
How can i fix this? or what can i do to fix this?
Note: I installed leiningen at /bin but when i cd ~/bin as the installation guide said i get and error about the folder(not exist).
You installed it in /bin and ran it with sudo to install the lein jars initially, which means they are owned (and probably only readable) by root. You should install the script at ~/bin instead. You can fix it like this:
sudo rm /bin/lein
sudo rm ~/.lein
mkdir ~/bin
cd ~/bin
wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
export PATH="${HOME}/bin:${PATH}"
lein
You should also add ${HOME}/bin to your $PATH. If you are using bash, add this to ~/.bashrc:
export PATH="${HOME}/bin:${PATH}"
You can do that using echo:
echo 'export PATH="${HOME}/bin:${PATH}"' >> ~/.bashrc

how to install nodejs 0.10.26 from binaries in Ubuntu

I am new to linux and am trying to install nodejs latest version with binaries. The solutions I have looked up suggest the installation using apt-get on some private repositories(PPA), which I do not want to do.
So I ran the following commands:
wget http://nodejs.org/dist/v0.10.26/node-v0.10.26-linux-x64.tar.gz
tar -zxvf node-v0.10.26-linux-x64.tar.gz
mv node-v0.10.26-linux-x64 node-v0.10.26
sudo cp -r node-v0.10.26 /usr/local/src
After this, I don't really know what to do. I read an article which suggested created symbolic links, which I am kind of scared to mess up with without knowing the details.
Could you please give me a set of commands to run after this in order to install node with npm? I guess npm should be a part of this binary version.
The best way to install Node.js and have the latest version (or any other version that you prefer, be it LTS or "current") is to download the official binary bundle and uncompress it. A neat way to do it:
# Use version 0.10.26
$ NODE_VERSION="v0.10.26"
# To use a newer version, for example 6.10.3, use instead:
$ NODE_VERSION="v6.10.3"
$ curl -LO http://nodejs.org/dist/$NODE_VERSION/node-$NODE_VERSION-linux-x64.tar.gz
$ tar xzf node-$NODE_VERSION-linux-x64.tar.gz
$ sudo cp -rp node-$NODE_VERSION-linux-x64 /usr/local/
$ sudo ln -s /usr/local/node-$NODE_VERSION-linux-x64 /usr/local/node
The basic idea is to move all the contents of the archive into /usr/local, then create a symlink in /usr/local/node pointing to the most recent version.
For enabling the use of the "node" executable from the command line without referencing the full path (/usr/local/node/bin/node), add /usr/local/node/bin to your $PATH (usually this involves altering the ~/.bash_profile or ~/.profile file; there's plenty of docs for how to do this).
If you need to update Node.js (suppose it's version 7.10.0), then, just extract the tarball in /usr/local and update the symbolic link so it points to the new one. You can then optionally remove the old folder.
May 2017 update
As of the "Creators Update", the commands above can now work also on Windows 10 using the "Windows Subsystem for Linux" (via bash). On Ubuntu on Windows 10, after creating the symlink like above, to add the folder to your $PATH add PATH="/usr/local/node/bin:$PATH" in the ~/.bashrc file.
I combined both of these answers for my docker container. I wanted the executable to be in the PATH already without me explicitly doing that.
#!/usr/bin/env bash
install_node() {
NODE_VERSION="v8.3.0"
curl -# "http://nodejs.org/dist/${NODE_VERSION}/node-${NODE_VERSION}-linux-x64.tar.gz" | tar -xz
cp -pr "node-${NODE_VERSION}-linux-x64" "/usr/local/"
ln -s "/usr/local/node-${NODE_VERSION}-linux-x64" "/usr/local/node"
ln -s /usr/local/node/bin/* "/usr/local/bin"
rm -rf "node-${NODE_VERSION}-linux-x64"
}
install_node
You can extract the binary anywhere and use update-alternatives command which maintain symbolic links determining default commands for example this is on my laptop.
first i extract my node node-v10.16.3-linux-x64.tar.xz on /mnt/e/WSL_Ubuntu/Downloads/node-v10.16.3-linux-x64/ folder :
xxxx#xxxxPC:.../WSL_Ubuntu/Downloads$ tar xvf node-v10.16.3-linux-x64.tar.xz
xxxx#xxxxPC:.../WSL_Ubuntu/Downloads$ cd node-v10.16.3-linux-x64/
then update-alternatives --install :
xxxx#xxxxPC:.../node-v10.16.3-linux-x64/bin$ sudo update-alternatives --install /home/wira/.local/bin/node node\
> /mnt/e/WSL_Ubuntu/Downloads/node-v10.16.3-linux-x64/bin/node 60
update-alternatives: using /mnt/e/WSL_Ubuntu/Downloads/node-v10.16.3-linux-x64/bin/node to provide /home/wira/.local/bin/node (node) in auto mode
Now i use the node on terminal
xxxx#xxxxPC:.../node-v10.16.3-linux-x64/bin$ node --version
v10.16.3
You should also update-alternatives --install on npm binaries.
I think there is still a cleaner way
NODE_VERSION="v6.7.0"
# Download
curl -LO http://nodejs.org/dist/$NODE_VERSION/node-$NODE_VERSION-linux-x64.tar.gz
# uncompress
tar xzf node-$NODE_VERSION-linux-x64.tar.gz
# selective copy
cp -R ./node-$NODE_VERSION-linux-x64/bin/* /usr/local/bin
cp -R ./node-$NODE_VERSION-linux-x64/lib/* /usr/local/lib
cp -R ./node-$NODE_VERSION-linux-x64/include/* /usr/local/include
cp -R ./node-$NODE_VERSION-linux-x64/share/* /usr/local/share
Node should be working now
$ node -e 'console.log("HI")'
HI
Hope it helps

Resources