Shell - Creating text file which appends content - Using cat - linux

I have this code for example:
rm /etc/rc.local
cat <<EOF >/etc/rc.local
#!/bin/sh -e
apt-get update && apt-get -y install git-core
EOF
Is there a way instead of having it so I don't need to delete the original rc.local and make a new one, but instead I can use pretty much the same code but append this content to the bottom of the rc.local file, rather than recreating it?

Try this :
cat <<EOF >>/etc/rc.local
apt-get update && apt-get -y install git-core
EOF
Or simply :
echo 'apt-get update && apt-get -y install git-core' >> /etc/rc.local

Related

NodeJS Bluez Ubuntu Dockerfile Error

I have a docker file that is supposed to build a container to run a simple Bluetooth scanning program written with nodejs that depends on bluez, Ubuntu and Noble.js but when I run it I get the following error that stops me from building the container when running a docker build -t. Here is the Dockerfile. And the error,
48 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
wget: invalid option -- 'f'
wget: invalid option -- 's'
Usage: wget [OPTION]... [URL]...
Try `wget --help' for more options.
The command '/bin/sh -c apt-get update && apt-get install -y vim python3 python3-dev python3-pip python3-virtualenv python3-wheel gcc build-essential libglib2.0-dev libbluetooth-dev libboost-python-dev git libdbus-1-dev libudev-dev libical-dev libreadline-dev wget curl --no-install-recommends && wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.49.tar.xz tar -xf bluez-5.49.tar.xz cd bluez-5.49 ./configure make make install curl -sL https://deb.nodesource.com/setup_9.x -o nodesource_setup.sh bash nodesource_setup.sh apt-get update && apt-get install -y apt-get install nodejs ln -s /usr/bin/nodejs /usr/bin/node rm -rf /var/lib/apt/lists/*' returned a non-zero code: 2
Look at the following section, particularly where tar is first called:
wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.49.tar.xz tar -xf bluez-5.49.tar.xz cd bluez-5.49 ./configure make make install curl -sL https://deb.nodesource.com/setup_9.x -o nodesource_setup.sh bash nodesource_setup.sh apt-get update && apt-get install -y apt-get install nodejs ln -s /usr/bin/nodejs /usr/bin/node rm -rf /var/lib/apt/lists/*
When executing multiple commands in succession on a single line, you need to separate those commands with an operator like &&.
The && operator will run a command, but only if the first succeeds
The || operator will run a command, but only if the first fails
The ; operator will run a command, regardless if the first succeeds or fails
In addition you have a stray apt-get install in your command, most likely a copy/paste error. The section above should look like this:
wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.49.tar.xz && tar -xf bluez-5.49.tar.xz && cd bluez-5.49 && ./configure && make && make install && curl -sL https://deb.nodesource.com/setup_9.x -o nodesource_setup.sh && bash nodesource_setup.sh && apt-get update && apt-get install -y nodejs && ln -s /usr/bin/nodejs /usr/bin/node && rm -rf /var/lib/apt/lists/*
As an additional note, the ln -s /usr/bin/nodejs /usr/bin/node is probably unnecessary and will cause an error, as the nodejs package already appears to create /usr/bin/node.

Docker, running NVM script in a new bash shell

I have the following in my Dockerfile:
run apt-get update; \
apt-get install -y curl && \
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
Following this line of code, I need to run a command in a new bash shell so that the environment variable set from the NVM script are used.
I have tired the following to install Nodejs and it does not work:
run ["/bin/bash", "-c", "nvm install 8.7.0"]
What can I do?
It's better to use a Dockerhub repo and use it in your Dockerfile.
You can check this repositorie or this link for more repositories, please read description before choosing a repositorie.
So for example, you can add the code line below in your Dockerfile it will pull the nvm image and install it then add your app instructions.
FROM livingdocs/nvm
Or you can read their Dockerfile and use the command they used it to install nvm
ADD ./.nvmrc /app/.nvmrc
RUN bash -c '. /usr/share/nvm/nvm.sh && cd /app && nvm install && nvm alias default'
if it didn't put this one from another repositorie:
RUN sudo apt-get update && \
sudo apt-get install -y build-essential libssl-dev libmysqlclient-dev && \
sudo apt-get clean && \
sudo rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN curl --location https://raw.github.com/creationix/nvm/master/install.sh | sh && \
sudo /bin/bash -c "echo \"[[ -s \$HOME/.nvm/nvm.sh ]] && . \$HOME/.nvm/nvm.sh\" >> /etc/profile.d/npm.sh" && \
echo "[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh" >> $HOME/.bashrc

Why does this not work to configure node using nvm and yarn on remote VM?

I am trying to automate VM configuration with a script and am having some trouble getting access to some path variables that get set in either ~/.bashrc, ~/.bash_profile, or ~/.profile.
My remote VM is running ubuntu 14.04 LTS and I am deploying over ssh.
This is the array that gets joined together to be run as a bash command to configure the vm by installing nvm:
return [
rm -rf ~/.nvm,
sudo apt-get update,
sudo apt-get install -y build-essential libssl-dev,
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nvm.sh,
bash install_nvm.sh,
echo "source ~/.nvm/nvm.sh" >> ~/.bash_profile
].join('\n');
return [
`rm -rf ~/.nvm`,
`sudo apt-get update`,
`sudo apt-get install -y build-essential libssl-dev`,
`curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nvm.sh`,
`bash install_nvm.sh`,
`echo "source ~/.nvm/nvm.sh" >> ~/.bash_profile`
].join('\n');
But when when I run the next script that actually installs node and yarn, it cannot find nvm:
return [
`nvm install ${config.node.version}`,
`nvm use ${config.node.version}`,
`echo "using node $(node -v) and npm $(npm -v)"`,
`curl -o- -L https://yarnpkg.com/install.sh | bash`,
'echo "export PATH="$HOME/.yarn/bin:$PATH"" >> ~/.bash_profile',
].join('\n');
This is the error:
bash: nvm: command not found
bash: line 1: nvm: command not found`
I don't want to ssh in and manually add anything to any of the various profiles. I'd like it all to be done by the script. I also want to avoid sourcing ~/.nvm/nvm.sh or sourcing any of the profiles when the ssh session begins. I was under the impression that an ssh session automatically sources ~/.bash_profile, which should then read from those variables correct? If not, then how else can I configure my deployment script to automatically have access to these variables?
Based on the fact that you are using && as you said in your comments I would add a line to actually source ~/.nvm/nvm.sh before running the nvm commands. You likely don't have the command available at the shell until that has been run.
Change this:
return [
`rm -rf ~/.nvm`,
`sudo apt-get update`,
`sudo apt-get install -y build-essential libssl-dev`,
`curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nvm.sh`,
`bash install_nvm.sh`,
`echo "source ~/.nvm/nvm.sh" >> ~/.bash_profile`
].join('\n');
To this:
return [
`rm -rf ~/.nvm`,
`sudo apt-get update`,
`sudo apt-get install -y build-essential libssl-dev`,
`curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nvm.sh`,
`bash install_nvm.sh`,
`echo "source ~/.nvm/nvm.sh" >> ~/.bash_profile`,
`source ~/.nvm/nvm.sh`
].join('\n');

cannot 'sudo' inside of bash if statement

I've dual linux boot i'm newbie in bash
when running the following script i got strange error:
if [[ 'grep -i fedora /etc/issue' ]]; then
echo "the OS is Fedora"
$(sudo yum update -y && sudo yum upgrade -y)
else
echo "the OS is Ubuntu"
$(sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y)
fi
error : ./server_update.sh: line 9: Loaded: command not found
It's attempting to execute the output of your apt-get/yum commands, lose the $(..)
You also have an issue at the start:
if [[ -n "$(grep -i fedora /etc/issue)" ]]; then
is the correct way to check if a string exists.
Your code should then look like this:
if [[ -n "$(grep -i fedora /etc/issue)" ]]; then
echo "the OS is Fedora"
sudo yum update -y && sudo yum upgrade -y
else
echo "the OS is Ubuntu"
sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y
fi

Bash string manipulation works differently in shell than .sh file?

I have a script to get and setup the latest NodeJS on my .deb system:
echo "Downloading, building and installing latest NodeJS"
sudo apt-get install python g++ make checkinstall
mkdir /tmp/node_build && cd $_
curl -O "http://nodejs.org/dist/node-latest.tar.gz"
tar xf node-latest.tar.gz && cd node-v*
NODE_VERSION="${PWD#*v}"
#NODE_VERSION=python -c "print '$PWD'.split('-')[-1][1:]"
echo "Installing NodeJS" $NODE_VERSION
./configure
sudo checkinstall -y --install=no --pkgversion NODE_VERSION
sudo dpkg -i node_$NODE_VERSION
Unfortunately it doesn't work; as the echo line outputs:
Installing NodeJS i8/dir-where-runnning-script-from/node-v0.10.24
It does work from the shell though:
$ cd /tmp/node_build/node-v0.10.24 && echo "${PWD#*v}"
0.10.24
Is there another "v" in the path, like right before the "i8/"? #*v will remove through the first "v" in the variable; I'm pretty sure you want ##*v which'll remove through the last "v" in the variable. (Technically, # removes the shortest matching prefix, and ## removes the longest match). Thus:
NODE_VERSION="${PWD##*v}"
Should work.
Try this
sudo checkinstall -y --install=no --pkgversion "${NODE_VERSION##*v}"

Resources