How to remove duplicate packages between brew and apt-get - linux

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.

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

Bash scripting: if variable equals to string [duplicate]

This question already has answers here:
Meaning of "[: too many arguments" error from if [] (square brackets)
(6 answers)
Closed 2 years ago.
I am pretty new to Bash scripting and currently, working on creating an installer to install a Docker client with Docker-Compose, according to the user's OS. This script is not planned to work on each OS, the scope of this is only for Ubuntu 16.04, 18.04, CentOS 7 and 8.
I have written the below code to verify the user's OS and start the installation process (as for now, I'm trying to make it work for Ubuntu 18.04):
################
### Check OS ###
################
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
. /etc/os-release
OS=$NAME
VER=$VERSION_ID
RESULT="$OS $VER"
printf -v $RESULT "Ubuntu 18.04"
elif type lsb_release >/dev/null 2>&1; then
# linuxbase.org
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
. /etc/lsb-release
OS=$DISTRIB_ID
VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
OS=Debian
VER=$(cat /etc/debian_version)
elif [ -f /etc/SuSe-release ]; then
# Older SuSE/etc.
...
elif [ -f /etc/redhat-release ]; then
# Older Red Hat, CentOS, etc.
...
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
OS=$(uname -s)
VER=$(uname -r)
fi
echo $RESULT
#################################
### Ubuntu 18.04 Installation ###
#################################
if [ $RESULT = "Ubuntu 18.04" ]; then
echo "Installing on Ubuntu 18.04"
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
sudo apt update
apt-cache policy docker-ce
sudo apt install docker-ce
sudo systemctl status docker
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
else
echo "not working"
fi
Running the above on Ubuntu 18.04 results with:
Ubuntu 18.04
./test.sh: line 46: [: too many arguments
not working
How can I compare the output of the $RESULT to a string? Any pointers or recommendations are more than welcome!
$RESULT contains whitespaces, quote it to avoid word
splitting:
if [ "$RESULT" = "Ubuntu 18.04" ]; then

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.

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