Browse Source

Improve formatting

Bryan Roessler 2 years ago
parent
commit
5049ac23cf
10 changed files with 52 additions and 84 deletions
  1. 1 0
      extract
  2. 19 22
      installpkg
  3. 25 39
      prunefiles
  4. 6 4
      share-link
  5. 0 2
      speedtest-compare
  6. 1 1
      strip-exif
  7. 0 8
      ulauncher-reload
  8. 0 2
      update-git-hooks
  9. 0 3
      workstation-shutdown
  10. 0 3
      workstation-suspend

+ 1 - 0
extract

@@ -1,4 +1,5 @@
 #!/usr/bin/env bash
+# This script/function will unzip most filetypes automatically
 
 extract () {
 	if [ -f "$1" ] ; then

+ 19 - 22
installpkg

@@ -1,11 +1,10 @@
 #!/usr/bin/env bash
-#
 # Identify host OS and execute package manager install command on input args
 #
 
-installpkg () {
+installpkg() {
 
-	_getOS () {
+	getOS() {
 
 		# Widely supported method to retrieve host $ID
 		if [[ -e /etc/os-release ]]; then
@@ -18,36 +17,36 @@ installpkg () {
 	}
 
 
-	_setCmdArr () {
+	setCmdArr () {
 
-		declare -ga _CmdArr
+		declare -ga CMD_ARR
 
 		# Create OS-specific package install command arrays
 		if [[ "$ID" == "fedora" ]]; then
-			_CmdArr=( "dnf" "install" "-y" )
+			CMD_ARR=( "dnf" "install" "-y" )
 		elif [[ "$ID" == "centos" && "$VERSION_ID" -ge 8 ]]; then
-			_CmdArr=( "dnf" "install" "-y" )
+			CMD_ARR=( "dnf" "install" "-y" )
 		elif [[ "$ID" == "centos" && "$VERSION_ID" -le 7 ]]; then
-			_CmdArr=( "yum" "install" "-y" )
+			CMD_ARR=( "yum" "install" "-y" )
 		elif [[ "$ID" == "ubuntu" || "$ID" == "debian" ]]; then
-			_CmdArr=( "apt-get" "install" "-y" )
+			CMD_ARR=( "apt-get" "install" "-y" )
 		elif [[ "$ID" == "arch" ]]; then
-			_CmdArr=( "pacman" "-Syu" )
+			CMD_ARR=( "pacman" "-Syu" )
 		else
-			"Your OS is currently unsupported! You are welcome to add your own and submit a PR."
+			echo "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[@]}" )
+		[[ "$(whoami)" != "root" ]] && CMD_ARR=( "sudo" "${CMD_ARR[@]}" )
 	}
 
 
-	_installPackage () {
+	installPackage() {
 
 		# Check for input arguments
 		if [[ "$#" -ge 1 ]]; then
-			if ! "${_CmdArr[@]}" "$@"; then
+			if ! "${CMD_ARR[@]}" "$@"; then
 				echo "Installation failed!"
 				return 1
 			fi
@@ -58,17 +57,15 @@ installpkg () {
 	}
 
 
+	main() {
 
-	__main () {
-
-		_getOS && \
-		_setCmdArr && \
-		_installPackage "$@" && \
-        unset _CmdArr
+		getOS && \
+		setCmdArr && \
+		installPackage "$@" && \
+        unset CMD_ARR
 	}
 
-
-	__main "$@"
+	main "$@"
 }
 
 if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then

+ 25 - 39
prunefiles

@@ -1,87 +1,73 @@
 #!/usr/bin/env bash
+# prunefiles, a script/function to remove all but the n latest versions of a file
 #
-# 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 #
-    #############
+    declare -ag PREFIXES
+    declare -g KEEP_INT=1 # default # of files to keep
 
-    _printHelpAndExit () {
+    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)
+	-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 () {
+    parseInput () {
 
         if _input=$(getopt -o +k: -l keep: -- "$@"); then
             eval set -- "$_input"
             while true; do
                 case "$1" in
                     -k|--keep)
-                        shift && _keepInt=$1
+                        shift && KEEP_INT=$1
+                        ;;
+                    -h|--help)
+                        printHelpAndExit 0
                         ;;
                     --)
-                        shift && break
+                        shift; break
                         ;;
                 esac
                 shift
             done
         else
             echo "Incorrect option(s) provided"
-            _printHelpAndExit 1
+            printHelpAndExit 1
         fi
 
-        _filePrefixes=( "$@" )
+        PREFIXES=( "$@" )
     }
 
 
-    _findAndRemove () {
+    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"
+        declare prefix file
+
+        for prefix in "${PREFIXES[@]}"; do
+            for file in $(find . -maxdepth 1 -type f -name "${prefix}*" -printf '%T@ %p\n' | sort -r -z -n | tail -n+$((KEEP_INT + 1)) | awk '{ print $2; }'); do
+                rm "$file"
             done
         done
     }
 
 
-    __main () {
-
-        _parseInput "$@"
-        _findAndRemove
-
+    main () {
+        parseInput "$@"
+        findAndRemove
     }
 
-    __main "$@"
+    main "$@"
 }
 
 # Allow script to be safely sourced

+ 6 - 4
share-link

@@ -1,5 +1,6 @@
 #!/usr/bin/env bash
-# A simple script with nautilus, wl-clipboard, and notify-send support for creating one or more shared links
+# Nautilus script for creating one or more shared links
+# Requires wl-clipboard and notify-send 
 
 ssh_server="bryanroessler.com"
 ssh_files_path="/var/www/repos.bryanroessler.com/files"
@@ -10,7 +11,7 @@ if [[ "$#" -lt 1 ]]; then
 	exit 1
 fi
 
-! hash wl-copy &>/dev/null && sudo dnf install -y wl-clipboard
+hash wl-copy &>/dev/null || echo "Please install wl-copy (usually in the wl-clipboard package)"
 
 if [[ -v NAUTILUS_SCRIPT_SELECTED_URIS ]]; then
 	readarray -t files <<< "$NAUTILUS_SCRIPT_SELECTED_URIS"
@@ -26,7 +27,7 @@ for file in "${files[@]}"; do
     fname="${file##*/}"
     random64=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 64 | head -n 1)
 	echo "rsync -a ${file} ${ssh_server}:${ssh_files_path}/${random64}/"
-	rsync -a "${file}" "${ssh_server}:${ssh_files_path}/${random64}/" &
+	nohup rsync -a "${file}" "${ssh_server}:${ssh_files_path}/${random64}/" &
 	links_array+=("$www_files_path/${random64}/${fname// /%20}")
 done
 
@@ -36,6 +37,7 @@ else
 	printf '%s\n' "${links_array[@]}" | wl-copy
 fi
 
-notify-send -t 3000 -i face-smile "share-link" "File(s) uploaded and link copied to clipboard"
+hash notify-send &>/dev/null &&
+	notify-send -t 3000 -i face-smile "share-link" "File(s) uploaded and link copied to clipboard"
 
 exit 0

+ 0 - 2
speedtest-compare

@@ -1,9 +1,7 @@
 #!/usr/bin/env bash
-#
 # This script will perform speedtests over wireguard and the
 # native connections and print their output
 #
-#
 
 _speedTestData() {
 

+ 1 - 1
strip-exif

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

+ 0 - 8
ulauncher-reload

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

+ 0 - 2
update-git-hooks

@@ -1,8 +1,6 @@
 #!/usr/bin/env bash
-#
 # This script will update the post-receive hooks of multiple bare git repos
 #
-#
 
 for i in /var/lib/git/gogs-repositories/bryan/*/hooks/post-receive; do
 

+ 0 - 3
workstation-shutdown

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

+ 0 - 3
workstation-suspend

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