Skip to content

Instantly share code, notes, and snippets.

@woodRock
Last active March 20, 2020 05:35
Show Gist options
  • Save woodRock/a1a8c56f56fdcca1584827e4373114b9 to your computer and use it in GitHub Desktop.
Save woodRock/a1a8c56f56fdcca1584827e4373114b9 to your computer and use it in GitHub Desktop.
Creates a list of packages that I install for linux
#!/usr/bin/env bash
PKG_LIST_DIR=~/Downloads/package_list.txt
INSTALL="sudo apt-get"
REMOVE="sudo apt-get -r"
SEARCH="apt-cache search"
# Search for a package in the package manager
search(){
for PKG in "$@"
do
eval "$SEARCH $PKG"
done
}
# Tries to install a package and adds it to the list if successful
install()
{
for PKG in "$@"
do
eval "$INSTALL $PKG" && echo -e "$PKG" >> $PKG_LIST_DIR
done
}
# Tries to remove a package and removes it from the list if successful
remove(){
for PKG in "$@"
do
eval "$REMOVE $PKG" && sed -i "/$PKG/d" $PKG_LIST_DIR
done
}
# Displays the package list to the user sorted alphabetically
list()
{
cat $PKG_LIST_DIR | sort
}
# Creates the file for the package list
set_up()
{
touch $PKG_LIST_DIR
}
# Displays the command usage to the user
usage()
{
echo "Usage: pkg-get [-hl] [-S package] [-R package]"
echo " install [package] installs a package and logs it"
echo " remove [package] removes a package from the system and list"
echo " list lists all of the packages installed"
echo " help shows the usage for the program"
echo " setup sets up the package list for this command"
}
while [ "$1" != "" ]; do
case $1 in
search )
shift # Shift the arguments all up a position
search "$@"
;;
install )
shift # Shift the arguments all up a position
install "$@"
;;
remove )
shift # Dont pass the positional parameters
remove "$@"
;;
help )
usage
exit
;;
list )
list
exit
;;
setup )
set_up
exit
;;
* )
usage
exit
;;
esac
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment