-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshell_connector.test
executable file
·114 lines (92 loc) · 3.68 KB
/
shell_connector.test
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
#!/bin/bash
SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
. $SRCDIR/shell_connector.bash
SQLITE=/usr/bin/sqlite3
###
### Demo-sample: define/open 3 connectors to `bc`, `date` and `sqlite`,
### for interactive use from main bash script.
###
# Test if sqlite is present
if [ ! -x $SQLITE ];then
echo >&2 "Tool '$SQLITE' not found (please correct SQLITE var if installed)."
exit 1
fi
# 1. instanciate a long-running bc command which we will then be
# able to interact with with myBc().
# Initialize them by declaring `mil` function, to compute
# human readable, returning `A,XX.YYY` where A mean power of 1K
# and XX.YYY is value divided by 1024**A.
newConnector /usr/bin/bc "-l" 'mil(0)' '0,0' - <<-"EOF"
define void mil (s) {
if (s==0) { print "0,0\n"; return;};
p=l(s)/l(1024);
scale=0;
p=p/1;
scale=20;
print p,",",s/1024^p,"\n";
}
EOF
# 2. instanciate a long-running date command which we will then be able
# to interact with with myDate(), convert date input to UnixTimeStamp.
newConnector /bin/date '-f - +%s' @0 0
# 3. instanciate a long-running sqlite command with file based on /dev/shm
# Interact with mySqlReq
newSqlConnector $SQLITE $'-separator \t -header /dev/shm/test.sqlite'
# Database initialisation...
cat >&$SQLIN <<-EOSQLInit
DROP TABLE IF EXISTS files;
CREATE TABLE files (perms, user, date UNSIGNED BIGINT,
size UNSIGNED BIGINT,name);
EOSQLInit
declare -a ABR=(K M G T P)
# First: A simple demo using backgrounded `bc -l`
{
read headline
echo "dtot=0;duse=0;" >&$BCIN
while read filesystem type size used free prct mpoint;do
echo >&$BCIN "dtot+=$used+$free;duse+=$used;"
myBc "100*$used/($used+$free)" mpct # Compute % of use
myBc "mil($used)" Used # Human readable form
myBc "mil($used+$free)" Total
printf "%-26s %-8s %7.2f%s %7.2f%s %9s %7.2f%%\n" "$mpoint" "$type" \
${Used#*,} ${ABR[${Used%,*}]} ${Total#*,} ${ABR[${Total%,*}]} \
$prct $mpct
done
} < <(LANG=C df -kT)
myBc "mil(dtot)" Total
myBc "mil(duse)" Used
printf "%-36s%7.2f%s %7.2f%s\n" Total \
${Used#*,} ${ABR[${Used%,*}]} ${Total#*,} ${ABR[${Total%,*}]}
# Prepare to work agains files
ABR=('' ${ABR[@]}) # lowest is byte, not kb.
echo "ftot=0;" >&$BCIN # file size total = 0, but as BC variable
# Let play with SQL
{
read headline
while read perm blk user group size month day yot name;do
echo >&$BCIN "ftot+=$size" # Increment BC variable ftot.
myDate "$month $day $yot" Date # Convert `ls` date format to EPOCH
printf >&$SQLIN "INSERT INTO files values('%s','%s',%s,%s,'%s');" \
"$perm" "$user" $Date $size "$name" # DB INSERT
done
} < <(LANG=C /bin/ls --full-time -Al)
mySqlReq myarray "SELECT * from files order by date;" # DB SELECT files by date
printf "%-12s %-12s %16s %10s %s\n" "${myarray_h[@]}" # print header
for line in "${myarray[@]}";do
IFS=$'\t' read perm user date size name <<<"$line"
myBc "mil($size)" Size
printf " %-11s %-12s %(%F %H:%M)T %7.2f%-2s %s\n" $perm $user $date \
${Size#*,} ${ABR[${Size%,*}]}b "$name"
done
myBc "mil(ftot)" Total # Total computed by bc
mySqlReq stot 'SELECT SUM(size) FROM files;' # DB SELECT SUM of file size
myBc "mil($stot)" STotal # Total computed by DB
printf "%18sby bc:%7.2f%-2s, by sql:%7.2f%-2s %s\n" '' \
${Total#*,} ${ABR[${Total%,*}]}b ${STotal#*,} ${ABR[${STotal%,*}]}b Total
myBc ftot bctot # bc variable ftot to bash var bctot (hope BC and SQL match!)
[ "$bctot" = "$stot" ] || echo "WARN: BC:'$bctot' and SQL:'$stot' don't match!"
myDate now now # Date from backgrounded date task
printf "%-14s: %(%a %d %b %T)T\n" \
"Last SQL read" $lastsqlread "now by date" $now "now by bash" -1
mySid=$(ps ho sid $$)
ps --sid $mySid fw