diff options
author | InigoGutierrez <taamas@taamas.xyz> | 2022-07-20 12:32:01 +0000 |
---|---|---|
committer | InigoGutierrez <taamas@taamas.xyz> | 2022-07-20 12:32:01 +0000 |
commit | 84a27deb717e57d066fd256147862c96736fbb7c (patch) | |
tree | e92c76f7def52b033e32952c19d4ca71dd663691 /data/storeSelect.sh | |
parent | 4fb56fea26d27a978eb62d7303631186317852e2 (diff) | |
download | scripts-84a27deb717e57d066fd256147862c96736fbb7c.tar.gz scripts-84a27deb717e57d066fd256147862c96736fbb7c.zip |
Created storeSelect.sh to interactively store data based on a tables description file.
Diffstat (limited to 'data/storeSelect.sh')
-rwxr-xr-x | data/storeSelect.sh | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/data/storeSelect.sh b/data/storeSelect.sh new file mode 100755 index 0000000..ae57447 --- /dev/null +++ b/data/storeSelect.sh @@ -0,0 +1,57 @@ +#!/bin/sh +# +# Apunta datos interactivamente. +# +# Estructura del archivo con información sobre cada tabla y primera línea de este archivo: +# # Name # File # AddDate # AddTime # Separator # Prompts... +# El primer caracter de esta primera línea se toma como el caracter de separación entre campos. +# AddDate y AddTime son verdaderos cuando su valor es 'y' y falsos en cualquier otro caso. +# Los _ en los prompts se sustituyen por espacios + +storesDataFile="$HOME/data/stores.dat" +[ ! -f "$storesDataFile" ] && + printf "Error: store data file \"${storesDataFile}\" not found.\n" >&2 && + exit 1 + +dataFileSeparator="$(sed '1q' "$storesDataFile" | grep -Eo '^.')" + +blankLineMarker='@BLANKLINEMARKER@' + +gatherData () { + + shift 1 # table name + + file="$1" + shift 1 + + addDate='' + [ "$1" = 'y' ] && addDate='-d' + shift 1 + + addTime='' + [ "$1" = 'y' ] && addTime='-t' + shift 1 + + separator="$1" + [ "$1" = "$blankLineMarker" ] && separator="' '" + shift 1 + + data='' + for prompt in "$@"; do + fixedPrompt=$(echo "$prompt" | tr '_' ' ') + printf "${fixedPrompt}: " >&2 + read newData + [ -z "$newData" ] && exit 1 + data="${data} ${newData}" + done + store.sh -f "'${file}'" "$addDate" "$addTime" -s "$separator" $data +} + +table="$(grep -Eo "^[^${dataFileSeparator}]+" "$storesDataFile" | fzf)" +[ -z "$table" ] && exit 0 + +tableLine="$(grep "^${table}${dataFileSeparator}" "$storesDataFile")" +tableData=$(echo $tableLine | + tr "$dataFileSeparator" '\n' | + sed "s/^\s*$/${blankLineMarker}/") +gatherData $tableData |