Skip to content

Instantly share code, notes, and snippets.

@Stewie410
Stewie410 / generic_bash_script_options.sh
Created July 22, 2023 19:12
Rewrite of 'Generic Bash Script Options' posted to reddit
#!/usr/bin/env bash
#
# Generic Bash Script Args
show_help() {
cat << EOF
Generic Bash Script Args
USAGE: ${0##*/} [OPTIONS] -c PATH -r TIME -n NUM -s NUM
${0##*/} [OPTIONS] --copy-from PATH --creation TIME --var-num NUM --step NUM
@Stewie410
Stewie410 / bash_aliases.sh
Last active June 18, 2024 09:15
TheOuterLinux 'bashrc - basic.txt' Overhaul
#!/usr/bin/env bash
# bashtask
alias bashtask='bashtask.sh'
alias taskadd='bashtask --add'
alias taskin='bashtask --insert'
alias taskrm='bashtask --remove'
alias taskcl='bashtask --clear'
alias tasklist='bashtask --print'
@Stewie410
Stewie410 / ajw_stewie410.sh
Last active September 30, 2022 15:18
A rewrite of u/dewdude's AJW script
#!/usr/bin/env bash
#
# Generate the dialplan that interfaces with the player logic
cleanup() {
if [[ -s "${stage}" ]]; then
if [[ "${outfile}" == "-" ]]; then
cat "${stage}"
else
mv --force "${stage}" "${outfile}"
@Stewie410
Stewie410 / reponse.md
Created July 25, 2022 05:06
Libby Response

Argument Parsing

I know you're trying to make things functional for both Linux & MacOS, so some care would be required there; but I'd recommend taking a look at GNU getopt or getopts for argument processing & parsing.

Additionally, I'd personally set default values to variables set in the argument parse loop before the loop occurrs -- that way you don't need to check if a variable is undefined before setting a value.

Here's how I'd rewrite the argparse loop & variable definitions. This would have the added benefit of allowing the user to condense multiple short options into on string (eg -ku)...though, YMMV with MacOS support, as I'm wholly unfamiliar with that platform):

mode="fzf"
#!/usr/bin/env bash
# ---------------------------
# -- Traps --
# ---------------------------
# Always execute "cleanup" function on exit
trap cleanup EXIT
# -------------------------------
# -- Functions --
@Stewie410
Stewie410 / bootstrap.sh
Created October 19, 2019 01:45
My "rewrite" of u/brunomiguel's bootstrap script
#!/bin/env bash
#
# bootstrap.sh
# Author: u/stewie410
# Date: 2019-10-18
#
# Rewrite for u/brunomiguel
# https://www.reddit.com/r/bash/comments/djcop0/asking_feedback_on_a_bootstrap_script
# ##----------------------------##
@Stewie410
Stewie410 / mkCert.sh
Last active September 12, 2019 03:19
An overly complicated response to u/RANDY_MAYONNSAISE
#!/bin/env bash
#
# mkCert.sh
# Author: u/stewie410
# Date: 2019-09-11
#
# Generates SSL Certificates auto-magically
# Based on u/RANDY_MAYONNAISE's post:
# https://www.reddit.com/r/bash/comments/d2y10e/working_with_arguments_in_bash/