Browse Source

Add some useful scripts and functions

cryobry 1 year ago
parent
commit
d90ff45a30
9 changed files with 252 additions and 0 deletions
  1. 28 0
      extract
  2. 77 0
      installpkg
  3. 91 0
      prunefiles
  4. 9 0
      send-torrent-to-htpc
  5. 30 0
      share-link
  6. 3 0
      strip-exif
  7. 8 0
      ulauncher-reload
  8. 3 0
      workstation-shutdown
  9. 3 0
      workstation-suspend

+ 28 - 0
extract

@@ -0,0 +1,28 @@
+#!/usr/bin/env bash
+
+extract () {
+	if [ -f "$1" ] ; then
+		case "$1" in
+		*.tar.bz2) tar xjf "$1" ;;
+		*.tar.gz) tar xzf "$1" ;;
+		*.bz2) bunzip2 "$1" ;;
+		*.rar) unrar e "$1" ;;
+		*.gz) gunzip "$1" ;;
+		*.tar) tar xf "$1" ;;
+		*.tbz2) tar xjf "$1" ;;
+		*.tgz) tar xzf "$1" ;;
+		*.zip) unzip "$1" ;;
+		*.Z) uncompress "$1" ;;
+		*.7z) 7z x "$1" ;;
+		*) echo "$1 cannot be extracted via extract()" ;;
+		esac
+	else
+		echo "$1 is not a valid file"
+	fi
+}
+
+# Allow script to be safely sourced
+if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
+    extract "$@"
+    exit $?
+fi

+ 77 - 0
installpkg

@@ -0,0 +1,77 @@
+#!/usr/bin/env bash
+#
+# Identify host OS and execute package manager install command on input args
+#
+
+installpkg () {
+
+	_getOS () {
+
+		# Widely supported method to retrieve host $ID
+		if [[ -e /etc/os-release ]]; then
+		    source /etc/os-release
+		else
+		    echo "No /etc/os-release found!"
+		    echo "Your OS is unsupported!"
+		    return 1
+		fi
+	}
+
+
+	_setCmdArr () {
+
+		declare -ga _CmdArr
+
+		# Create OS-specific package install command arrays
+		if [[ "$ID" == "fedora" ]]; then
+			_CmdArr=( "dnf" "install" "-y" )
+		elif [[ "$ID" == "centos" && "$VERSION_ID" -ge 8 ]]; then
+			_CmdArr=( "dnf" "install" "-y" )
+		elif [[ "$ID" == "centos" && "$VERSION_ID" -le 7 ]]; then
+			_CmdArr=( "yum" "install" "-y" )
+		elif [[ "$ID" == "ubuntu" || "$ID" == "debian" ]]; then
+			_CmdArr=( "apt-get" "install" "-y" )
+		elif [[ "$ID" == "arch" ]]; then
+			_CmdArr=( "pacman" "-Syu" )
+		else
+			"Your OS is currently unsupported! You are welcome to add your own and submit a PR."
+			return 1
+		fi
+
+		# Append sudo if not running as root
+		[[ "$(whoami)" != "root" ]] && _CmdArr=( "sudo" "${_CmdArr[@]}" )
+	}
+
+
+	_installPackage () {
+
+		# Check for input arguments
+		if [[ "$#" -ge 1 ]]; then
+			if ! "${_CmdArr[@]}" "$@"; then
+				echo "Installation failed!"
+				return 1
+			fi
+		else
+			echo "You must supply one or more packages to install!"
+			return 1
+		fi
+	}
+
+
+
+	__main () {
+
+		_getOS && \
+		_setCmdArr && \
+		_installPackage "$@" && \
+        unset _CmdArr
+	}
+
+
+	__main "$@"
+}
+
+if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
+	installpkg "$@"
+	exit $?
+fi

+ 91 - 0
prunefiles

@@ -0,0 +1,91 @@
+#!/usr/bin/env bash
+#
+# prunefiles, a function to remove all but the n latest versions of a file
+# by Bryan Roessler
+#
+# This file can be sourced directly to import `prunefiles` or run as a script
+#
+# Useful to prune rpm repositories of obsolete packages
+#
+
+declare -a _filePrefixes
+
+prunefiles () {
+
+    #############
+    # DEFAULTS ##
+    #############
+
+    # Default number of matching files to keep
+    _keepInt=1
+
+
+    #############
+    # FUNCTIONS #
+    #############
+
+    _printHelpAndExit () {
+
+    cat <<-'EOF'
+USAGE:
+pruneFiles -k 3 thisfileprefix [thatfileprefix]
+
+OPTIONS
+    -k|--keep NUMBER
+        Keep NUMBER of latest files that matches each file prefix (Default: 1)
+EOF
+
+    # Exit using passed exit code
+    [[ -z $1 ]] && exit 0 || exit "$1"
+
+}
+
+    _parseInput () {
+
+        if _input=$(getopt -o +k: -l keep: -- "$@"); then
+            eval set -- "$_input"
+            while true; do
+                case "$1" in
+                    -k|--keep)
+                        shift && _keepInt=$1
+                        ;;
+                    --)
+                        shift && break
+                        ;;
+                esac
+                shift
+            done
+        else
+            echo "Incorrect option(s) provided"
+            _printHelpAndExit 1
+        fi
+
+        _filePrefixes=( "$@" )
+    }
+
+
+    _findAndRemove () {
+
+        for _filePrefix in "${_filePrefixes[@]}"; do
+            for _file in $(find . -maxdepth 1 -type f -name "${_filePrefix}*" -printf '%T@ %p\n' | sort -r -z -n | tail -n+$(($_keepInt + 1)) | awk '{ print $2; }'); do
+                rm "$_file"
+            done
+        done
+    }
+
+
+    __main () {
+
+        _parseInput "$@"
+        _findAndRemove
+
+    }
+
+    __main "$@"
+}
+
+# Allow script to be safely sourced
+if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
+    prunefiles "$@"
+    exit $?
+fi

+ 9 - 0
send-torrent-to-htpc

@@ -0,0 +1,9 @@
+#!/usr/bin/env bash
+#
+# I develop on a laptop and prefer to send torrent files (linux isos, etc.) to my desktop
+#
+# I set .torrent links to open this script by default in firefox
+#
+
+mv "$1" "$HOME/Documents/torrents/"
+chmod +rw "$HOME/Documents/torrents/$1"

+ 30 - 0
share-link

@@ -0,0 +1,30 @@
+#!/usr/bin/env bash
+
+ssh_server="bryanroessler.com"
+ssh_files_path="/var/www/repos.bryanroessler.com/files"
+www_files_path="https://repos.bryanroessler.com/files"
+
+share-link () {
+    if [[ "$#" -lt 1 ]]; then
+    	echo "You must provide at least one argument"
+    	exit 1
+    else
+    	local -a links_array
+    	for file in "$@"; do
+    		local ext="${file#*.}"
+    		local random32
+            random32=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 32 | head -n 1)
+    		local random_fname="${random32}.${ext}"
+    		scp "${file}" "${ssh_server}:${ssh_files_path}/${random_fname}"
+    		links_array+=("$www_files_path/$random_fname")
+    	done
+    	if [[ "${#links_array[@]}" == 1 ]]; then
+    		printf '%s' "${links_array[@]}" | xclip -sel c
+    	else
+    		printf '%s\n' "${links_array[@]}" | xclip -sel c
+    	fi
+    fi
+}
+
+share-link "$@"
+exit $?

+ 3 - 0
strip-exif

@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+
+exiftool -all= "${1}"

+ 8 - 0
ulauncher-reload

@@ -0,0 +1,8 @@
+#!/usr/bin/env bash
+
+if pgrep ulauncher &>/dev/null; then
+    killall ulauncher
+    sleep 1
+fi
+
+exo-open /usr/share/applications/ulauncher.desktop

+ 3 - 0
workstation-shutdown

@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+
+ssh -t workstation.lan 'sudo systemctl poweroff'

+ 3 - 0
workstation-suspend

@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+
+ssh -t workstation.lan 'sudo systemctl suspend'