Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

73 lines (66 sloc) 1.871 kB
#!/bin/sh
#
# # Hash in file Module
#
# Allows manipulation of multiline values and keyss containing - and \.
#
# TODO: Replace hash() with zsh mapfiles / internal constructs merge into config.
hash()
{
typeset _token _file _keys _valid_keys
_valid_keys='^[^ ^=]+'
while (( $# ))
do
_token="$1" && shift
case "${_token}" in
(from)
;;
(key)
_key="$1"
(( $# )) && shift || __sm.log.fail "Key name must follow keyword 'key'"
;;
(variable)
_variable="$1"
(( $# )) && shift || __sm.log.fail "Variable name must follow keyword 'variable'"
;;
(*)
if [[ -z "${_command}" ]]
then
_command="${_token}"
else
_keys+=("$1")
fi
;;
esac
done
case "${_command}" in
(keys)
[[ -n "${_file}" ]] || __sm.log.fail " Filename not given."
echo -n $(grep -E "${_valid_keys}=" "${_file}" | grep -Eo "${_valid_keys}")
;;
(read)
# Read value from given file($1) for given key($2), and assign it to a variable($3).
[[ -n "${_file}" ]] || __sm.log.fail "Filename was not given (from '{filename}')."
[[ -n "${_key}" ]] || __sm.log.fail "Key name was not given (key '{key}')."
if [[ -n "${_variable}" ]]
then
eval "${variable}=$(
awk \
-v key="^${key}=" \
-v valid_keys="${valid_keys}=" \
'{
if ($0 ~ valid_keys) found=0;
if ($0 ~ key) {found=1; gsub(key,"");};
if (found == 1) print $0
}' \"$file\"
)"
else
awk -v key="^${key}=" -v valid_keys="${_valid_keys}=" '{
if ($0 ~ valid_keys) { found=0; }
if ($0 ~ key) { found=1; gsub(key,""); };
if (found == 1) print $0
}' "${_file}"
fi
;;
esac
}
Jump to Line
Something went wrong with that request. Please try again.