Newer
Older
###################################################
# VirtualBox VM Scripts 18.6.1 - Common functions
###################################################
# templates folder
TEMPLATE_DIR="${SRC_DIR}/templates"
#-------------------------------------------------------------------------------
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# some terminal output formatting helpers
BOLD=$(tput bold) # bold
NORMAL=$(tput sgr0) # reset formatting
RED=$(tput setaf 1) # red
GREEN=$(tput setaf 2) # green
ERROR=$RED # error
# WARNING: this function is just for reference,
# its only purpose is to remind me how to get a script's source folder properly
# (the inner command is used at the very beginning of these vbox-scripts, too)
get_script_source_folder () {
local myscript=$1
# some alternatives:
#echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#echo "$( cd "${BASH_SOURCE[0]%/*}" && pwd )"
#echo "$(dirname $(readlink -f ${BASH_SOURCE[0]}))"
#echo "$(dirname $(readlink -f $0))"
echo "$(dirname $(readlink -f $myscript))"
}
# Check if files matching a wildcard name exist.
# Example: doesAnyFileExist "test*.settings"
doesAnyFileExist () {
local arg="$*"
local files=($arg)
[ ${#files[@]} -gt 1 ] || [ ${#files[@]} -eq 1 ] && [ -e "${files[0]}" ]
}
#declare -a vms=()
# Get all VMs' IDs and put them in the global array vms
#create_vm_list () {
# vms=()
# local vms_user=()
# local vms_global=()
#
# if [ -d "${VM_SETTINGS_DIR}" ]; then
# # add settings files in the folder
# for f in $(ls -U "${VM_SETTINGS_DIR}/"*.settings 2> /dev/null) ; do
# vms_global=("${vms_global[@]}" "`basename $f .settings`")
# done
# fi
# if [ -d "${VM_USER_SETTINGS_DIR}" ]; then
# # add settings files in the folder
# for f in $(ls -U "${VM_USER_SETTINGS_DIR}/"*.settings 2> /dev/null) ; do
# vms_user=("${vms_user[@]}" "`basename $f .settings`")
# done
# fi
#
# # get the final list removing duplicates
# OLDIFS="$IFS"
# IFS=$'\n'
# vms=(`for R in "${vms_user[@]}" "${vms_global[@]}" ; do echo "$R" ; done | sort -du`)
# IFS="$OLDIFS"
#}
declare -A vms
# Get all VMs' IDs and put them in the global associative array vms
# key <== VM_ID
# value <== settings file
vms=()
if [ -d "${VM_SETTINGS_DIR}" ]; then
# add settings files in the folder
for f in $(ls -U "${VM_SETTINGS_DIR}/"*.settings 2> /dev/null) ; do
vms_id=("`basename $f .settings`")
vms[$vms_id]=$f
done
fi
if [ -d "${VM_USER_SETTINGS_DIR}" ]; then
# add settings files in the folder
for f in $(ls -U "${VM_USER_SETTINGS_DIR}/"*.settings 2> /dev/null) ; do
vms_id=("`basename $f .settings`")
vms[$vms_id]=$f
done
fi
# Print all available VMs' IDs
create_vm_list
# make a sorted array by key (vm_id)
mapfile -d '' sorted < <(printf '%s\0' "${!vms[@]}" | sort -z)
echo "Available VM IDs:"
for k in "${sorted[@]}"; do
echo -n "${BOLD} ${k}${NORMAL}"
if [[ ${vms[$k]} == ${VM_USER_SETTINGS_DIR}* ]]; then
echo "${GREEN} (user)${NORMAL}"
else
echo
fi
done
# Get a VM's settings given its ID
get_vm_settings_by_id () {
create_vm_list
echo "${vms[$1]}"
# local fuser=""
# local fglobal=""
# fuser=`find ${VM_USER_SETTINGS_DIR} -name $1.settings`
# if [ "$fuser" != "" ]; then
# echo "$fuser"
# else
# fglobal=`find ${VM_SETTINGS_DIR} -name $1.settings`
# if [ "$fglobal" != "" ]; then
# echo "$fglobal"
# else
# echo ""
# fi
# fi
# Get a VM's settings given its name
get_vm_settings_by_name () {
local fuser=""
local fglobal=""
fuser=`grep -liR "VM_NAME=\"$1\"" ${VM_USER_SETTINGS_DIR}/*.settings`
if [ "$fuser" != "" ]; then
echo "$fuser"
else
fglobal=`grep -liR "VM_NAME=\"$1\"" ${VM_SETTINGS_DIR}/*.settings`
if [ "$fglobal" != "" ]; then
echo "$fglobal"
else
echo ""
fi
fi
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Get a VM's settings given its ID or name
get_vm_settings () {
local vm_id=$1
local vm_settings=""
vm_settings=`get_vm_settings_by_id "${vm_id}"`
if [[ "$vm_settings" == "" ]]; then
vm_settings=`get_vm_settings_by_name "${vm_id}"`
fi
echo "${vm_settings}"
}
# Get a VM's ID given its name
get_vm_id_by_name () {
local fuser=`grep -liR "VM_NAME=\"$1\"" ${VM_USER_SETTINGS_DIR}/*.settings`
if [ "$fuser" != "" ]; then
echo `basename $fuser .settings`
else
local fglobal=`grep -liR "VM_NAME=\"$1\"" ${VM_SETTINGS_DIR}/*.settings`
if [ "$fglobal" != "" ]; then
echo `basename $fglobal .settings`
else
echo ""
fi
fi
}
# Terminate the script with a message and an error code
die () {
echo >&2 "$@"
exit 1