#!/bin/bash ## Small sample of using bash loadable modules. ## loading ``csv'' as builtin, little demo using RFC4180 compliant CSV. ## (C) 2021 Felix Hauri - felix@f-hauri.ch # Licensed by GNU GENERAL PUBLIC LICENSE Version 3 # https://www.gnu.org/licenses/gpl-3.0.txt # Require loadable bash builtins! # Under Debian based: ...$ su-to-root apt install bash-builtins enable -f /usr/lib/bash/csv csv exec {FD}<<-EndofCSV Id,Name,Full description,Value 1234,Cpt1023,"Energy counter",34213 2343,Sns2123,"Temperatur sensor to trigg for alarm",48.4 42,Eye1412,"Solar sensor, ""Day/ Night""",12199.21 EndofCSV declare -a headline=() row=() ## Prevent SC2154: referenced but not assigned. declare -i max=0 rows ## Using integer for quick operations # Because headline define width and number of rows, newlines are forbidden! read -ru $FD line csv -a headline "$line" for val in "${headline[@]}";do max=" ${#val} > max ? ${#val} : max " done max+=1 rows=${#headline[@]} printf -v fieldfmt "%-${max}s: \\e[47m%%q\\e[0m\\\\n" "${headline[@]}" while read -ru $FD line;do while csv -a row "$line" ; (( ${#row[@]} < rows )) ;do read -ru $FD subline || break line+=$'\n'"$subline" done # shellcheck disable=SC2059 # Don't use variables in the printf... printf "${fieldfmt}\\n" "${row[@]}" done exec {FD}<&-