From 0ded89a78aeaf7e47e8fa74b8804e9ec7c7eedad Mon Sep 17 00:00:00 2001 From: slowpeek Date: Wed, 13 Oct 2021 17:03:56 +0300 Subject: [PATCH 1/2] Tidy the script startup code --- macos-guest-virtualbox.sh | 89 ++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 25 deletions(-) diff --git a/macos-guest-virtualbox.sh b/macos-guest-virtualbox.sh index 38698f4..691f056 100755 --- a/macos-guest-virtualbox.sh +++ b/macos-guest-virtualbox.sh @@ -1096,17 +1096,25 @@ fi function and_all_subsequent_stages() { # if exactly two arguments were specified on the command line, and the first is a stage title, # then perform all stages subsequent to the specified stage, otherwise do nothing. - # first_argument is already sanitized so it's safe (though incorrect) to use as a regex (for brevity) - first_argument=${specified_arguments%% *} - last_argument=${specified_arguments##* } - [[ "${first_argument} ${last_argument}" = "${specified_arguments}" ]] && \ - [[ "${stages}" =~ "${first_argument}" ]] && \ - for stage in ${stages##*${first_argument}}; do ${stage}; done + if [[ ${#specified_arguments[@]} == 2 ]]; then + local stage + local first_argument=${specified_arguments[0]} + local run=n + + for stage in "${stages[@]}"; do + [[ $run == n ]] || "$stage" + + if [[ $stage == "$first_argument" ]]; then + # Run all subsequent stages. + run=y + fi + done + fi } function documentation() { low_contrast_stages="" -for stage in ${stages}; do +for stage in "${stages[@]}"; do low_contrast_stages="${low_contrast_stages}"' '"${low_contrast_color}${stage}${default_color}"$'\n' done echo -ne "\n ${highlight_color}NAME${default_color} @@ -1685,7 +1693,7 @@ function prompt_delete_y_n() { # command-line argument processing check_shell -stages=' +stages=( check_gnu_coreutils_prefix set_variables welcome @@ -1703,27 +1711,58 @@ stages=' create_target_virtual_disk populate_macos_target_disk prompt_delete_temporary_files -' -[[ -z "${1}" ]] && for stage in ${stages}; do ${stage}; done && exit -[[ "${1}" = "documentation" ]] && documentation && exit -valid_arguments=(${stages//$'[\r\n]'/ } check_shell troubleshoot documentation and_all_subsequent_stages) -specified_arguments="$@" # this variable is used in the function "and_all_subsequent_stages" -for specified_arg in "$@"; do - there_is_a_match="" - # doing matching the long way to prevent delimiter confusion - for valid_arg in "${valid_arguments[@]}"; do - [[ "${valid_arg}" = "${specified_arg}" ]] && there_is_a_match="true" && break +) + +if [[ $# == 0 ]]; then + for stage in "${stages[@]}"; do + "$stage" done - if [[ -z "${there_is_a_match}" ]]; then - echo -e "\nOne or more specified arguments is not recognized." - echo -e "\nRecognized stages:\n${stages}" - echo -e "Other recognized arguments:\n\n check_shell\n documentation\n troubleshoot\n and_all_subsequent_stages" - echo -e "\nView documentation by entering the following command:" - would_you_like_to_know_less + + exit +fi + +if [[ $1 == documentation ]]; then + documentation + exit +fi + +other_commands=( + check_shell + troubleshoot + documentation + and_all_subsequent_stages +) + +specified_arguments=("$@") # this variable is used in the function "and_all_subsequent_stages" +for specified_arg; do + there_is_a_match=n + for valid_arg in "${stages[@]}" "${other_commands[@]}"; do + if [[ $valid_arg == "$specified_arg" ]]; then + there_is_a_match=y + break + fi + done + + if [[ $there_is_a_match == n ]]; then + cat < Date: Thu, 14 Oct 2021 00:30:09 +0300 Subject: [PATCH 2/2] arrays instead of strings int comparisons with `-eq` or `-ne` and without quotes `do ${var}` without quotes --- macos-guest-virtualbox.sh | 85 +++++++++++++-------------------------- 1 file changed, 29 insertions(+), 56 deletions(-) diff --git a/macos-guest-virtualbox.sh b/macos-guest-virtualbox.sh index 691f056..28687a8 100755 --- a/macos-guest-virtualbox.sh +++ b/macos-guest-virtualbox.sh @@ -2,7 +2,7 @@ # Push-button installer of macOS on VirtualBox # (c) myspaghetti, licensed under GPL2.0 or higher # url: https://github.com/myspaghetti/macos-virtualbox -# version 0.99.1.3 +# version 0.99.1.4 # Dependencies: bash coreutils gzip unzip wget xxd dmg2img # Optional features: tesseract-ocr tesseract-ocr-eng @@ -205,6 +205,9 @@ if [[ -n "$(gcsplit --help 2>/dev/null)" ]]; then function seq() { gseq "$@" } + function sort() { + gsort "$@" + } fi } @@ -1096,20 +1099,12 @@ fi function and_all_subsequent_stages() { # if exactly two arguments were specified on the command line, and the first is a stage title, # then perform all stages subsequent to the specified stage, otherwise do nothing. - if [[ ${#specified_arguments[@]} == 2 ]]; then - local stage - local first_argument=${specified_arguments[0]} - local run=n - - for stage in "${stages[@]}"; do - [[ $run == n ]] || "$stage" - - if [[ $stage == "$first_argument" ]]; then - # Run all subsequent stages. - run=y - fi - done - fi + [[ ${#specified_arguments[@]} -ne 2 ]] && return + for stage in "${stages[@]}"; do + [[ "${stages[0]}" = "${specified_arguments[0]}" ]] && break + stages=( "${stages[@]:1}" ) + done + for stage in "${stages[@]:1}"; do ${stage}; done } function documentation() { @@ -1713,19 +1708,6 @@ stages=( prompt_delete_temporary_files ) -if [[ $# == 0 ]]; then - for stage in "${stages[@]}"; do - "$stage" - done - - exit -fi - -if [[ $1 == documentation ]]; then - documentation - exit -fi - other_commands=( check_shell troubleshoot @@ -1733,36 +1715,27 @@ other_commands=( and_all_subsequent_stages ) -specified_arguments=("$@") # this variable is used in the function "and_all_subsequent_stages" -for specified_arg; do - there_is_a_match=n - for valid_arg in "${stages[@]}" "${other_commands[@]}"; do - if [[ $valid_arg == "$specified_arg" ]]; then - there_is_a_match=y - break - fi - done +# script called without arguments +[[ $# -eq 0 ]] && for stage in "${stages[@]}"; do ${stage}; done && exit - if [[ $there_is_a_match == n ]]; then - cat <