forked from FluxionNetwork/fluxion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOUtils.sh
executable file
·149 lines (114 loc) · 5.05 KB
/
IOUtils.sh
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bash
if [ "$IOUtilsVersion" ]; then return 0; fi
readonly IOUtilsVersion="1.0"
if [ ! "$FLUXIONLibPath" ]; then return 1; fi
IOUtilsHeader="[x] ================================ [x]"
IOUtilsQueryMark="[-] "
IOUtilsPrompt="[$USER@$HOSTNAME]> "
if [ ! "$ArrayUtilsVersion" ]; then
source "$FLUXIONLibPath/ArrayUtils.sh"
fi
io_input_choice() {
local __io_input_choice__choice
until [ ! -z "$__io_input_choice__choice" ]; do
echo -ne "$IOUtilsPrompt"
local __io_input_choice__input
read __io_input_choice__input
local __io_input_choice__choices
for __io_input_choice__choices in ${@}; do
array_contains $__io_input_choice__choices "$__io_input_choice__input"
if [ $? -eq 0 ]; then
__io_input_choice__choice="$__io_input_choice__input"
break
fi
done
done
IOInputChoice=$__io_input_choice__choice
}
io_dynamic_output() {
eval 'echo -ne "'${@}'"'
}
io_input_enumerated_choice() {
local __io_input_enumerated_choice__choices=("${!1}")
local __io_input_enumerated_choice__indexes=($(seq ${#__io_input_numeric_choice__choices[@]}))
io_input_choice __io_input_enumerated_choice__indexes[@]
IOInputEnumeratedChoice=${__io_input_enumerated_choice__choices[$IOInputChoice]}
}
# This function outputs formatted lines of fields.
# The function takes an output file (like stdout),
# a "printf format string," and a variable number
# of indirect-expansion passed arrays (reference).
# NOTICE: At least the first array must be passed!
# Example: /dev/stdout "%s is %s." name[@] mood[@]
io_output_format_fields() {
# Determine the amount of arguments passed.
local __io_output_format_fields__argument_count=${#@}
# Load locally by indirect expansion, ${! ... },
# and mangle the variable number argument arrays.
local __io_output_format_fields__i
for ((__io_output_format_fields__i = 3; __io_output_format_fields__i <= __io_output_format_fields__argument_count; __io_output_format_fields__i++)); do
eval "local __io_output_format_fields__field$__io_output_format_fields__i=(\"\${!$__io_output_format_fields__i}\")"
done
# Determine the amount of records/lines to print.
# Notice at least the first array must be passed.
local __io_output_format_fields__record_count=${#__io_output_format_fields__field3[@]}
for ((__io_output_format_fields__i = 0; __io_output_format_fields__i < __io_output_format_fields__record_count; __io_output_format_fields__i++)); do
local __io_output_format_fields__values="\"\${__io_output_format_fields__field"$(
seq -s "[$__io_output_format_fields__i]}\" \"\${__io_output_format_fields__field" 3 $__io_output_format_fields__argument_count
)"[$__io_output_format_fields__i]}\""
eval "printf \"$2\" $__io_output_format_fields__values > $1"
done
}
io_query_format_fields() {
# Assure we've got required parameters.
if [ ${#@} -lt 2 ]; then
return 1
fi
local __io_query_format_fields__argument_count=${#@}
local __io_query_format_fields__query="$1"
local __io_query_format_fields__format="$2"
# Load locally by indirect expansion, ${! ... },
# and mangle the variable number argument arrays.
local __io_query_format_fields__i
for ((__io_query_format_fields__i = 3; __io_query_format_fields__i <= __io_query_format_fields__argument_count; __io_query_format_fields__i++)); do
eval "local __io_query_format_fields__f$__io_query_format_fields__i=(\"\${!$__io_query_format_fields__i}\")"
done
local __io_query_format_fields__record_count=${#__io_query_format_fields__f3[@]}
local __io_query_format_fields__indexes=($(seq $__io_query_format_fields__record_count))
if [ ! -z "$1" ]; then
if [ "$(type -t $(echo -e "$IOUtilsHeader" | grep -vE '\s'))" = "function" ]; then $IOUtilsHeader
else echo -e "$IOUtilsHeader"; fi
echo -e "$__io_query_format_fields__query"
echo
fi
io_output_format_fields /dev/stdout "$__io_query_format_fields__format" __io_query_format_fields__indexes[@] ${@:3}
echo
io_input_choice __io_query_format_fields__indexes[@]
IOQueryFormatFields=()
for ((__io_query_format_fields__i = 3; __io_query_format_fields__i <= __io_query_format_fields__argument_count; __io_query_format_fields__i++)); do
eval "IOQueryFormatFields[${#IOQueryFormatFields[@]}]=\${__io_query_format_fields__f$__io_query_format_fields__i[IOInputChoice - 1]}"
done
}
io_query_choice() {
# Assure we've got required parameters.
if [ ${#@} -lt 2 ]; then
return 1
fi
__io_query_choice__query=$([ -z "$1" ] && echo -n "" || echo -ne "$FLUXIONVLine $1\n")
io_query_format_fields "$__io_query_choice__query" "\t$CRed[$CSYel%d$CClr$CRed]$CClr %b\n" $2
IOQueryChoice="${IOQueryFormatFields[0]}"
}
io_query_file() {
if [ ${#@} -lt 2 ]; then
return 1
fi
local __io_query_file__options
# List a line per line and redirect output.
# readarray __io_query_file__options < $2
mapfile __io_query_file__options <$2
# Strip newline characters from array elements
__io_query_file__options=("${__io_query_file__options[@]/$'\n'/}")
io_query_choice "$1" __io_query_file__options[@]
IOQueryFile=$IOQueryChoice
}
# FLUXSCRIPT END