making script compatible with zsh for absolutely no reason

This commit is contained in:
Jack
2020-04-09 01:04:10 +03:00
parent c0dda851d1
commit 08af5a567d
+61 -30
View File
@@ -2,7 +2,7 @@
# Push-button installer of macOS on VirtualBox # Push-button installer of macOS on VirtualBox
# (c) myspaghetti, licensed under GPL2.0 or higher # (c) myspaghetti, licensed under GPL2.0 or higher
# url: https://github.com/myspaghetti/macos-guest-virtualbox # url: https://github.com/myspaghetti/macos-guest-virtualbox
# version 0.89.5 # version 0.89.6
# Dependencies: bash coreutils gzip unzip wget xxd dmg2img # Dependencies: bash coreutils gzip unzip wget xxd dmg2img
# Supported versions: # Supported versions:
@@ -102,7 +102,8 @@ clear_input_buffer_then_read
# check dependencies # check dependencies
function check_bash_version() { function check_shell() {
if [[ "${SHELL}" =~ /bash ]]; then
if [[ -z "${BASH_VERSION}" ]]; then if [[ -z "${BASH_VERSION}" ]]; then
echo "Can't determine BASH_VERSION. Exiting." echo "Can't determine BASH_VERSION. Exiting."
exit exit
@@ -117,6 +118,19 @@ elif [[ "${BASH_VERSION:0:1}" -eq 4 && "${BASH_VERSION:2:1}" -le 2 ]]; then
echo "Please run this script on Bash 4.3 or higher." echo "Please run this script on Bash 4.3 or higher."
exit exit
fi fi
elif [[ "${SHELL}" =~ /zsh ]]; then
if [[ -z "${ZSH_VERSION}" ]]; then
echo "Can't determine ZSH_VERSION. Exiting."
exit
elif [[ "${ZSH_VERSION:0:1}" -lt 5 ]]; then
echo "Please run this script on zsh 5 or higher."
exit
fi
setopt extendedglob sh_word_split ksh_arrays posix_argzero
else
echo "Can't determine SHELL. Exiting."
exit
fi
} }
function check_gnu_coreutils_prefix() { function check_gnu_coreutils_prefix() {
@@ -176,7 +190,8 @@ if [[ -z "$(echo "xxd" | xxd -p 2>/dev/null)" || \
fi fi
# wget supports --show-progress from version 1.16 # wget supports --show-progress from version 1.16
if [[ "$(wget --version 2>/dev/null | head -n 1)" =~ 1\.1[6-9]|1\.2[0-9] ]]; then regex='1\.1[6-9]|1\.2[0-9]'
if [[ "$(wget --version 2>/dev/null | head -n 1)" =~ ${regex} ]]; then
wgetargs="--quiet --continue --show-progress" # pretty wgetargs="--quiet --continue --show-progress" # pretty
else else
wgetargs="--continue" # ugly wgetargs="--continue" # ugly
@@ -342,10 +357,9 @@ if [[ -n "$(VBoxManage showvminfo "${vm_name}" 2>/dev/null)" ]]; then
echo '' echo ''
echo 'A virtual machine named "'"${vm_name}"'" already exists.' echo 'A virtual machine named "'"${vm_name}"'" already exists.'
printf "${warning_color}"'Delete existing virtual machine "'"${vm_name}"'"?'"${default_color}" printf "${warning_color}"'Delete existing virtual machine "'"${vm_name}"'"?'"${default_color}"
delete="" prompt_delete_y_n
read -n 1 -p ' [y/N] ' delete if [[ "${delete}" == "y" ]]; then
echo "" echo "Deleting ${vm_name} virtual machine."
if [[ "${delete,,}" == "y" ]]; then
VBoxManage unregistervm "${vm_name}" --delete VBoxManage unregistervm "${vm_name}" --delete
else else
echo "" echo ""
@@ -653,10 +667,9 @@ print_dimly "stage: create_install_vdi"
if [[ -w "Install ${macOS_release_name}.vdi" ]]; then if [[ -w "Install ${macOS_release_name}.vdi" ]]; then
echo '"'"Install ${macOS_release_name}.vdi"'" virtual disk image exists.' echo '"'"Install ${macOS_release_name}.vdi"'" virtual disk image exists.'
printf "${warning_color}"'Delete "'"Install ${macOS_release_name}.vdi"'"?'"${default_color}" printf "${warning_color}"'Delete "'"Install ${macOS_release_name}.vdi"'"?'"${default_color}"
delete="" prompt_delete_y_n
read -n 1 -p " [y/N] " delete
echo "" echo ""
if [[ "${delete,,}" == "y" ]]; then if [[ "${delete}" == "y" ]]; then
if [[ "$( VBoxManage list runningvms )" =~ \""${vm_name}"\" ]]; if [[ "$( VBoxManage list runningvms )" =~ \""${vm_name}"\" ]];
then then
echo '"'"Install ${macOS_release_name}.vdi"'" may be deleted' echo '"'"Install ${macOS_release_name}.vdi"'" may be deleted'
@@ -753,6 +766,7 @@ fi
echo "Starting virtual machine ${vm_name}. This should take a couple of minutes." echo "Starting virtual machine ${vm_name}. This should take a couple of minutes."
( VBoxManage startvm "${vm_name}" >/dev/null 2>&1 ) ( VBoxManage startvm "${vm_name}" >/dev/null 2>&1 )
echo "While the script is running, please do not interact with the virtual machine." echo "While the script is running, please do not interact with the virtual machine."
[[ -z "${kscd}" ]] && declare_scancode_dict
prompt_lang_utils prompt_lang_utils
prompt_terminal_ready prompt_terminal_ready
print_dimly "Please wait" print_dimly "Please wait"
@@ -839,6 +853,7 @@ if [[ -n $(
fi fi
echo "The VM will boot from the populated installer base system virtual disk." echo "The VM will boot from the populated installer base system virtual disk."
( VBoxManage startvm "${vm_name}" >/dev/null 2>&1 ) ( VBoxManage startvm "${vm_name}" >/dev/null 2>&1 )
[[ -z "${kscd}" ]] && declare_scancode_dict
prompt_lang_utils prompt_lang_utils
prompt_terminal_ready prompt_terminal_ready
add_another_terminal add_another_terminal
@@ -929,10 +944,8 @@ else
fi fi
echo "" echo ""
printf "${warning_color}"'Delete temporary files?'"${default_color}" printf "${warning_color}"'Delete temporary files?'"${default_color}"
delete="" prompt_delete_y_n
read -n 1 -p " [y/N] " delete if [[ "${delete}" == "y" ]]; then
echo ""
if [[ "${delete,,}" == "y" ]]; then
rm "${macOS_release_name}_Apple"* \ rm "${macOS_release_name}_Apple"* \
"${macOS_release_name}_BaseSystem"* \ "${macOS_release_name}_BaseSystem"* \
"${macOS_release_name}_Install"* \ "${macOS_release_name}_Install"* \
@@ -969,17 +982,18 @@ patiently and, less than ten times, press enter when prompted.
The script is divided into stages. Stage titles may be given as command-line The script is divided into stages. Stage titles may be given as command-line
arguments for the script. When the script is run with no command-line arguments for the script. When the script is run with no command-line
arguments, each of the available stages except \"${low_contrast_color}documentation${default_color}\" and arguments, each of the available stages except \"${low_contrast_color}documentation${default_color}\" and
\"${low_contrast_color}troubleshoot${default_color}\" is executed in succession in the order listed: \"${low_contrast_color}troubleshoot${default_color}\" is performed in succession in the order listed:
${low_contrast_stages} ${low_contrast_stages}
The stage \"${low_contrast_color}documentation${default_color}\" and \"${low_contrast_color}troubleshoot${default_color}\" is only executed when it is Either of the stages \"${low_contrast_color}documentation${default_color}\" and \"${low_contrast_color}troubleshoot${default_color}\" is only performed when
specified as the first command-line argument, with all subsequent arguments it is specified as the first command-line argument, with all subsequent
ignored. When specified after any argument, \"${low_contrast_color}documentation${default_color}\" and \"${low_contrast_color}troubleshoot${default_color}\" arguments ignored. When specified after any argument, either of \"${low_contrast_color}documentation${default_color}\"
is ignored. and \"${low_contrast_color}troubleshoot${default_color}\" is ignored.
The four stages \"${low_contrast_color}check_bash_version${default_color}\", \"${low_contrast_color}check_gnu_coreutils_prefix${default_color}\", The stage \"${low_contrast_color}check_shell${default_color}\" is always performed when the script loads.
\"${low_contrast_color}set_variables${default_color}\", and \"${low_contrast_color}check_dependencies${default_color}\" are always performed when any stage The stages \"${low_contrast_color}check_gnu_coreutils_prefix${default_color}\", \"${low_contrast_color}set_variables${default_color}\", and
title other than \"${low_contrast_color}documentation${default_color}\" or \"${low_contrast_color}troubleshoot${default_color}\" is specified as the first \"${low_contrast_color}check_dependencies${default_color}\" are always performed when any stage title other than
\"${low_contrast_color}documentation${default_color}\" or \"${low_contrast_color}troubleshoot${default_color}\" is specified as the first
argument, and the rest of the stages are parsed only after the checks pass. argument, and the rest of the stages are parsed only after the checks pass.
${highlight_color}EXAMPLES${default_color} ${highlight_color}EXAMPLES${default_color}
@@ -1162,7 +1176,9 @@ function sleep() {
# Virtualbox Mac scancodes found here: # Virtualbox Mac scancodes found here:
# https://wiki.osdev.org/PS/2_Keyboard#Scan_Code_Set_1 # https://wiki.osdev.org/PS/2_Keyboard#Scan_Code_Set_1
# First half of hex code - press, second half - release, unless otherwise specified # First half of hex code - press, second half - release, unless otherwise specified
declare -A kscd=( function declare_scancode_dict() {
declare -gA kscd
kscd=(
["ESC"]="01 81" ["ESC"]="01 81"
["1"]="02 82" ["1"]="02 82"
["2"]="03 83" ["2"]="03 83"
@@ -1296,6 +1312,7 @@ declare -A kscd=(
[">"]="2A 34 B4 AA" [">"]="2A 34 B4 AA"
["?"]="2A 35 B5 AA" ["?"]="2A 35 B5 AA"
) )
}
function clear_input_buffer_then_read() { function clear_input_buffer_then_read() {
while read -d '' -r -t 0; do read -d '' -t 0.1 -n 10000; break; done while read -d '' -r -t 0; do read -d '' -t 0.1 -n 10000; break; done
@@ -1304,8 +1321,9 @@ function clear_input_buffer_then_read() {
# read variable kbstring and convert string to scancodes and send to guest vm # read variable kbstring and convert string to scancodes and send to guest vm
function send_keys() { function send_keys() {
scancode=$(for (( i=0; i < ${#kbstring}; i++ )); scancode=$(for (( i=0; i < ${#kbstring}; i++ )); do
do c[i]=${kbstring:i:1}; echo -n ${kscd[${c[i]}]}" "; done) c[${i}]=${kbstring:${i}:1}; echo -n ${kscd[${c[${i}]}]}" "
done)
VBoxManage controlvm "${vm_name}" keyboardputscancode ${scancode} 1>/dev/null 2>&1 VBoxManage controlvm "${vm_name}" keyboardputscancode ${scancode} 1>/dev/null 2>&1
} }
@@ -1373,9 +1391,21 @@ function would_you_like_to_know_less() {
fi fi
} }
function prompt_delete_y_n() {
delete=""
if [[ "${SHELL}" =~ /zsh ]]; then
read -s -q delete\?' [y/N] '
delete="${delete:l}"
elif [[ "${SHELL}" =~ /bash ]]; then
read -n 1 -p ' [y/N] ' delete
delete="${delete,,}"
fi
echo ""
}
# command-line argument processing # command-line argument processing
stages=' stages='
check_bash_version check_shell
check_gnu_coreutils_prefix check_gnu_coreutils_prefix
set_variables set_variables
welcome welcome
@@ -1396,12 +1426,14 @@ stages='
documentation documentation
troubleshoot troubleshoot
' '
check_shell
stages_without_newlines="${stages//[$'\r\n']/}" stages_without_newlines="${stages//[$'\r\n']/}"
[[ "${1}" = "documentation" ]] && documentation && exit [[ "${1}" = "documentation" ]] && documentation && exit
if [[ "${1}" = "troubleshoot" ]]; then set_variables; check_dependencies >/dev/null; troubleshoot; exit; fi if [[ "${1}" = "troubleshoot" ]]; then set_variables; check_dependencies >/dev/null; troubleshoot; exit; fi
stages="${stages//documentation/}" # strip all occurrences of "documentation" stages_without_newlines="${stages//[$'\r\n']/ }" # replace newline with space character
stages="${stages//troubleshoot/}" # strip all occurrences of "troubleshoot" stages_without_newlines="${stages_without_newlines//documentation/}" # strip all occurrences of "documentation"
[[ -z "${1}" ]] && for stage in ${stages}; do ${stage}; done && exit stages_without_newlines="${stages_without_newlines//troubleshoot/}" # strip all occurrences of "troubleshoot"
[[ -z "${1}" ]] && for stage in ${stages_without_newlines}; do ${stage}; done && exit
# every stage name must be preceded and followed by a space character # every stage name must be preceded and followed by a space character
# for the command-line argument checking below to work # for the command-line argument checking below to work
for argument in $@; do for argument in $@; do
@@ -1415,7 +1447,6 @@ for argument in $@; do
exit exit
fi fi
done done
check_bash_version
check_gnu_coreutils_prefix check_gnu_coreutils_prefix
set_variables set_variables
check_dependencies check_dependencies