Shell script to verify docker installation on Linux - linux

I want to write a shell script which verifies whether docker is installed or not.
If docker is installed:
$ docker -v
Docker version 1.7.0, build 0baf609
$ echo $?
0
If docker is not installed:
$ docker -v
The program 'docker' is currently not installed. You can install it by typing:
apt-get install docker
$ echo $?
127
Here is my script:
#!/bin/bash
docker -v
if echo $? = 128 ; then
echo "The program 'docker' is currently not installed."
else
echo "Continuing with dockerized way"
fi
here for testing purpose, I ran it on the machine where docker is not installed, I kept 127 = 128, condition is wrong, so it should go in else, but still it prints The program 'docker' is currently not installed. I would like to know what I am missing here.

The correct syntax is:
if [ $? -eq 128 ]; then
...
To make it even more robust, you might want to check:
if [ $? -ne 0 ]; then
...

Related

How to install gprbuild on Linux - Centos7

I have recently dowloaded GNAT Community on my Linux machine (Centos7).
Within /home/parallels/opt/GNAT/2019 there is a folder gprbuild, my understanding is that to install this I need to execute the bootstrap.sh script that is located within gprbuild:
/home/parallels/opt/GNAT/2019/gprbuild/bootstrap.sh
I try to execute the bootstrap.sh script like so...
[parallels#localhost gprbuild]$ ./bootstrap.sh
Then I recieve this error message...
./bootstrap.sh: line 87: gnatmake: command not found
Here is the bootstrap.sh script...
# bootstrap.sh - a simple bootstrap for building gprbuild with xmlada
progname=bootstrap
prefix=/usr/local
bindir=/bin
datarootdir=/share
libexecdir=/libexec
srcdir=$PWD
xmlada_src=../xmlada
CC=${CC:-cc}
GNATMAKE=${GNATMAKE:-gnatmake}
CFLAGS=${CFLAGS:-$CFLAGS}
GNATMAKEFLAGS=${GNATMAKEFLAGS:--j0}
usage() {
cat >&2 <<EOF
usage: $progname [options]
Options [defaults in brackets]:
--prefix=DIR installation prefix [$prefix]
--bindir=DIR user executables [PREFIX/bin]
--libexecdir=DIR program executables [PREFIX/libexec]
--datarootdir=DIR read-only arch.-independent data root [PREFIX/share]
--srcdir=DIR source code path [$PWD]
--with-xmlada=DIR xmlada source path [$xmlada_src]
--build build only but do not install
--install install only, skip build steps
Environment variables:
CC specify C compiler [$CC]
CFLAGS set C and Ada compilation flags [$CFLAGS]
DESTDIR optional for staged installs
GNATMAKE specify gnatmake Ada builder [$GNATMAKE]
GNATMAKEFLAGS additional Ada builder flags [$GNATMAKEFLAGS]
EOF
exit 0
}
error() {
printf -- "%s: $1" "$progname" "${#:2}" >&2
exit 1
}
while :; do
case $1 in
--prefix=?*) prefix=${1#*=} ;;
--bindir=?*) bindir=${1#*=} ;;
--libexecdir=?*) libexecdir=${1#*=} ;;
--datarootdir=?*) datarootdir=${1#*=} ;;
--srcdir=?*) srcdir=${1#*=} ;;
--with-xmlada=?*) xmlada_src=${1#*=} ;;
--build) MODE="build";;
--install) MODE="install";;
-h|-\?|--help) usage ;;
*=*) error '%s: Requires a value, try --help\n' "$1" ;;
-?*) error '%s: Unknown option, try --help\n' "$1" ;;
*) break # End of arguments.
esac
shift
done
set -e
inc_flags="-I$srcdir/src -I$srcdir/gpr/src -I$xmlada_src/sax -I$xmlada_src/dom \
-I$xmlada_src/schema -I$xmlada_src/unicode -I$xmlada_src/input_sources"
# Programs to build and install
bin_progs="gprbuild gprconfig gprclean gprinstall gprname gprls"
lib_progs="gprlib gprbind"
# Build
if [ "x"${MODE} == "x" ] || [ ${MODE} == "build" ];
then
command $CC -c $CFLAGS "$srcdir"/gpr/src/gpr_imports.c
for bin in $bin_progs; do
command $GNATMAKE $inc_flags "$bin"-main -o "$bin" $CFLAGS $GNATMAKEFLAGS -largs gpr_imports.o
done
for lib in $lib_progs; do
command $GNATMAKE $inc_flags "$lib" $CFLAGS $GNATMAKEFLAGS -largs gpr_imports.o
done
fi;
# Install
if [ "x"${MODE} == "x" ] || [ ${MODE} == "install" ];
then
mkdir -p "$DESTDIR$prefix$bindir"
mkdir -p "$DESTDIR$prefix$libexecdir"/gprbuild
mkdir -p "$DESTDIR$prefix$datarootdir"/gprconfig
mkdir -p "$DESTDIR$prefix$datarootdir"/gpr
install -m0755 $bin_progs -t "$DESTDIR$prefix$bindir"
install -m0755 $lib_progs -t "$DESTDIR$prefix$libexecdir"/gprbuild
install -m0644 "$srcdir"/share/gprconfig/*.xml -t "$DESTDIR$prefix$datarootdir"/gprconfig
install -m0644 "$srcdir"/share/gprconfig/*.ent -t "$DESTDIR$prefix$datarootdir"/gprconfig
install -m0644 "$srcdir"/share/_default.gpr "$DESTDIR$prefix$datarootdir"/gpr/_default.gpr
fi
I have been told that I need to install xmlada prior to installing gprbuild, then I have read elsewhere that I need to install gprbuild to be able to install xmlada!
I have a similair issue when attempting to install xmlada, the shell script within the xmlada folder is called install-sh, when I attempt to install this I am told there is no input file specified...
[parallels#localhost xmlada]$ ./install-sh
./install-sh: no input file specified.
I apreciate this is really two questions in one, but I felt I had to explain it this way, as I am unsure which library needs to be installed first, and also how do I actually install them.
Any help would be greatly apreciated! I hope you're all having a good weekend... :)
Thanks,
Lloyd
Just install gcc-ada, or search in your package manager for gcc-ada (may change its name), gnat* commands come in this package

Python virtual environment not activating from bash script

Now before you mark this as a duplicate, I have tried the solution posted here and they aren't working for me. I tried making an alias and I tried creating a function as so:
activate () {
echo Activating Virtual Environment...
source alexa/bin/activate
}
activate
But my script just gets run through without a virtual environment getting activated. The script is being run from the same directory as my virtual environment directory, alexa.
For clarity, the other solution I tried was to make an alias:
alias activate="source alexa/bin/activate"
activate
That didn't work and gave me an error that ./alexaEnvSetup.sh: line 43: activate: command not found.
Any thoughts or ideas?
EDIT: I think it is worth mentioning that the echo command does print out when I do this. So the function is getting entered. The virtual environment is just not getting activated.
EDIT: Adding full code:
#!/bin/bash
if [[ "$OSTYPE" == "linux-gnu" ]]; then
echo Operating system: Linux
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo Operating system: Mac OSX
echo
# Install Python 3.6.5 using `curl`
curl -O https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
tar xf Python-3.6.5.tgz
cd Python-3.6.5
./configure
make
make install
echo
echo Python Version 3.6.5 Installed
echo
# Install Pip
curl -O http://bootstrap.pypa.io/get-pip.py
/usr/local/bin/python3.6 get-pip.py
echo
echo Pip Installed
echo
# Install virtualenv
pip install virtualenv
echo
echo Virtual Environment Installed
virtualenv -p python3 alexa
echo Created Virtual Environment, \"alexa\"
activate () {
echo Activating Virtual Environment...
source /Users/XXXX/Auto-Lab/Commerce/alexa/bin/activate
}
export -f activate
activate
echo Virtual Environment, \"alexa\", Created and Activated
echo
# All packages (time, urllib, and json) should come default with Python3
elif [[ "$OSTYPE" == "cygwin" ]]; then
# POSIX compatibility layer and Linux environment emulation for Windows
echo Operating system: Cygwin
elif [[ "$OSTYPE" == "msys" ]]; then
# Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
echo Operating system: Msys
elif [[ "$OSTYPE" == "win32" ]]; then
echo Operating system: Windows32
elif [[ "$OSTYPE" == "freebsd"* ]]; then
echo Operating system: FreeBSD
else
echo Operating system unknown.
fi
use the full path to the activate script in the function: source /path/to/activate
export the function: export -f activate
ensure the script is a bash script: #!/bin/bash
The following works for me using Anaconda virtual env, perhaps it will work with yours also?
#!/usr/bin/env bash
# do bash stuff
# Python env
PATH=/home/username/path/to/activate/bin
python -u /script/to/run

Installing and running .sh file in linux mint

I'm using Linux mint and I want to install COMSOL. The problem is that, when I run below code in terminal, there is an error which doesn't make sense. Could anyone solve the problem?
The code I wrote:
./COMSOL_Server_Workaround.sh
the error is:
COMSOL Server Installer Work-around Script
2016 TeAM SolidSQUAD-SSQ
./COMSOL_Server_Workaround.sh: 14: [: Linux: unexpected operator
./COMSOL_Server_Workaround.sh: 15: [: Linux: unexpected operator
-e ERROR: This script should be executed
from the COMSOL Server installation directory
(where comsolsetup.log resides)
picture of files
COMSOL_Server_Workaround.sh contains:
#!/bin/sh
echo
echo COMSOL Server Installer Work-around Script
echo 2016 TeAM SolidSQUAD-SSQ
echo
# assume working dir is COMSOL installation root
COMSOLROOT="$PWD"
# check architecture
[ "`uname`" == "Linux" ] && COMSOL_TARS="fl fl_setup_glnxa64 fl_setup_maclnx fl_jvm_glnxa64 fl_glnxa64 mph_maclnx mph_glnxa64"
[ "`uname`" == "Darwin" ] && COMSOL_TARS="fl fl_setup_maclnx fl_maclnx fl_jvm_maci64 fl_maci64 mph_maclnx mph_maci64"
# tell user he has to run the script from COMSOL Server folder
[ ! -e "$COMSOLROOT/comsolsetup.log" ] && echo -e "ERROR: This script should be executed\n from the COMSOL Server installation directory\n (where comsolsetup.log resides)" && exit 1
# check if we can write to folder
[ ! -w "$COMSOLROOT/comsolsetup.log" ] && echo -e "ERROR: Cannot write to folder \"$COMSOLROOT\".\n If COMSOL Server was installed as root, run this script as root too\n otherwise run it as user which made the installation of COMSOL Server!" && exit 2
# let us extract the source location and check if it accessible
COMSOLDVD="`grep "^cs.installroot" "$COMSOLROOT/comsolsetup.log" | awk '{print $3}'`"
echo "COMSOL SERVER Installation Path = $COMSOLROOT"
echo "COMSOL Server Installation Source Path = $COMSOLDVD"
# check if source is accessible
[ ! -r "$COMSOLDVD/setupconfig.ini" ] && echo "The installaton source \"$COMSOLDVD\" id not accessible! Make sure the DVD is mounted" && exit 3
# now start untaring FL archives
for COMSOL_TAR in $COMSOL_TARS
do
tar -xf "$COMSOLDVD/archives/$COMSOL_TAR.isa" -C "$COMSOLROOT"
done
# Null the bin\tomcat\conf\login.properties
echo > "$COMSOLROOT/bin/tomcat/conf/login.properties"
# Mission complete
echo
echo All done! Enjoy!
echo
exit 0

why the command script generated for my npm package is different than other package?

I published a npm package myself: makeTimeTable
I installed it happily in cygwin:
npm install -g maketimetable
But when I try to run the command line, an error is thrown:
/node_modules/maketimetable/cli.js: No such file or directory
Finally I figure out that the command script file(C:\Users\xxx\AppData\Roaming\npm\maketimetable) generated when installing my package is different than other global command line tools I installed, below is mine:
"$basedir/node_modules/maketimetable/cli.js" "$#"
exit $?
other global command line tools' script is like this:
#!/bin/sh
basedir=`dirname "$0"`
case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/node_modules/eslint/bin/eslint.js" "$#"
ret=$?
else
node "$basedir/node_modules/eslint/bin/eslint.js" "$#"
ret=$?
fi
exit $ret
So in other tools' script, it will distinguish whether I'm using cygwin, it will treat the path and directory a little differently. But why my package's script is different than those? How can I make npm to generate the same kind of script for my package?
Try putting #!/usr/bin/env node as the first line of your cli.js.

Cross-compiling Node.js for ARM6 (Raspberry Pi)

I'm trying to get node.js v0.7.9 to compile for the raspberry pi, but as node and v8 are quite large, I'm hoping to be able to cross-compile on another more powerful PC. I'm using the linux-x86 arm-bcm2708-linux-gnueabi toolchain from https://github.com/raspberrypi/tools and have used them to successfully build other executables for the system. I ended up setting the CC,CXX,CPP,STRIP,OBJCOPY,etc. variables to the toolchain equivalents in the environmental variables and ran configure with: ./configure --dest-cpu=arm --without-snapshot to get the final executable. Copying it over to the system and running it however produces the following error:
Extension or internal compilation error at line 0.
Segmentation fault
However, the segmentation fault doesn't happen for any of the non-javascript tasks like node --version and node --help. Are there any CFLAGS/CXXFLAGS I might be missing causing this problem? Bit confused....
NodeJS is available pre-compiled for a few Linux distributions:
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
Since NodeJS is still in testing phase for Debian, in order to install NodeJS on my Raspberry Pi under Debian wheezy, I do:
sudo su
echo deb ftp://ftp.dk.debian.org/debian/ sid main > /etc/apt/sources.list.d/sid.list
apt-get update
apt-get install nodejs
rm /etc/apt/sources.list.d/sid.list
apt-get update
exit
NodeJS works very fine and stable on my Raspberry Pi.
Just a note - this worked great for me but didn't install NPM. So if you want NPM (which you most assuredly do I would think) make sure to run
apt-get install npm
after installing node before you remove the update locations from the update list.
I've been working on this a bit since the question was originally asked, even added some patches to help auto-detect cross-compiler settings. Node.js in the repositories is (at the moment) a rather old version, and may or may not support the full hard-float (VFP) architecture.
For a full detailed HOWTO, see Nathan Rajlich's write up at http://n8.io/cross-compiling-nodejs-v0.8/
I've posted binaries for others who don't want to go through all this hassle for the same hardware at https://gist.github.com/3245130
Here's the script I wrote to automatically cross compile NodeJS for ARMv6 (Raspbery Pi) using Jenkins.
https://gist.github.com/hertzg/12c2d7fc40f68ff6deeb
I've used #Adam M-W's 'packing' snippet
The script will automatically download everything required and start building.
Just note that current (v0.10.30) version will not compile due to this issue:
https://github.com/joyent/node/issues/8062#issuecomment-52541037
Run with _PARAMS_NODEJS_SOURCE_ARCHIVE_URL=http://nodejs.org/dist/v0.10.29/node-v0.10.29.tar.gz to get the latest cross compile-able version.
#!/bin/bash
set -e
set -x
### IF we dont have archive url prefix
if [ -z "$_PARAMS_NODEJS_SOURCE_ARCHIVE_URL" ]; then
_PARAMS_NODEJS_SOURCE_ARCHIVE_URL=$(wget -qO- http://nodejs.org/dist/latest/ | egrep -o 'node-v[0-9\.]+.tar.gz' | tail -1);
_PARAMS_NODEJS_SOURCE_ARCHIVE_URL="http://nodejs.org/dist/latest/"$_PARAMS_NODEJS_SOURCE_ARCHIVE_URL
fi
if [ -z "$_PRAMS_RPI_TOOLS_SOURCE_ARCHIVE_URL" ]; then
_PRAMS_RPI_TOOLS_SOURCE_ARCHIVE_URL="https://github.com/raspberrypi/tools/archive/master.tar.gz"
fi;
NODEJS_SOURCE_ARCHIVE_FILENAME=$(basename $_PARAMS_NODEJS_SOURCE_ARCHIVE_URL)
NODEJS_SOURCE_DIRECTORY=${NODEJS_SOURCE_ARCHIVE_FILENAME%.tar.gz}
#Download NodeJS
echo "-> Searching for NodeJS "$NODEJS_SOURCE_ARCHIVE_FILENAME;
if [ ! -e "$PWD/$NODEJS_SOURCE_ARCHIVE_FILENAME" ]; then
echo "--> Downloading from "$_PARAMS_NODEJS_SOURCE_ARCHIVE_URL;
wget --no-check-certificate -O $NODEJS_SOURCE_ARCHIVE_FILENAME $_PARAMS_NODEJS_SOURCE_ARCHIVE_URL
echo "--> Download finished!"
fi;
echo "--> Extracting"
rm -rf $NODEJS_SOURCE_DIRECTORY
tar --overwrite -xf $NODEJS_SOURCE_ARCHIVE_FILENAME
echo "--> Linking"
ln -snf "$PWD/$NODEJS_SOURCE_DIRECTORY" "$PWD/node"
echo "-> Done!"
echo "-> Searching Raspberry Pi Toolset";
if [ ! -d "$PWD/rpi" ]; then
if [ ! -e "$PWD/rpi-tools.tar.gz" ] || [ -s "$PWD/rpi-tools.tar.gz" ]; then
echo "--> Downloading from "$_PRAMS_RPI_TOOLS_SOURCE_ARCHIVE_URL
wget --no-check-certificate -O "rpi-tools.tar.gz" $_PRAMS_RPI_TOOLS_SOURCE_ARCHIVE_URL
echo "--> Download finished"
else
echo "--> Found rpi-tools.tar.gz."
fi
echo "--> Extracting"
tar xf "rpi-tools.tar.gz"
echo "--> Linking tools-master to rpi"
ln -snf "$PWD/tools-master" "$PWD/rpi"
else
echo "-> found"
fi;
echo "-> Done!"
echo "-> Cross-Compile..."
echo "--> Setup ENV"
export PATH="$PWD/rpi/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin:"$PATH
export HOST="arm-bcm2708hardfp-linux-gnueabi"
export CC="${HOST}-gcc"
export CXX="${HOST}-g++"
export AR="${HOST}-ar"
export RANLIB="${HOST}-ranlib"
export LD="${HOST}-ld"
export CPP="${HOST}-gcc -E"
export STRIP="${HOST}-strip"
export OBJCOPY="${HOST}-objcopy"
export OBJDUMP="${HOST}-objdump"
export NM="${HOST}-nm"
export AS="${HOST}-as"
"${HOST}-gcc" --version
echo "--> Waiting 5s to contiue"
pushd "$PWD/node"
sleep 5
echo "--> Clean"
make clean
echo "--> Configure"
./configure --prefix=/ --without-snapshot --dest-cpu=arm --dest-os=linux
echo "--> Build"
VERSION=${NODEJS_SOURCE_DIRECTORY##node-}
export BINARYNAME=node-${VERSION}-linux-arm-armv6j-vfp-hard
mkdir ${BINARYNAME}
make install DESTDIR=${BINARYNAME} V=1 PORTABLE=1
echo "--> Pack"
cp README.md ${BINARYNAME}
cp LICENSE ${BINARYNAME}
cp ChangeLog ${BINARYNAME}
tar -czf ${BINARYNAME}.tar.gz ${BINARYNAME}
echo "--> Cleanup"
popd
mv $PWD"/node/${BINARYNAME}.tar.gz" "./"
echo "-> Done!"
I managed to cross compile nodejs version 0.10 and run it on Freescale i.MX6.
I created a cross compile script to setup the environment and the execute make.
The script basically just set the CC, CXX etc. variables to my cross compile tools plus it sets the arch and some other flags for the compiler.
Then it runs the configure with dest-cpu=arm
I tried to also link in the V8 library (which I also cross compiled), but it would not compile when I included that flag plus the path in configure.
When I compile node, does it compile it's own V8, since I manage to compile without pointing to V8 library (I can see it is compiling some V8 stuff during the compilation)?
Just a heads up you can now just do:
sudo apt-get install nodejs npm
For node 0.12, http://conoroneill.net//download-compiled-version-of-nodejs-0120-stable-for-raspberry-pi-here has instructions + pre-built binaries.

Resources