Whiptail is not running my bash commands - linux

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.

Related

shell script to help install packages using yum

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

a weird ssh file appearing from nowhere with name pbcopyfy in .ssh directory

Well not a question as such, but more of a brainstorming thing or better say an issue. Well today I came to my server and realised that i can't git pull due to the keys being denied which was okay because for sure there were no keys in .ssh directory. All deleted and a new file (pbcopyfy there). Not sure what this means though. The content of the file are shown below:
#!/bin/sh
# Copyright (C) 2009-2017 Three Nine Consulting
# Always good practice to update packages. However ask user if they would like to do so
# For explanation on how this works and why check out https://garywoodfine.com/use-pbcopy-on-ubuntu/
read -p "Do you want to update your package repositories before proceeding ? " -n 1 -r
echo #adding new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
sudo apt update
sudo apt upgrade -y
sudo apt autoremove -y
fi
# Check to see if Xclip is installed if not install it
if [ $(dpkg-query -W -f='${Status}' xclip 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
echo 'xclip not installed .... installing now!'
sudo apt install xclip -y;
fi
# Add the aliases to the .bashrc
echo 'updating bash profile'
echo "#pbcopy & pbpaste aliases" >> ~/.bashrc
echo "alias pbcopy='xclip -selection clipboard'" >> ~/.bashrc
echo "alias pbpaste='xclip -selection clipboard -o'" >> ~/.bashrc
source ~/.bashrc
echo 'Complete! happy PbCopy'
Is this something I should be worried about or should I just get new keys and go up and running with new keys?

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.

Install node on ubuntu 17.04

I installed node 8.0.0 on Ubuntu then followed this answer to make it work but now when typing node -v it says v4.7.2. I'd like to have v.8. The one I'm trying to install. Is it a problem with the symlink or what's up ?
(Ubuntu 17:04)
I suggest you remove the ubuntu package
sudo apt-get remove --purge nodejs-*
then execute below shell script to install nodejs ... you must update your ~/.bashrc with a copy N paste of the lines mentioned in below ... vi install_node.sh
#!/bin/bash
# ............... top of cut ........................ install_node.sh
export NODE_VER=v8.0.0 # see available versions at https://nodejs.org/dist/
# pick parent dir of nodejs install ... comment out or remove ONE of below
# export CODE_PARENT_DIR=/opt/code # root owned dir ... requires you to sudo prior to npm install going forward
export CODE_PARENT_DIR=${HOME} # RECOMMENDED execute as yourself including npm install
# ......... following env vars are OK no edits needed
curr_OS=$( uname )
if [[ "${curr_OS}" == "Darwin" ]]; then
OS_ARCH=darwin-x64
elif [[ "${curr_OS}" == "Linux" ]]; then
OS_ARCH=linux-x64
else
echo "ERROR - failed to recognize OS $curr_OS"
exit 5
fi
if [[ -z ${CODE_PARENT_DIR} ]]; then
echo "ERROR - failed to see env var CODE_PARENT_DIR"
exit 5
fi
export NODE_CODEDIR=${CODE_PARENT_DIR}/nodejs
export COMSUFFIX=tar.gz
export NODE_NAME=node-${NODE_VER}
export NODE_PARENT=${NODE_CODEDIR}/${NODE_NAME}-${OS_ARCH}
export PATH=${NODE_PARENT}/bin:${PATH}
export NODE_PATH=${NODE_PARENT}/lib/node_modules
# ............... end of cut ........................ install_node.sh
# copy and paste above from ... top of cut ... to here into your file ~/.bashrc
echo
echo "NODE_CODEDIR $NODE_CODEDIR<--"
echo
echo "mkdir -p ${NODE_CODEDIR}"
echo
mkdir -p ${NODE_CODEDIR}
echo
echo "cd ${NODE_CODEDIR}"
cd ${NODE_CODEDIR}
echo
# this is compiled code NOT source
[ -f ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} ] && rm ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} # if file exists remove
echo "wget -q --show-progress https://nodejs.org/download/release/${NODE_VER}/${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}"
wget -q --show-progress https://nodejs.org/download/release/${NODE_VER}/${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}
echo
echo "tar -C ${NODE_CODEDIR} -xf ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}"
tar -C ${NODE_CODEDIR} -xf ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}
echo
[ -f ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} ] && rm ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} # if file exists remove
# ........... done ........... #
which node
node --version
# .... bottom of file install_node.sh

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