remove eval

This commit is contained in:
illiliti 2020-02-25 15:40:07 +03:00
parent 81f7aac01a
commit d590409a99

View File

@ -21,14 +21,12 @@ msg() {
} }
usage() { usage() {
scriptdir=$(readlink -f $(dirname "$0"))
# TODO more options # TODO more options
cat << EOF cat << EOF
usage: $0 [options] usage: $0 [options]
-o, --output <file> output file. default is ${scriptdir}/initramfs-$(uname -r) -o, --output <file> output file
-c, --config <file> config file. default is ${scriptdir}/config -c, --config <file> config file
-f, --files <dir> files directory. default is ${scriptdir}/files -f, --files <dir> files directory
EOF EOF
} }
@ -37,15 +35,15 @@ parse_args() {
while [ "$1" ]; do while [ "$1" ]; do
case "$1" in case "$1" in
-o | --output) -o | --output)
initramfs="${2?}" _initramfs="${2:?}"
shift 2 shift 2
;; ;;
-f | --files) -f | --files)
filesdir="${2?}" _filesdir="${2:?}"
shift 2 shift 2
;; ;;
-c | --config) -c | --config)
config="${2?}" _config="${2:?}"
shift 2 shift 2
;; ;;
-h | --help) -h | --help)
@ -62,19 +60,13 @@ parse_args() {
} }
parse_conf() { parse_conf() {
while read -r line; do . "${_config:-./config}" ||
msg panic "failed to parse config"
# ignore comments kernel="${kernel:-$(uname -r)}"
if [ ! "${line##\#*}" ]; then moddir="${moddir:-/lib/modules}"
continue filesdir="${_filesdir:-./files}"
initramfs="${_initramfs:-${initramfs:-./initramfs-${kernel}}}"
# check if variable already exists via 'variable indirection' method
# if no exists then 'source' variable
# see https://stackoverflow.com/q/36235612
elif [ ! "$(eval printf "%s" "\"\$${line%%=*}\"")" ]; then
eval "$line"
fi
done < "${config:-$(readlink -f $(dirname "$0"))/config}" || msg panic "failed to parse config"
} }
create_workdir() { create_workdir() {
@ -462,11 +454,6 @@ create_initramfs() {
parse_args "$@" parse_args "$@"
parse_conf parse_conf
: "${kernel:=$(uname -r)}"
: "${moddir:=/lib/modules}"
: "${filesdir:=$(readlink -f $(dirname "$0"))/files}"
: "${initramfs:=$(readlink -f $(dirname "$0"))/initramfs-${kernel}}"
# remove workdir on exit or unexpected error # remove workdir on exit or unexpected error
trap remove_workdir EXIT INT trap remove_workdir EXIT INT