Initial commit

This commit is contained in:
Randy Jordan 2025-03-22 20:12:33 -05:00
commit 670d0c9cef
Signed by: Randy-Jordan
GPG Key ID: 5CA1B5504D7A21AA
7 changed files with 1836 additions and 0 deletions

1351
CHEATSHEET.md Normal file

File diff suppressed because it is too large Load Diff

49
README.md Normal file
View File

@ -0,0 +1,49 @@
# SH
## Description
Bash scripts and files related to them.
## Table of Contents
- [Description](#description)
- [Features](#features)
- [Usage](#usage)
- [Credits / Resources](#credits--resources)
- [Cheatsheets](#cheatsheets)
- [License](#license)
## Features / TODOS
+ Easy automation
+ Template with flags and params.
+ Bare template with simple logic.
+ Cheat Sheets
[ ] TODO Better Examples.
[ ] TODO Better Usage Guide.
[ ] Reformat lines 812+ of cheatsheet into .MD
## Usage
To make .sh executable just:<br>
`chmod +x filename.sh` and then execute like this `./filename.sh`<br>
For ease of use add the folder (such as /sh) to your $PATH. You can also:<br>
`curl https://myrepos.dev/Randy-Jordan/sh/raw/branch/main/template.sh | /bin/bash` TODO, add gist subpath
## Credits / Resources
[Bash Cheat Sheet](https://devhints.io/bash)<br>
[Minimal Bash Template](https://betterdev.blog/minimal-safe-bash-script-template/)<br>
[Awesome Cheatsheets](https://github.com/LeCoupa/awesome-cheatsheets/)<br>
[Rehan Saeed - Bash Cheat Sheet](https://github.com/RehanSaeed/Bash-Cheat-Sheet/blob/main/README.md)<br>
[Bash-hackers wiki](https://web.archive.org/web/20230406205817/https://wiki.bash-hackers.org/) _(bash-hackers.org)_
[Shell vars](https://web.archive.org/web/20230318164746/https://wiki.bash-hackers.org/syntax/shellvars) _(bash-hackers.org)_
[Learn bash in y minutes](https://learnxinyminutes.com/docs/bash/) _(learnxinyminutes.com)_
[Bash Guide](http://mywiki.wooledge.org/BashGuide) _(mywiki.wooledge.org)_
[ShellCheck](https://www.shellcheck.net/) _(shellcheck.net)_
## Cheatsheets
Sourced from above and compiled in to one cheatsheet.md in the repo.
## License
This project is licensed under MIT - see the [LICENSE](LICENSE) file for details.

83
config.sh Executable file
View File

@ -0,0 +1,83 @@
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat << EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v]
This script clones a bare Git repository for dotfiles, checks out files,
and backs up any pre-existing files that may conflict.
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
# cleanup code here, if needed
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' YELLOW=''
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
parse_params() {
while :; do
case "${1-}" in
-h | --help) usage ;;
-v | --verbose) set -x ;;
--no-color) NO_COLOR=1 ;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
}
parse_params "$@"
setup_colors
# script logic here
msg "${YELLOW}Cloning dotfiles repository...${NOFORMAT}"
git clone --bare https://myrepos.dev/Randy-Jordan/dot.git "$HOME/.dot" || die "Failed to clone repository."
config() {
/usr/bin/git --git-dir="$HOME/.dot/" --work-tree="$HOME" "$@"
}
mkdir -p "$HOME/.config-backup"
if config checkout; then
msg "${GREEN}Checked out config.${NOFORMAT}"
else
msg "${YELLOW}Backing up pre-existing dot files...${NOFORMAT}"
config checkout 2>&1 | grep -E "\s+\." | awk '{print $1}' | xargs -I{} mv {} "$HOME/.config-backup/{}"
config checkout
fi
config config status.showUntrackedFiles no
msg "${GREEN}Dotfiles setup complete.${NOFORMAT}"

132
kernel_dl.sh Executable file
View File

@ -0,0 +1,132 @@
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat << EOF # remove the space between << and EOF, this is due to web plugin issue
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] arg1_kernel_version
Script to download and compile kernel. First argument is the kernel version requested.
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
# script cleanup here
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "\033[0;31m$msg\033[0m"
exit "$code"
}
download_kernel(){
local VERSION=$1
local KERNEL_MAJOR=$(echo $KERNEL_VERSION | sed 's/\([0-9]*\)[^0-9].*/\1/')
local URL="https://www.kernel.org/pub/linux/kernel/v${KERNEL_MAJOR}.x/linux-${KERNEL_VERSION}.tar.xz"
# Download the file using wget
if ! wget -q "${URL}"; then
die "Failed to download file:${URL}\n"
fi
msg "${GREEN} ${KERNEL_VERSION} Download Success.\n${NOFORMAT}"
return 0;
}
untar_kernel(){
local VERSION=$1
msg "${CYAN} Attempting to extract linux-${VERSION}...${NOFORMAT}\n"
# Extract the tar file
if ! tar -xf "linux-${VERSION}.tar.xz"; then
die "Failed to extract file: ${VERSION}"
fi
msg "${GREEN} ${KERNEL_VERSION} Unpack Success.\n${NOFORMAT}"
return 0
}
make_kernel(){
local VERSION=$1
msg "${CYAN} Attempting to build kernel ${VERSION}... ${NOFORMAT}\n"
# Change directory to ./linux
if ! cd "./linux-${VERSION}"; then
die "Failed to change directory to ${VERSION}\n"
fi
# Run make defconfig
if ! make defconfig; then
die "make defconfig failed"
fi
# Build the bzImage
if ! make -j$(nproc) bzImage; then
die "make bzImage failed"
fi
# Change back to the previous directory
if ! cd ..; then
die "Failed to return to the previous directory"
fi
msg "${GREEN} ${KERNEL_VERSION} Build Success.\n${NOFORMAT}"
return 0
}
parse_params() {
# default values of variables set from params
while :; do
case "${1-}" in
-h | --help) usage ;;
-v | --verbose) set -x ;;
--no-color) NO_COLOR=1 ;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
args=("$@")
# Check if there is exactly one argument
if [[ ${#args[@]} -ne 1 ]]; then
usage
return 1
fi
# Params successfully parsed.
return 0
}
parse_params "$@"
setup_colors
# script logic here
KERNEL_VERSION=${args[0]}
msg "${CYAN} Attempting to download ${KERNEL_VERSION}...${NOFORMAT}"
download_kernel ${KERNEL_VERSION}
untar_kernel ${KERNEL_VERSION}
make_kernel ${KERNEL_VERSION}

61
pkg.sh Executable file
View File

@ -0,0 +1,61 @@
#!/usr/bin/env bash
set -Eeuo pipefail # Enable strict error handling (exit on error, unset variable, or command failure)
trap cleanup SIGINT SIGTERM ERR EXIT # Register cleanup function for various signals
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P) # Get the directory of the script
cleanup() {
trap - SIGINT SIGTERM ERR EXIT # Remove signal traps on cleanup
# script cleanup actions here (e.g., removing temporary files)
}
setup_colors() {
# Set color codes if output is to a terminal and no 'NO_COLOR' env var is set
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
# Define color variables for output
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
# Set empty color codes if no terminal or NO_COLOR is set
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
echo >&2 -e "${1-}" # Print the message to stderr, with color formatting if set
}
die() {
local msg=$1 # The error message to display
local code=${2-1} # The exit code (default 1 if not provided)
echo >&2 -e "${RED}$msg${NOFORMAT}" # Print the error message
exit "$code" # Exit with the specified code
}
setup_colors # Initialize color settings
# Execute your logic here (place the main script actions)
msg "${CYAN}Attempting installation of all packages... ${NOFORMAT}"
# Check if ran as root.
if [ "$EUID" -ne 0 ]; then
die "This script must be run as root. Please use sudo." 1
fi
# Install packages from the list
while read -r p; do
msg "\n${CYAN}Installing package: $p ${NOFORMAT}" # Print which package is being installed
sudo apt-get install -y "$p" # Install the package
done << "EOF"
gpg
build-essential
coreutils
tmux
zsh
vim-gtk3
universal-ctags
fzf
git
wget
curl
EOF

74
repoc.sh Executable file
View File

@ -0,0 +1,74 @@
#!/bin/bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat << EOF
Usage: $(basename "${BASH_SOURCE[0]}") <foldername>
Script to initialize a project folder with specific files and directories.
Available options:
<foldername> The name of the folder to create and initialize
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
# script cleanup here
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
parse_params() {
# Ensure exactly one argument is provided
if [ $# -ne 1 ]; then
usage
fi
foldername="$1"
# Ensure the foldername is not empty
if [ -z "${foldername-}" ]; then
die "Missing required argument: foldername"
fi
}
parse_params "$@"
# Create the specified folder
mkdir -p "$foldername"
# Copy the required files from Athena/code
cp ~/eg/c_dev/gitignore_c "$foldername/.gitignore"
cp ~/eg/README.md "$foldername/README.md"
cp ~/eg/c_dev/Makefile "$foldername/Makefile"
cp ~/eg/licenses/LICENSE_MIT.md "$foldername/LICENSE"
# Create src and tests subfolders
mkdir -p "$foldername/src"
mkdir -p "$foldername/tests"
mkdir -p "$foldername/include"
# Change directory to the created folder
cd "$foldername"
# Run ctags for C (not C++)
ctags -R --c-kinds=+p --fields=+iaS --extras=+q /usr/include
msg "Initialization and ctags generation complete for folder: $foldername"

86
template.sh Normal file
View File

@ -0,0 +1,86 @@
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat << EOF # remove the space between << and EOF, this is due to web plugin issue
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-f] -p param_value arg1 [arg2...]
Script description here.
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-f, --flag Some flag description
-p, --param Some param description
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
# script cleanup here
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "\033[0;31m$msg\033[0m"
exit "$code"
}
parse_params() {
# default values of variables set from params
flag=0
param=''
while :; do
case "${1-}" in
-h | --help) usage ;;
-v | --verbose) set -x ;;
--no-color) NO_COLOR=1 ;;
-f | --flag) flag=1 ;; # example flag
-p | --param) # example named parameter
param="${2-}"
shift
;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
args=("$@")
# check required params and arguments
[[ -z "${param-}" ]] && die "Missing required parameter: param"
[[ ${#args[@]} -eq 0 ]] && die "Missing script arguments"
return 0
}
parse_params "$@"
setup_colors
# script logic here
msg "${RED}Read parameters:${NOFORMAT}"
msg "- flag: ${flag}"
msg "- param: ${param}"
msg "- arguments: ${args[*]-}"