#!/bin/bash ################################################### # VirtualBox VM Scripts 18.6.1 - Common functions # by ulisse aka A. Livio Beccaria ################################################### # the user executing these scripts USER=`whoami` # templates folder TEMPLATE_DIR="${SRC_DIR}/templates" #------------------------------------------------------------------------------- # 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 create_vm_list () { 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 print_vm_list () { 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 } # 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 }