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
+65 -34
View File
@@ -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-guest-virtualbox
# version 0.89.5
# version 0.89.6
# Dependencies: bash coreutils gzip unzip wget xxd dmg2img
# Supported versions:
@@ -102,20 +102,34 @@ clear_input_buffer_then_read
# check dependencies
function check_bash_version() {
if [[ -z "${BASH_VERSION}" ]]; then
function check_shell() {
if [[ "${SHELL}" =~ /bash ]]; then
if [[ -z "${BASH_VERSION}" ]]; then
echo "Can't determine BASH_VERSION. Exiting."
exit
elif [[ "${BASH_VERSION:0:1}" -lt 4 ]]; then
elif [[ "${BASH_VERSION:0:1}" -lt 4 ]]; then
echo "Please run this script on Bash 4.3 or higher."
if [[ -n "$(sw_vers 2>/dev/null)" ]]; then
echo "macOS detected. Make sure the script is not running on"
echo "the default /bin/bash which is version 3."
fi
exit
elif [[ "${BASH_VERSION:0:1}" -eq 4 && "${BASH_VERSION:2:1}" -le 2 ]]; then
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."
exit
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
}
@@ -176,7 +190,8 @@ if [[ -z "$(echo "xxd" | xxd -p 2>/dev/null)" || \
fi
# 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
else
wgetargs="--continue" # ugly
@@ -342,10 +357,9 @@ if [[ -n "$(VBoxManage showvminfo "${vm_name}" 2>/dev/null)" ]]; then
echo ''
echo 'A virtual machine named "'"${vm_name}"'" already exists.'
printf "${warning_color}"'Delete existing virtual machine "'"${vm_name}"'"?'"${default_color}"
delete=""
read -n 1 -p ' [y/N] ' delete
echo ""
if [[ "${delete,,}" == "y" ]]; then
prompt_delete_y_n
if [[ "${delete}" == "y" ]]; then
echo "Deleting ${vm_name} virtual machine."
VBoxManage unregistervm "${vm_name}" --delete
else
echo ""
@@ -653,10 +667,9 @@ print_dimly "stage: create_install_vdi"
if [[ -w "Install ${macOS_release_name}.vdi" ]]; then
echo '"'"Install ${macOS_release_name}.vdi"'" virtual disk image exists.'
printf "${warning_color}"'Delete "'"Install ${macOS_release_name}.vdi"'"?'"${default_color}"
delete=""
read -n 1 -p " [y/N] " delete
prompt_delete_y_n
echo ""
if [[ "${delete,,}" == "y" ]]; then
if [[ "${delete}" == "y" ]]; then
if [[ "$( VBoxManage list runningvms )" =~ \""${vm_name}"\" ]];
then
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."
( VBoxManage startvm "${vm_name}" >/dev/null 2>&1 )
echo "While the script is running, please do not interact with the virtual machine."
[[ -z "${kscd}" ]] && declare_scancode_dict
prompt_lang_utils
prompt_terminal_ready
print_dimly "Please wait"
@@ -839,6 +853,7 @@ if [[ -n $(
fi
echo "The VM will boot from the populated installer base system virtual disk."
( VBoxManage startvm "${vm_name}" >/dev/null 2>&1 )
[[ -z "${kscd}" ]] && declare_scancode_dict
prompt_lang_utils
prompt_terminal_ready
add_another_terminal
@@ -929,10 +944,8 @@ else
fi
echo ""
printf "${warning_color}"'Delete temporary files?'"${default_color}"
delete=""
read -n 1 -p " [y/N] " delete
echo ""
if [[ "${delete,,}" == "y" ]]; then
prompt_delete_y_n
if [[ "${delete}" == "y" ]]; then
rm "${macOS_release_name}_Apple"* \
"${macOS_release_name}_BaseSystem"* \
"${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
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
\"${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}
The stage \"${low_contrast_color}documentation${default_color}\" and \"${low_contrast_color}troubleshoot${default_color}\" is only executed when it is
specified as the first command-line argument, with all subsequent arguments
ignored. When specified after any argument, \"${low_contrast_color}documentation${default_color}\" and \"${low_contrast_color}troubleshoot${default_color}\"
is ignored.
Either of the stages \"${low_contrast_color}documentation${default_color}\" and \"${low_contrast_color}troubleshoot${default_color}\" is only performed when
it is specified as the first command-line argument, with all subsequent
arguments ignored. When specified after any argument, either of \"${low_contrast_color}documentation${default_color}\"
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}\",
\"${low_contrast_color}set_variables${default_color}\", and \"${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
The stage \"${low_contrast_color}check_shell${default_color}\" is always performed when the script loads.
The stages \"${low_contrast_color}check_gnu_coreutils_prefix${default_color}\", \"${low_contrast_color}set_variables${default_color}\", and
\"${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.
${highlight_color}EXAMPLES${default_color}
@@ -1162,7 +1176,9 @@ function sleep() {
# Virtualbox Mac scancodes found here:
# https://wiki.osdev.org/PS/2_Keyboard#Scan_Code_Set_1
# 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"
["1"]="02 82"
["2"]="03 83"
@@ -1295,7 +1311,8 @@ declare -A kscd=(
["<"]="2A 33 B3 AA"
[">"]="2A 34 B4 AA"
["?"]="2A 35 B5 AA"
)
)
}
function clear_input_buffer_then_read() {
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
function send_keys() {
scancode=$(for (( i=0; i < ${#kbstring}; i++ ));
do c[i]=${kbstring:i:1}; echo -n ${kscd[${c[i]}]}" "; done)
scancode=$(for (( i=0; i < ${#kbstring}; i++ )); do
c[${i}]=${kbstring:${i}:1}; echo -n ${kscd[${c[${i}]}]}" "
done)
VBoxManage controlvm "${vm_name}" keyboardputscancode ${scancode} 1>/dev/null 2>&1
}
@@ -1373,9 +1391,21 @@ function would_you_like_to_know_less() {
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
stages='
check_bash_version
check_shell
check_gnu_coreutils_prefix
set_variables
welcome
@@ -1396,12 +1426,14 @@ stages='
documentation
troubleshoot
'
check_shell
stages_without_newlines="${stages//[$'\r\n']/}"
[[ "${1}" = "documentation" ]] && documentation && exit
if [[ "${1}" = "troubleshoot" ]]; then set_variables; check_dependencies >/dev/null; troubleshoot; exit; fi
stages="${stages//documentation/}" # strip all occurrences of "documentation"
stages="${stages//troubleshoot/}" # strip all occurrences of "troubleshoot"
[[ -z "${1}" ]] && for stage in ${stages}; do ${stage}; done && exit
stages_without_newlines="${stages//[$'\r\n']/ }" # replace newline with space character
stages_without_newlines="${stages_without_newlines//documentation/}" # strip all occurrences of "documentation"
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
# for the command-line argument checking below to work
for argument in $@; do
@@ -1415,7 +1447,6 @@ for argument in $@; do
exit
fi
done
check_bash_version
check_gnu_coreutils_prefix
set_variables
check_dependencies