diff --git a/hosts b/hosts index 5f626bc..c32a5ef 100755 --- a/hosts +++ b/hosts @@ -62,7 +62,8 @@ HOSTS_PATH="${HOSTS_PATH:-/etc/hosts}" # variable has been set. The command is expected to print a message and # should typically be either `echo`, `printf`, or `cat`. _debug() { - if [[ "${_use_debug:-"0"}" -eq 1 ]]; then + if [[ "${_use_debug:-"0"}" -eq 1 ]] + then # Prefix debug message with "bug (U+1F41B)" printf "🐛 " "$@" @@ -154,12 +155,14 @@ optstring=h # argument to the option, such as wget -qO-) unset options # while the number of arguments is greater than 0 -while (($#)); do +while (($#)) +do case $1 in # if option is of type -ab -[!-]?*) # loop over each character starting with the second - for ((i=1; i<${#1}; i++)); do + for ((i=1; i<${#1}; i++)) + do # extract 1 character from position 'i' c=${1:i:1} # add current char to options @@ -167,14 +170,17 @@ while (($#)); do # if option takes a required argument, and it's not the last char # make the rest of the string its argument - if [[ $optstring = *"$c:"* && ${1:i+1} ]]; then + if [[ $optstring = *"$c:"* && ${1:i+1} ]] + then options+=("${1:i+1}") break fi done ;; # if option is of type --foo=bar, split on first '=' - --?*=*) options+=("${1%%=*}" "${1#*=}");; + --?*=*) + options+=("${1%%=*}" "${1#*=}") + ;; # end of options, stop breaking them up --) options+=(--endopts) @@ -183,7 +189,9 @@ while (($#)); do break ;; # otherwise, nothing special - *) options+=("$1");; + *) + options+=("$1") + ;; esac shift @@ -206,7 +214,8 @@ command_argv=("$0") cmd="" _use_debug=0 -while [ $# -gt 0 ]; do +while [ $# -gt 0 ] +do opt="$1" shift case "$opt" in @@ -222,7 +231,8 @@ while [ $# -gt 0 ]; do *) # The first non-option argument is assumed to be the command name. # All subsequent arguments are added to $command_arguments. - if [[ -n $cmd ]]; then + if [[ -n $cmd ]] + then command_argv+=("$opt") else cmd="$opt" @@ -292,7 +302,8 @@ _load_commands() { [[ "$function_name" == "desc" ]] || \ [[ "$function_name" == "debug" ]] || \ [[ "$function_name" == "die" ]] - ); then + ) + then defined_commands+=("$function_name") fi done @@ -319,7 +330,8 @@ _main() { _debug printf "main() \$cmd (upon entering): %s\n" "$cmd" # If $cmd is blank, then set to help - if [[ -z $cmd ]]; then + if [[ -z $cmd ]] + then cmd="$DEFAULT_COMMAND" fi @@ -327,7 +339,8 @@ _main() { _load_commands # If the command is defined, run it, otherwise return an error. - if ( _contains "$cmd" "${defined_commands[*]:-}" ); then + if ( _contains "$cmd" "${defined_commands[*]:-}" ) + then # Pass all comment arguments to the program except for the first ($0). $cmd "${command_parameters[@]:-}" else @@ -358,7 +371,8 @@ _contains() { for _test_element in "${test_list[@]:-}" do _debug printf "_contains() \$_test_element: %s\n" "$_test_element" - if [[ "$_test_element" == "$1" ]]; then + if [[ "$_test_element" == "$1" ]] + then _debug printf "_contains() match: %s\n" "$1" return 0 fi @@ -402,7 +416,8 @@ _command_argv_includes() { # Print a helpful error message when the specified operation can't be # performed due to the lack of write permissions. _verify_write_permissions() { - if ! test -w "${HOSTS_PATH}"; then + if ! test -w "${HOSTS_PATH}" + then _die printf \ "You don't have permission to perform this operation. Try again with: sudo !!\n" @@ -441,7 +456,8 @@ sudo !!\n" desc() { set +e [[ -z $1 ]] && _die printf "desc: No command name specified.\n" - if [[ -n ${2:-} ]]; then + if [[ -n ${2:-} ]] + then read -d '' "_desc_$1" <