-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.sh
executable file
·51 lines (40 loc) · 1.18 KB
/
select.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
#!/bin/bash
# shellcheck source=functions.sh
source "$(dirname "$0")/functions.sh"
if [ "$#" -ne 3 ]; then
eval "${param_problem}"
fi
database=$1
table=$2
columns=$3
if [ ! -d "$database" ]; then
eval "${db_doesnt_exist}"
fi
table_path="${database}/${table}"
if [ ! -f "${table_path}" ]; then
eval "${table_doesnt_exist}"
fi
schema=$(head -n1 "${table_path}")
schema_field_count=$(eval count_fields "${schema}")
IFS=","
for entry in ${columns}; do
# -eq fails in not integer.
[ "${entry}" -eq "${entry}" ] 2> /dev/null || eval "${column_doesnt_exist}"
# Numbers must be between 1 and the number of fields.
[ "${entry}" -lt "1" ] && eval "${column_doesnt_exist}"
[ "${entry}" -gt "${schema_field_count}" ] && eval "${column_doesnt_exist}"
done
echo "start_results"
read_results() {
while read -r line; do
for column in ${columns}; do
# Print this column from line, replace newline with ','
cut -d',' -f"${column}" <<< "$line" | tr $'\n' ','
done
# Delete last ','
echo -e '\b '
# Read in table, from second line to ignore header.
done < <(tail -n +2 "${table_path}")
}
# read_results
attempt_work "$table_path" read_results "echo end_results; exit 0"