shell script to help install packages using yum - linux

I wrote the script below as i am trying to help users install the packages they need from a yum repository.
The usage of the script is ./script PACKAGE VERSION,
#!/bin/sh
PAKAGENAME=${1}
VERSION=${2}
if [[ -z ${1} ]]; then
echo "you should at least specify a component name"
echo "Usage : installrpm {COMPONENT} {VERSION}"
elif [[ -z ${2} ]]; then
echo "the latest version of the component will be installed"
sudo yum install -y ${1}
elif [[ ${1} == all ]]; then
echo "All component will be installed in latest versions available"
sudo yum remove -y PAKAGE1
sudo yum install -y PAKAGE1 --skip-broken
sudo yum remove -y PAKAGE2
sudo yum install -y PAKAGE2 --skip-broken
else
sudo yum remove -y ${1}
sudo yum install -y ${1}-${2}
fi
When i use the ./script packagename version or ./script packagename it workes but when i try to use ./script packagename all i get the following error :
the latest deployed version of the component will be installed
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
No package all available.
Error: Nothing to do
It seams that the script starts looking for a package name all even when i'm specifying the packages on this condition.
How can i fix this error and i'll be grateful for any enhacements on the script.

I think the problem depends on the second elif. You're checking the first opt (${1}) if it is equal to all but in the command ./script packagename all the opt all is the second argument. So, you can try modifying the script as shown below:
#!/bin/bash
PAKAGENAME=${1}
VERSION=${2}
if [[ -z ${1} ]]; then
echo "you should at least specify a component name"
echo "Usage : installrpm {COMPOSANT} {VERSION}"
elif [[ -z ${2} ]]; then
echo "the latest version of the component will be installed"
echo "$PAKAGENAME"
elif [[ ${2} == 'all' ]]; then
echo "All component will be installed in latest versions available"
echo "$VERSION"
else
echo "else"
fi

Related

SSH Bash script issues with if statement

I'm trying to learn how to write Bash scripts. I have this script to update my servers through ssh. I'm trying to add a check and a conditional to determine if the OS uses Yum or Apt then it will run the appropriate update commands. The if else statement seems to be wrong but I'm not sure how to correct this.
Here is the script:
#!/bin/bash
USERNAME="root"
HOSTS="host1 host2 host3"
apt_run="apt update && apt -y upgrade"
yum_run="yum check-update && yum -y update"
for HOSTNAME in ${HOSTS} ; do
ssh -l ${USERNAME} ${HOSTNAME}
find_os=$( command -v yum || command -v apt-get ) || echo "Neither
yum nor apt-get found"
if [[ $find_os='yum' ]]
then
"${yum_run}"
else
"${apt_run}"
fi
done
Here is my script for my virtual machines.
#!/bin/bash
hosts=(
leap151 kali ubuntu omv
)
for hostname in "${hosts[#]}"; do
ssh -t root#"$hostname" << 'EOF'
if type -P zypper >/dev/null; then
command zypper ref && command zypper up
elif type -P apt-get >/dev/null; then
command apt-get update && command apt-get upgrade
else
echo 'Neither zypper nor apt found!' >&2
exit 127
fi
EOF
done
Use an array for the host. Since you're using bash the builtin type is fine just for searching the executable within your PATH. See help type for more info. Use the -t option in ssh also use a heredoc just what I have/did. The exit 127 is what the shell would exit if there are no executable see man 1p exit.

error while loading shared libraries: libreadline.so.7: cannot open shared object file: No such file or directory

I'm trying to set up the environment of deepmind/dqn, https://github.com/deepmind/dqn, I run ./install_dependencies.sh to install
LuaJIT and Torch 7.0
nngraph
Xitari
AleWrap
first.But I got:
/home/dqn/torch/bin/luajit: error while loading shared libraries: libreadline.so.7: cannot open shared object file: No such file or directory
- => Torch7 has been installed successfully
Installing nngraph ... /home/dqn/torch/bin/luajit: error while
loading shared libraries: libreadline.so.7: cannot open shared object
file: No such file or directory Error. Exiting.
The install_dependencies.sh is:
# Install dependencies for Torch:
sudo apt-get update
sudo apt-get install -qqy build-essential
sudo apt-get install -qqy gcc g++
sudo apt-get install -qqy cmake
sudo apt-get install -qqy curl
sudo apt-get install -qqy libreadline-dev
sudo apt-get install -qqy git-core
sudo apt-get install -qqy libjpeg-dev
sudo apt-get install -qqy libpng-dev
sudo apt-get install -qqy ncurses-dev
sudo apt-get install -qqy imagemagick
sudo apt-get install -qqy unzip
sudo apt-get update
echo "==> Torch7's dependencies have been installed"
# Build and install Torch7
cd /tmp
rm -rf luajit-rocks
git clone https://github.com/torch/luajit-rocks.git
cd luajit-rocks
mkdir -p build
cd build
git checkout master; git pull
rm -f CMakeCache.txt
cmake .. -DCMAKE_INSTALL_PREFIX=$PREFIX -DCMAKE_BUILD_TYPE=Release
RET=$?; if [ $RET -ne 0 ]; then echo "Error. Exiting."; exit $RET; fi
make
RET=$?; if [ $RET -ne 0 ]; then echo "Error. Exiting."; exit $RET; fi
make install
RET=$?; if [ $RET -ne 0 ]; then echo "Error. Exiting."; exit $RET; fi
path_to_nvcc=$(which nvcc)
if [ -x "$path_to_nvcc" ]
then
cutorch=ok
cunn=ok
fi
# Install base packages:
$PREFIX/bin/luarocks install cwrap
$PREFIX/bin/luarocks install paths
$PREFIX/bin/luarocks install torch
$PREFIX/bin/luarocks install nn
[ -n "$cutorch" ] && \
($PREFIX/bin/luarocks install cutorch)
[ -n "$cunn" ] && \
($PREFIX/bin/luarocks install cunn)
$PREFIX/bin/luarocks install luafilesystem
$PREFIX/bin/luarocks install penlight
$PREFIX/bin/luarocks install sys
$PREFIX/bin/luarocks install xlua
$PREFIX/bin/luarocks install image
$PREFIX/bin/luarocks install env
echo ""
echo "=> Torch7 has been installed successfully"
echo ""
echo "Installing nngraph ... "
$PREFIX/bin/luarocks install nngraph
RET=$?; if [ $RET -ne 0 ]; then echo "Error. Exiting."; exit $RET; fi
echo "nngraph installation completed"
echo "Installing Xitari ... "
cd /tmp
rm -rf xitari
git clone https://github.com/deepmind/xitari.git
cd xitari
$PREFIX/bin/luarocks make
RET=$?; if [ $RET -ne 0 ]; then echo "Error. Exiting."; exit $RET; fi
echo "Xitari installation completed"
echo "Installing Alewrap ... "
cd /tmp
rm -rf alewrap
git clone https://github.com/deepmind/alewrap.git
cd alewrap
$PREFIX/bin/luarocks make
RET=$?; if [ $RET -ne 0 ]; then echo "Error. Exiting."; exit $RET; fi
echo "Alewrap installation completed"
echo
echo "You can run experiments by executing: "
echo
echo " ./run_cpu game_name"
echo
echo " or "
echo
echo " ./run_gpu game_name"
echo
echo "For this you need to provide the rom files of the respective games (game_name.bin) in the roms/ directory"
echo
when I test the code ./run_gpu {game_name}, I got:
../torch/bin/luajit: error while loading shared libraries: libreadline.so.7: cannot open shared object file: No such file or directory
when I run ldconfig -p | grep readline
My version is below 6
libreadline.so.6 (libc6,x86-64) => /lib/x86_64-linux-gnu/libreadline.so.6
libreadline.so (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libreadline.so
libguilereadline-v-18.so.18 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libguilereadline-v-18.so.18
libguilereadline-v-18.so (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libguilereadline-v-18.so
but I cannot install libreadline7 by : sudo apt-get install libreadline7-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libreadline7-dev
How could I tackle the installation of libreadline7??
This is mostly due to anaconda, try disable that from you PATH
To resolve this dependency you need to manually install the mentioned package. go to ubuntu store and search for the package.
https://packages.ubuntu.com/search?suite=default&section=all&arch=any&keywords=libreadline7&searchon=names
this will list the package information. Click on the hyperlink provided in the package description. In this case :
https://packages.ubuntu.com/bionic/libreadline7
click on any link for your region. this will start download of the debian file for the package.
once you have got the debian file you simply have to install it using dpkg command
eg :
sudo dpkg -i Desktop/libreadline7_7.0-3_amd64.deb

How to remove duplicate packages between brew and apt-get

Being a noob in Linux I started installing packages left and right, with brew and apt-get on Mint.
Now that I am running out os space, I started to browse these packages and noticed many duplicates (gcc, ag,....etc)
How to remove the duplicates in an efficient way without hurting applications that have dependencies on these applications and keep the newest versions?
1) You output all the packages that you install from the brew.
brew list >> brewList.txt
2) You output all the packages that you installed from apt-get.
dpkg-query -l >> dpkgList.txt
3) You output all the intersected packages name from dpkgList.txt and brewList.txt
grep -Fx -f brew.txt dpkg.txt >> intersectedList.txt
4) Now, remove all intersected packages from apt-get or brew. Note: I am removing packages from apt-get here.
sudo apt-get remove `cat intersectedList.txt`
/* if the package name has the same prefix, then you can use
sudo apt-get remove `cat intersectedList.txt`*
*/
So, the whole bash script that also checks every command ran correctly is as below:
brew list >> brewList.txt
if [ $? -eq 0 ]; then
dpkg-query -l >> dpkgList.txt
if [ $? -eq 0 ]; then
grep -Fx -f brew.txt dpkg.txt >> intersectedList.txt
if [ $? -eq 0 ]; then
sudo apt-get remove `cat intersectedList.txt` /* you can place * after ` symbol, if you want to remove node (or nodejs) */
if [ $? -eq 0 ]; then
echo OK
else
echo "Task not completed!"
fi
else
echo "grep -Fx -f brew.txt dpkg.txt error!"
fi
else
echo "dpkg-query error!"
fi
else
echo "brew list!"
fi
There is no automatic way to do this, but you can list installed packages through homebrew with:
brew list
and then list installed packages through Mint with:
dpkg-query -l
some packages will have small differences in their names but you will recognize them. You can then remove packages from homebrew with:
brew remove <package>
and remove packages from Mint with:
sudo apt-get remove <package>
beware that packages installed from homebrew are generally newer than packages installed from Mint.

Whiptail is not running my bash commands

I've created a bash program using whiptail to give a graphical type interface to the user to setup their system. For some reason my script isn't running any of my bash commands though, instead it seems to be cycling through outputting to my log.txt file, but no packages are being installed.
STATUS=0
touch log.txt
while [ $STATUS -lt 100 ]; do
# update apt repos
apt-get update
wait
echo "apt-get update" >> log.txt
let STATUS=STATUS+15
echo $STATUS
# update apt package
apt-get upgrade
wait
echo "apt-get upgrade" >> log.txt
let STATUS=STATUS+15
echo $STATUS
# install required packages
apt-get -y git-all nmap hydra
wait
echo "apt-get -y git-all nmap hydra" >> log.txt
let STATUS=STATUS+10
echo $STATUS
# install rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
wait
echo "cloning rbenv" >> log.txt
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'exporting PATH' >> log.txt
~/.rbenv/bin/rbenv init
wait
echo 'initializing rbenv' >> log.txt
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
wait
echo "cloning ruby-build" >> log.txt
rbenv install 2.1.4
wait
echo "installing ruby 2.1.4" >> log.txt
let STATUS=STATUS+25
echo $STATUS
done | whiptail --gauge "Setting Up Neo (THIS WILL TAKE SOME TIME)..." 40 78 0
So, to confirm my while loop is actually running, I started echoing things to log.txt. Here is the output:
apt-get update
apt-get upgrade
apt-get -y git-all nmap hydra
cloning rbenv
exporting PATH
initializing rbenv
cloning ruby-build
installing ruby 2.1.4
What have I done wrong?
First, since you have no backgrounded processes, wait is not doing anything.
Second, since whiptail is reading stdin, you need to ensure that stdout from all apt-get, git, rbenv, etc commands are redirected to stderr, or better, to your log.
# update apt repos
echo "apt-get update" >> log.txt
apt-get update >>log.txt 2>&1
(( STATUS += 15 ))
echo $STATUS
# update apt package
echo "apt-get upgrade" >> log.txt
apt-get upgrade >> log.txt 2>&1
(( STATUS += 15 ))
echo $STATUS
and so on.

How to write an install script for ubuntu

I just developed an opengl game in linux (ubuntu).. Now I would like to write a setup script for the same which installs the game directly into the apt using the command..
sudo apt-get install ...
so that it runs from anywhere throughout linux without going into the specified folder for the game. Anyone knows how to do that ?
http://blog.boxedice.com/2010/02/05/how-to-create-a-debian-deb-package/
http://ptspts.blogspot.com/2010/02/how-to-create-debianubuntu-package-deb.html
http://wiki.debian.org/HowToPackageForDebian
etc
#!/bin/bash
set -eu -o pipefail # fail on error , debug all lines
sudo -n true
test $? -eq 0 || exit 1 "you should have sudo priveledge to run this script"
echo installing the must-have pre-requisites
while read -r p ; do sudo apt-get install -y $p ; done < <(cat << "EOF"
perl
zip unzip
exuberant-ctags
mutt
libxml-atom-perl
postgresql-9.6
libdbd-pgsql
curl
wget
libwww-curl-perl
EOF
)
echo installing the nice-to-have pre-requisites
echo you have 5 seconds to proceed ...
echo or
echo hit Ctrl+C to quit
echo -e "\n"
sleep 6
sudo apt-get install -y tig

Resources