2014-06-17 10:44:46 +02:00
|
|
|
# This file contains bash functions that may be used by both guest and host
|
|
|
|
# systems.
|
|
|
|
|
|
|
|
# Non-recursive removal of all files except README.*
|
|
|
|
function clean_dir {
|
2014-08-30 13:04:15 +02:00
|
|
|
local target_dir=$1
|
|
|
|
if [ ! -e "$target_dir" ]; then
|
|
|
|
mkdir -pv "$target_dir"
|
|
|
|
elif [ ! -d "$target_dir" ]; then
|
|
|
|
echo >&2 "Not a directory: $target_dir"
|
2014-06-17 10:44:46 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
shopt -s nullglob
|
2014-08-30 13:04:15 +02:00
|
|
|
local entries=("$target_dir"/*)
|
|
|
|
if [ -n "${entries[0]-}" ]; then
|
|
|
|
for f in "${entries[@]}"; do
|
2014-06-17 10:44:46 +02:00
|
|
|
# Skip directories
|
|
|
|
if [ ! -f "$f" ]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Skip README.*
|
|
|
|
if [[ $f =~ /README\. ]]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
rm -f "$f"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function is_root {
|
|
|
|
if [ $EUID -eq 0 ]; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function yes_or_no {
|
|
|
|
local prompt=$1
|
|
|
|
local input=""
|
|
|
|
while [ : ]; do
|
|
|
|
read -p "$prompt (Y/n): " input
|
|
|
|
case "$input" in
|
|
|
|
N|n)
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
""|Y|y)
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
*)
|
2015-04-27 11:46:57 +05:30
|
|
|
echo -e "${CError:-}Invalid input: ${CData:-}$input${CReset:-}"
|
2014-06-17 10:44:46 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
# Helpers to incrementally number files via name prefixes
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function get_next_file_number {
|
2014-08-30 13:04:15 +02:00
|
|
|
local dir=$1
|
|
|
|
local ext=${2:-""}
|
2014-06-17 10:44:46 +02:00
|
|
|
|
|
|
|
# Get number of *.log files in directory
|
|
|
|
shopt -s nullglob
|
2014-08-30 13:04:15 +02:00
|
|
|
if [ -n "$ext" ]; then
|
2014-06-17 10:44:46 +02:00
|
|
|
# Count files with specific extension
|
2014-08-30 13:04:15 +02:00
|
|
|
local files=("$dir/"*".$ext")
|
2014-06-17 10:44:46 +02:00
|
|
|
else
|
|
|
|
# Count all files
|
2014-08-30 13:04:15 +02:00
|
|
|
local files=("$dir/"*)
|
2014-06-17 10:44:46 +02:00
|
|
|
fi
|
2014-08-30 13:04:15 +02:00
|
|
|
echo "${#files[*]}"
|
2014-06-17 10:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_next_prefix {
|
2014-08-30 13:04:15 +02:00
|
|
|
local dir=$1
|
|
|
|
local ext=$2
|
2014-06-17 10:44:46 +02:00
|
|
|
# Number of digits in prefix string (default 3)
|
2014-08-30 13:04:15 +02:00
|
|
|
local digits=${3:-3}
|
2014-06-17 10:44:46 +02:00
|
|
|
|
2014-08-30 13:04:15 +02:00
|
|
|
# Get number of *.$ext files in $dir
|
|
|
|
local cnt="$(get_next_file_number "$dir" "$ext")"
|
2014-06-17 10:44:46 +02:00
|
|
|
|
2014-08-30 13:04:15 +02:00
|
|
|
printf "%0${digits}d" "$cnt"
|
2014-06-17 10:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# vim: set ai ts=4 sw=4 et ft=sh:
|