Created
March 10, 2024 17:25
-
-
Save DimitriGilbert/7a6e1778b759364369d24ec480d5df71 to your computer and use it in GitHub Desktop.
my fedora daily update script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # @parseArger-begin | |
| # @parseArger-help "I send an SOS to the world" --option "help" --short-option "h" | |
| # @parseArger-declarations | |
| # @parseArger opt shutdown "shutdown after the value" | |
| # @parseArger flag dnf "update dnf package" --on | |
| # @parseArger flag flathub "update flatpak" --on | |
| # @parseArger flag yes "auto yes" --short y | |
| # @parseArger-declarations-end | |
| # @parseArger-parsing | |
| die() | |
| { | |
| local _ret="${2:-1}" | |
| test "${_PRINT_HELP:-no}" = yes && print_help >&2 | |
| echo "$1" >&2 | |
| exit "${_ret}" | |
| } | |
| begins_with_short_option() | |
| { | |
| local first_option all_short_options='' | |
| first_option="${1:0:1}" | |
| test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0 | |
| } | |
| # POSITIONALS ARGUMENTS | |
| _positionals=(); | |
| # OPTIONALS ARGUMENTS | |
| _arg_shutdown= | |
| # FLAGS | |
| _arg_dnf="on" | |
| _arg_flathub="on" | |
| _arg_yes="off" | |
| print_help() | |
| { | |
| echo -e "I send an SOS to the world:" | |
| echo -e " --shutdown <shutdown>: shutdown after the value" | |
| echo -e " --dnf|--no-dnf: update dnf package, on by default (use --no-dnf to turn it off)" | |
| echo -e " --flathub|--no-flathub: update flatpak, on by default (use --no-flathub to turn it off)" | |
| echo -e " -y|--yes|--no-yes: auto yes" | |
| echo -e "Usage : | |
| $0 [--shutdown <value>] [--[no-]dnf] [--[no-]flathub] [--[no-]yes]" | |
| } | |
| parse_commandline() | |
| { | |
| _positionals_count=0 | |
| while test $# -gt 0 | |
| do | |
| _key="$1" | |
| case "$_key" in | |
| --shutdown) | |
| test $# -lt 2 && die "Missing value for the option: '$_key'" 1 | |
| _arg_shutdown="$2" | |
| shift | |
| ;; | |
| --shutdown=*) | |
| _arg_shutdown="${_key##--shutdown=}" | |
| ;; | |
| --no-dnf|--dnf) | |
| _arg_dnf="on" | |
| test "${1:0:5}" = "--no-" && _arg_dnf="off" | |
| ;; | |
| --no-flathub|--flathub) | |
| _arg_flathub="on" | |
| test "${1:0:5}" = "--no-" && _arg_flathub="off" | |
| ;; | |
| -y|--no-yes|--yes) | |
| _arg_yes="on" | |
| test "${1:0:5}" = "--no-" && _arg_yes="off" | |
| ;; | |
| -h|--help) | |
| print_help; | |
| exit 0; | |
| ;; | |
| -h*) | |
| print_help; | |
| exit 0; | |
| ;; | |
| *) | |
| _last_positional="$1" | |
| _positionals+=("$_last_positional") | |
| _positionals_count=$((_positionals_count + 1)) | |
| ;; | |
| esac | |
| shift | |
| done | |
| } | |
| handle_passed_args_count() | |
| { | |
| local _required_args_string="" | |
| test "${_positionals_count}" -le 0 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect at most 0 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}')." 1 | |
| test "${_positionals_count}" -ge 0 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require at least 0 (namely: $_required_args_string), but got only ${_positionals_count}." 1 | |
| } | |
| assign_positional_args() | |
| { | |
| local _positional_name _shift_for=$1; | |
| _positional_names=""; | |
| shift "$_shift_for" | |
| for _positional_name in ${_positional_names};do | |
| test $# -gt 0 || break; | |
| eval "if [ \"\$_one_of${_positional_name}\" != \"\" ];then [[ \"\${_one_of${_positional_name}[*]}\" =~ \"\${1}\" ]];fi" || die "${_positional_name} must be one of: $(eval "echo \"\${_one_of${_positional_name}[*]}\"")" 1 | |
| eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1; | |
| shift; | |
| done | |
| } | |
| print_debug() | |
| { | |
| print_help | |
| # shellcheck disable=SC2145 | |
| echo "DEBUG: $0 $@"; | |
| echo -e "\tshutdown: ${_arg_shutdown | sed 's/-/_/g'}"; | |
| echo -e "\tdnf: ${_arg_dnf | sed 's/-/_/g'}"; | |
| echo -e "\tflathub: ${_arg_flathub | sed 's/-/_/g'}"; | |
| echo -e "\tyes: ${_arg_yes | sed 's/-/_/g'}"; | |
| } | |
| parse_commandline "$@" | |
| handle_passed_args_count | |
| assign_positional_args 1 "${_positionals[@]}" | |
| # @parseArger-parsing-end | |
| # print_debug "$@" | |
| # @parseArger-end | |
| if [[ "$_arg_dnf" = "on" ]]; then | |
| dnf_cmd="sudo dnf update --refresh"; | |
| if [[ "$_arg_yes" = "on" ]]; then | |
| dnf_cmd+=" -y"; | |
| fi | |
| $dnf_cmd; | |
| fi | |
| if [[ "$_arg_flathub" = "on" ]]; then | |
| fp_cmd="flatpak update"; | |
| if [[ "$_arg_yes" = "on" ]]; then | |
| fp_cmd+=" -y"; | |
| fi | |
| $fp_cmd; | |
| fi | |
| if [[ "$_arg_shutdown" != "" ]]; then | |
| if [[ "$_arg_shutdown" != "now" ]]; then | |
| _arg_shutdown="+$_arg_shutdown"; | |
| fi | |
| shutdown "$_arg_shutdown"; | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment