-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions
executable file
·49 lines (42 loc) · 1.57 KB
/
functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
# Read a configuration file.
# Source: http://rosettacode.org/wiki/Read_a_configuration_file#UNIX_Shell
readconfig() (
# redirect stdin to read from the given filename
exec 0<"$1"
declare -l varname # Original version forced everything to lowercase. Here we'll leave choice of case up to the software designer and user.
while IFS=$' =\t' read -ra words; do
# is it a comment or blank line?
if [[ ${#words[@]} -eq 0 || ${words[0]} == ["#;"]* ]]; then
continue
fi
# get the variable name
varname=${words[0]}
# split all the other words by comma
value="${words[*]:1}"
oldIFS=$IFS IFS=,
values=( $value )
IFS=$oldIFS
# assign the other words to a "scalar" variable or array
case ${#values[@]} in
0) printf '%s=true\n' "$varname" ;;
1) printf '%s=%q\n' "$varname" "${values[0]}" ;;
*) n=0
for value in "${values[@]}"; do
value=${value# }
printf '%s[%d]=%q\n' "$varname" $((n++)) "${value% }"
done
;;
esac
done
)
# THESE COMMANDS ARE USED FOR TESTING THE FUNCTION
# parse the config file and evaluate the output in the current shell
source <( readconfig config.file )
# The following was used to test this function.
echo "FULLNAME = $FULLNAME"
echo "FAVOURITEFRUIT = $FAVOURITEFRUIT"
echo "NEEDSPEELING = $NEEDSPEELING"
echo "SEEDSREMOVED = $SEEDSREMOVED"
for i in "${!OTHERFAMILY[@]}"; do
echo "OTHERFAMILY[$i] = ${OTHERFAMILY[i]}"
done