From d0aead5a9c979a5c6d738f3ca742b44705e9e686 Mon Sep 17 00:00:00 2001 From: z3rOR0ne Date: Thu, 7 Jul 2022 13:05:06 -0700 Subject: [PATCH] :sparkles: Updated scripts --- scripts/alarm | 3 + scripts/batnotify | 12 +- scripts/bgit | 323 ++++++++++++++++++++++++++++----------- scripts/changebrightness | 17 +++ scripts/dmconf | 1 + scripts/jscurl | 5 +- scripts/permissions.sh | 6 +- scripts/rdir | 8 + scripts/reboot.sh | 6 + scripts/rpipe.sh | 34 +++++ scripts/sdir | 20 +++ scripts/shutdown.sh | 6 + scripts/wpipe.sh | 30 ++++ scripts/zhome.sh | 14 ++ 14 files changed, 386 insertions(+), 99 deletions(-) create mode 100755 scripts/alarm create mode 100644 scripts/changebrightness create mode 100755 scripts/rdir create mode 100755 scripts/rpipe.sh create mode 100755 scripts/sdir create mode 100755 scripts/wpipe.sh create mode 100755 scripts/zhome.sh diff --git a/scripts/alarm b/scripts/alarm new file mode 100755 index 00000000..c789ac95 --- /dev/null +++ b/scripts/alarm @@ -0,0 +1,3 @@ +#!/bin/bash + +sleep $1 && notify-send "Times Up!!" & diff --git a/scripts/batnotify b/scripts/batnotify index caf21100..ab19e9b7 100755 --- a/scripts/batnotify +++ b/scripts/batnotify @@ -1,11 +1,11 @@ -#!/bin/bash +#!/bin/sh -while [ true ] ; do +while true ; do PWRLVL=$(cat /sys/class/power_supply/BAT1/capacity) - if [ $PWRLVL -le 15 ] ; then - exec notify-send "Battery is at 15%!!"; - break; + if [ "$PWRLVL" -le 15 ] ; then + notify-send "Battery is at 15%!!"; + sleep 300s; else - sleep 15s; + sleep 60s; fi done diff --git a/scripts/bgit b/scripts/bgit index 8fbfe882..3506a481 100755 --- a/scripts/bgit +++ b/scripts/bgit @@ -8,8 +8,12 @@ txtred=${txtbld}$(tput setaf 1) txtyellow=${txtbld}$(tput setaf 3) txtwhite=${txtbld}$(tput setaf 7) +# Notes on features to add: Have an option for saving password, +# rather than automatically just saving it... +# Possibly add feature for gitmoji?? + # Intro Prompt -echo "${txtblue}bgit: a handy shell script for automating the standard git process" +echo "${txtblue}bgit: a handy shell script for automating simple git inits/commits" # Reset output style tput bold & tput setaf 7 @@ -19,107 +23,246 @@ numrepos=$(git remote | wc -l) # If there are no git repositories in this directory... if [ $numrepos -eq 0 ] ; then - echo "${txtred}no git repositories in this directory...exiting bgit${txtwhite}" - exit 1 -fi + echo "${txtgreen}no git repository found, executing git init...${txtwhite}" + while [ true ] + do + #Prompts the user if they'd like to initialize repository + read -e -r -p "${txtwhite}initialize repository?(y/n): ${txtyellow}" init + if [[ $init == "n" || $init = "no" ]]; then + echo "${txtred}no git repository initialized...exiting${txtwhite}" + break + elif [[ $init == "y" || $init == "yes" ]] ; then -# creates a list of repos and puts them in an array -repoarray=($(git remote)) + # Creates a default README.md + echo -e "## A Simple README\n\nSome Text" > README.md + # Creates a default .gitignore + echo -e "node_modules\n*.env\n*.out\n.gitignore" > .gitignore + # Initializes git repository cwd... + git init + # and adds newly created README.md + git add README.md -# Grabs all new files -newfiles=$(git status --short | grep '??' | awk '{print $2}' | tr '\n' ' ') + while [ true ]; + do + read -e -r -p "${txtwhite}would you like to change username/password from global defaults?(y/n): ${txtyellow}" changedefaults + if [[ $changedefaults == "y" || $changedefaults == "yes" ]] ; then + read -e -r -p "${txtwhite}enter your username: ${txtyellow}" uname + if [[ -n $uname ]] ; then + git config user.name $uname + read -e -r -p "${txtwhite}enter your email: ${txtyellow}" uemail + if [[ -n $uemail ]] ; then + git config user.email $uemail + break + else + echo "${txtblue}no email entered, defaulting to global value${txtwhite}" + break + fi + else + echo "${txtblue}no username entered, defaulting to global value${txtwhite}" + break + fi + else + echo "${txtblue}defaulting to global username/password${txtwhite}" + break + fi + done -# Grabs all modified files -modified=$(git status --short | grep M | awk '{print $2}' | tr '\n' ' ') + git config commit.gpgsign false -# Grabs all deleted files -deleted=$(git status --short | grep D | awk '{print $2}' | tr '\n' ' ') + read -e -r -p "${txtwhite}include gitmoji?(y/n): ${txtyellow}" gittymoji + if [[ $gittymoji == "y" || $gittymoji == "yes" ]] ; then + gmoji=":tada:" + else + gmoji="" + fi -# Reports what is staged for commit -# If there is anything to commit... -if [[ ! -z $newfiles || ! -z $modified || ! -z $deleted ]] ; then - # Let the user know what is staged for commit - echo "${txtwhite}The following files are staged for commit:" -# Otherwise, if there isn't anything to commit... -else - # Let the user know that everything is up to date - echo "${txtred}everything up-to-date...exiting bgit${txtwhite}" - # and exit the program - exit 0 -fi - -# Let the user know what is staged for commit -if [[ $newfiles ]]; then - echo "${txtgreen}ADDED: ${txtgreen}$newfiles" -fi -if [[ $modified ]]; then - echo "${txtblue}MODIFIED: ${txtblue}$modified" -fi -if [[ $deleted ]]; then - echo "${txtred}DELETED: ${txtred}$deleted" -fi - - -while [ true ] -do - # Prompts user if they'd like to commit the changes - read -e -r -p "${txtwhite}commit changes?(y/n): ${txtyellow}" change - # If the user chooses to commit the changes... - if [[ $change == "y" || $change == "yes" ]] ; then - - # Grabs commit message - while [ true ] - do - # Prompts the user to input their commit message - read -e -r -p "${txtwhite}commit message: ${txtyellow}" message - cmessage=("$message") - # if the user inputted a commit message... - if [ -n "$message" ] ; then + while [ true ] + do + read -e -r -p "${txtwhite}commit message: ${txtyellow}" message + cmessage=("$message") + # capitalizes first letter of commit message + cmessage="$(tr '[:lower:]' '[:upper:]' <<< ${cmessage:0:1})${cmessage:1}" + # if the user entered a commit message over 50 characters... + if [ "${#message}" -ge 51 ] ; then + echo "${txtred}commit message is too large!" + echo "${txtred}please limit to 50 or less characters${txtwhite}" + # if the user inputted a commit message.. + elif [ -n "$message" ] ; then + git commit -m "$gmoji $cmessage" # then break out of the while loop and continue with the commit... break - # if the user did NOT input a commit message... + # if the user did NOT input a commit message... + else + # tell the user to do so or to quit + echo "${txtred}commit message is empty, please write a commit message" + echo "${txtred}or type 'CTRL+C' to quit${txtwhite}" + fi + done + + + while [ true ] + do + read -e -r -p "${txtwhite}change branch name?(y/n): ${txtyellow}" chbranch + if [[ $chbranch == "y" || $chbranch == "yes" ]] ; then + read -e -r -p "${txtwhite}branch name?(i.e. 'main'): ${txtyellow}" branch + if [ -n "$branch" ] ; then + git branch -M $branch + break + else + echo "${txtred}branch name is empty, please enter branch name!${txtwhite}" + fi + else + echo "${txtblue}branch name will be left as master${txtwhite}" + break + fi + done + while [ true ] + do + read -e -r -p "${txtwhite}Please enter remote to add: ${txtyellow}" remote + if [[ -n $remote ]] ; then + read -e -r -p "${txtwhite}Please enter name of repository: ${txtyellow}" repo + tput bold & tput setaf 7 + if [[ -n $repo ]] ; then + git remote add $remote "https://codeberg.org/z3rOR0ne/$repo" + git push -u $remote $branch + echo "${txtgreen}Git repository created!!${txtwhite}" + break + else + echo "${txtred}Please enter a name for repository!${txtwhite}" + fi else - # tell the user to do so or to quit - echo "${txtred}commit message is empty, please write a commit message" - echo "${txtred}or type 'CTRL+C' to quit${txtwhite}" + echo "${txtred}Please enter a name for remote!${txtwhite}" fi - done - - # Reset output style - tput bold & tput setaf 7 - - # adds the modified and new files (suppresses message anyways if no files are to be committed) - if [[ $newfiles || $modified ]] ; then - git config advice.addEmptyPathspec false - git add $modified $newfiles ; + done + break + else + break fi - # commits the user's message (multi-word supported) - git commit -m "$cmessage" ; + done - # pushes the commit to each repository - for ((i = 0; i < $numrepos; i++)) ; do - git push ${repoarray[i]} ; - done +# Otherwise, if there already is a .git directory... +else - exit 0 - # If the user chooses to not the commit the changes ... - elif [[ $change == "n" || $change == "no" ]] ; then + # creates a list of repos and puts them in an array + repoarray=($(git remote)) + # Grabs all new files + newfiles=$(git status --short | grep '??' | awk '{print $2}' | tr '\n' ' ') + # Grabs all modified files + modified=$(git status --short | grep M | awk '{print $2}' | tr '\n' ' ') + # Grabs all deleted files + deleted=$(git status --short | grep D | awk '{print $2}' | tr '\n' ' ') - # Reset output style - tput bold & tput setaf 7 - - # Report the git status - git status --short - - # And let the user know no changes were committed - echo "${txtred}no changes committed" - - exit 0 - # If the user chooses to quit... - elif [[ $change == "q" || $change == "quit" ]] ; then - exit 0 - # If the user continues to just press enter... + # Reports what is staged for commit + # If there is anything to commit... + if [[ ! -z $newfiles || ! -z $modified || ! -z $deleted ]] ; then + # Let the user know what is staged for commit + echo "${txtwhite}The following files are staged for commit:" + # Otherwise, if there isn't anything to commit... else - echo "${txtred}please enter y or n or type 'q' or 'quit' or 'CTRL+C' to quit${txtwhite}" + # Let the user know that everything is up to date + echo "${txtred}everything up-to-date...exiting bgit${txtwhite}" + # and exit the program + exit 0 fi -done + + # Let the user know what is staged for commit + if [[ $newfiles ]]; then + echo "${txtgreen}ADDED: ${txtgreen}$newfiles" + fi + if [[ $modified ]]; then + echo "${txtblue}MODIFIED: ${txtblue}$modified" + fi + if [[ $deleted ]]; then + echo "${txtred}DELETED: ${txtred}$deleted" + fi + + while [ true ] + do + # Prompts user if they'd like to commit the changes + read -e -r -p "${txtwhite}commit changes?(y/n): ${txtyellow}" change + # If the user chooses to commit the changes... + if [[ $change == "y" || $change == "yes" ]] ; then + # prompt if user wants to specify gitmoji: + while [ true ] + do + read -e -r -p "${txtwhite}add a gitmoji?(y/n): ${txtyellow}" gmojiprompt + if [[ $gmojiprompt == "y" || $gmojiprompt == "yes" ]] ; then + read -e -r -p "${txtwhite}enter gitmoji: ${txtyellow}" gitmoji + gitmoji=":$gitmoji:" + break + else + echo "${txtblue}no gitmoji specified...${txtwhite}" + gitmoji="" + break + fi + done + # Grabs commit message + while [ true ] + do + # Prompts the user to input their commit message + # (does not allow for multiple message commits) + read -e -r -p "${txtwhite}commit message: ${txtyellow}" message + cmessage=("$message") + # capitalizes first character of commit message + cmessage="$(tr '[:lower:]' '[:upper:]' <<< ${cmessage:0:1})${cmessage:1}" + # if the user entered a commit message over 50 characters... + if [ "${#message}" -ge 51 ] ; then + echo "${txtred}commit message is too large!" + echo "${txtred}please limit to 50 or less characters${txtwhite}" + # if the user inputted a commit message.. + elif [ -n "$message" ] ; then + # then break out of the while loop and continue with the commit... + break + # if the user did NOT input a commit message... + else + # tell the user to do so or to quit + echo "${txtred}commit message is empty, please write a commit message" + echo "${txtred}or type 'CTRL+C' to quit${txtwhite}" + fi + done + + # Reset output style + tput bold & tput setaf 7 + + # adds the modified and new files + # (suppresses message anyways if only deleted files are to be committed) + if [[ $newfiles || $modified ]] ; then + # also remembers password for 1 hour after last use + # (comment out if more security needed, or just use ssh...) + echo "${txtgreen}git will remember your password for 1 hour!${txtwhite}" + git config credential.helper 'cache --timeout=3600'; + git config advice.addEmptyPathspec false; + git add $modified $newfiles ; + fi + # commits the user's message (multi-word supported) + git commit -m "$gitmoji $cmessage" ; + + # pushes the commit to each repository + for ((i = 0; i < $numrepos; i++)) ; do + git push ${repoarray[i]} ; + done + + echo "${txtblue}bgit script has completed! goodbye!${txtwhite}" + exit 0 + # If the user chooses to not the commit the changes ... + elif [[ $change == "n" || $change == "no" ]] ; then + + # Reset output style + tput bold & tput setaf 7 + + # Report the git status + git status --short + + # And let the user know no changes were committed + echo "${txtred}no changes committed" + + exit 0 + # If the user chooses to quit... + elif [[ $change == "q" || $change == "quit" ]] ; then + exit 0 + # If the user continues to just press enter... + else + echo "${txtred}please enter y or n or type 'q' or 'quit' or 'CTRL+C' to quit${txtwhite}" + fi + done +fi diff --git a/scripts/changebrightness b/scripts/changebrightness new file mode 100644 index 00000000..00b4e526 --- /dev/null +++ b/scripts/changebrightness @@ -0,0 +1,17 @@ +#!/bin/bash + +function send_notification() { + brightness=$(printf "%.0f\n" $(brillo -G)) + dunstify -a "changebrightness" -u low -r 9991 -h int:value:"$brightness" -i "brightness-$1" "Brightness: $brightness%" -t 2000 +} + +case $1 in +up) + brillo -A 5 -q + send_notification $1 + ;; +down) + brillo -U 5 -q + send_notification $1 + ;; +esac diff --git a/scripts/dmconf b/scripts/dmconf index b1044b44..c5d17b36 100755 --- a/scripts/dmconf +++ b/scripts/dmconf @@ -11,6 +11,7 @@ declare -a confedit_list=( "bspwm - $HOME/.config/bspwm/bspwmrc" "sxhkd - $HOME/.config/sxhkd/sxhkdrc" "picom - $HOME/.config/picom/picom.conf" +"dunst - $HOME/.config/dunst/dunstrc" "i3 - $HOME/.config/i3/config" "kitty - $HOME/.config/kitty/kitty.conf" "nvim - $HOME/.config/nvim/lua/config.lua" diff --git a/scripts/jscurl b/scripts/jscurl index 69431d4b..de09c47e 100755 --- a/scripts/jscurl +++ b/scripts/jscurl @@ -16,7 +16,7 @@ helptag="-help" archivedir="$HOME/.recycle-bin" # Show help page -function helpmessage() +function helpmessage() { echo -e "\e[1mUsage: jscurl (no 'https://' necessary)\e[0m" @@ -35,7 +35,8 @@ function helpmessage() echo "" echo -e "\e[1mjson/html:\e[0m" echo -e "\e[1mjscurl (no 'https://' necessary)\t\t\t\t\t\t show json page\e[0m" - echo -e "\e[1mjscurl -m (no 'https://' necessary)\t\t\t\t\t show html page\e[0m" echo -e "\e[1mjscurl -w (no 'https://' or '.html' necessary)\t write html file to ~/.recycle-bin\e[0m" + echo -e "\e[1mjscurl -m (no 'https://' necessary)\t\t\t\t\t show html page\e[0m" + echo -e "\e[1mjscurl -w (no 'https://' or '.html' necessary)\t write html file to ~/.recycle-bin\e[0m" echo -e "\e[1mjscurl -j (no 'https://' or '.json' necessary)\t write json file to ~/.recycle-bin\e[0m" echo -e "\e[1mjscurl -h/--help\t\t\t\t\t\t\t\t show help page\e[0m" } diff --git a/scripts/permissions.sh b/scripts/permissions.sh index 31edad7e..0d1f986b 100755 --- a/scripts/permissions.sh +++ b/scripts/permissions.sh @@ -1,8 +1,12 @@ #!/bin/bash -if [[ "$(id -u)" -ne 0 ]]; then +myid=$(id -u) +echo "your userid is ${myid}" +if [[ $myid -ne 0 && $myid -ne 1000 ]]; then echo "[ERROR] You must run this script as root!" exit 0 +elif [ $myid -eq 1000 ]; then + echo "You are ${USER}!" else echo "You are root!" fi diff --git a/scripts/rdir b/scripts/rdir new file mode 100755 index 00000000..c4275198 --- /dev/null +++ b/scripts/rdir @@ -0,0 +1,8 @@ +#!/bin/bash + +# A simple bookmarking alias used in conjunction with sdir and our $HOME/.aliases file to bookmark our current directory to be returned to later using rdir. + +# grabs the current $sdir environment variable +source $HOME/.zshrc + +cd $sdir diff --git a/scripts/reboot.sh b/scripts/reboot.sh index 60020ce8..93c224f7 100755 --- a/scripts/reboot.sh +++ b/scripts/reboot.sh @@ -1,5 +1,11 @@ #!/bin/bash +last_line_first_word=$(awk '{w=$1} END{print w}' $HOME/.zshrc) + +if [[ $last_line_first_word == "export" ]] ; then + sed -i '$ d' $HOME/.zshrc +fi + if [ -d "$HOME/.recycle-bin" ] ; then doas rm -r "$HOME/.recycle-bin/" && doas reboot else diff --git a/scripts/rpipe.sh b/scripts/rpipe.sh new file mode 100755 index 00000000..f03b02d8 --- /dev/null +++ b/scripts/rpipe.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +# https://www.linuxjournal.com/content/using-named-pipes-fifos-bash + +# To be used in conjunction with wpipe.sh (demonstrating mkfifo, or named pipes) + +pipe=/tmp/testpipe + +if [[ ! -p $pipe ]]; then + mkfifo $pipe +fi + +while true; do + if read line < $pipe; then + if [[ "$line" == 'quit' ]]; then + break + fi + echo $line + fi +done + +echo "Exiting" + +# Example Output: +# $ sh rpipe.sh & +# [3] 23842 +# $ sh wpipe.sh +# Hello from 23846 +# $ sh wpipe.sh +# Hello from 23847 +# $ sh wpipe.sh +# Hello from 23848 +# $ sh wpipe.sh quit +# Reader exiting diff --git a/scripts/sdir b/scripts/sdir new file mode 100755 index 00000000..7e43b353 --- /dev/null +++ b/scripts/sdir @@ -0,0 +1,20 @@ +#/bin/bash + +# A simple bookmarking alias used in conjunction with rdir and our $HOME/.aliases file to bookmark our current directory to be returned to later using rdir. + +# grabs the last line of our .zshrc, and grabs the first word +last_line_first_word=$(awk '{w=$1} END{print w}' $HOME/.zshrc) + +# if the last line of our rc file begins with "export"... +# remove the last line of our rc file +if [[ $last_line_first_word == "export" ]] ; then + sed -i '$ d' $HOME/.zshrc +fi + +# save our current working directory +sdir=$(pwd) + +# and write that directory temporarily into our .zshrc +echo 'export sdir='$sdir >> $HOME/.zshrc +# immediately source it to use it in this shell session +source $HOME/.zshrc diff --git a/scripts/shutdown.sh b/scripts/shutdown.sh index 338d3dc8..26a9308b 100755 --- a/scripts/shutdown.sh +++ b/scripts/shutdown.sh @@ -1,5 +1,11 @@ #!/bin/bash +last_line_first_word=$(awk '{w=$1} END{print w}' $HOME/.zshrc) + +if [[ $last_line_first_word == "export" ]] ; then + sed -i '$ d' $HOME/.zshrc +fi + if [ -d "$HOME/.recycle-bin" ] ; then doas rm -r "$HOME/.recycle-bin/" && doas shutdown -h now else diff --git a/scripts/wpipe.sh b/scripts/wpipe.sh new file mode 100755 index 00000000..5f18ba9d --- /dev/null +++ b/scripts/wpipe.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# https://www.linuxjournal.com/content/using-named-pipes-fifos-bash + +# To be used in conjunction with rpipe.sh (demonstrating mkfifo, or named pipes) + +pipe=/tmp/testpipe + +if [[ ! -p $pipe ]]; then + echo "Reader not running" + exit 1 +fi + +if [[ "$1" ]]; then + echo "$1" > $pipe +else + echo "Hello from $$" > $pipe +fi + +# Example Output: +# $ sh rpipe.sh & +# [3] 23842 +# $ sh wpipe.sh +# Hello from 23846 +# $ sh wpipe.sh +# Hello from 23847 +# $ sh wpipe.sh +# Hello from 23848 +# $ sh wpipe.sh quit +# Reader exiting diff --git a/scripts/zhome.sh b/scripts/zhome.sh new file mode 100755 index 00000000..4a076e4c --- /dev/null +++ b/scripts/zhome.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +echo '' | xclip && xclip -selection clipboard /dev/null && clear && history -p && hash -r && /usr/bin/rm $HOME/.zsh_history && touch $HOME/.zsh_history + +# grabs the last line of our .zshrc, and grabs the first word +last_line_first_word=$(awk '{w=$1} END{print w}' $HOME/.zshrc) + +# if the last line of our rc file begins with "export"... +# remove the last line of our rc file +if [[ $last_line_first_word == "export" ]] ; then + sed -i '$ d' $HOME/.zshrc +fi + +# unset sdir