aboutsummaryrefslogtreecommitdiff
path: root/data/store.sh
blob: 67d8ba98fdf9f9001f6c3a34a2e3043008b1c107 (plain)
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
#!/bin/sh
#
# Apunta datos en el archivo correspondiente de un servidor.

usageMessage='Usage: store.sh [-dt] [-s SEPARATOR] [-S SERVER] -f FILE VALUE...\n'

separator='#'
server='taamas@taamas.xyz'
file=''
date=''
time=''

while echo "$1" | grep -q '^-'; do
	case "$1" in

		"-d")
			date='true'
			shift 1
			;;

		"-f")
			[ -z "$2" ] && echo '-f option needs an argument.' >&2 && exit 1
			file="$2"
			shift 2
			;;

		"-s")
			[ -z "$2" ] && echo '-s option needs an argument.' >&2 && exit 1
			separator="$2"
			shift 2
			;;

		"-S")
			[ -z "$2" ] && echo '-S option needs an argument.' >&2 && exit 1
			server="$2"
			shift 2
			;;

		"-t")
			time='true'
			shift 1
			;;

	esac
done

[ -z "$file" ] && printf "A remote file name must be provided with the -f flag.\n" >&2 && exit 2

values="$@"
[ -z "$values" ] && printf "$usageMessage" >&2 && exit 2
formattedValues=''
for value in "$@"; do
	formattedValues="${formattedValues}${separator}${value}"
done
formattedValues="$(echo "$formattedValues" | sed "s/^${separator}//")"
[ -n "$time" ] && formattedValues="$(date '+%H:%M:%S')${separator}${formattedValues}"
[ -n "$date" ] && formattedValues="$(date '+%y/%m/%d')${separator}${formattedValues}"

ssh "$server" "echo ${formattedValues} >>${file}" >/dev/null || exit 3