我的动态菜单已准备就绪。最后一部分是使列对齐。对于很大一部分来说,这已经可以工作了(不知道为什么),但是最后一部分没有正确对齐我已经提取了我的脚本并将测试数据放入其中以直接重现它
- 列末尾未对齐
- 标题未对齐
感谢帮助!
#!/bin/bash
#
listpcs=( kast screen test )
dockersets=( apps1 apps2 infra1 infra2 )
stacklist=( stack1 stack2 stack3 stack4 )
stack1=( domoticz dashboard dashticz nodered x y z ttttttttttttttt)
stack2=( mosquitto mysql nginx registry apache phpmyadmin )
stack3=( nextpvr motioneye joomla squeezebox q w r qqqqqqqqqqqqq)
stack4=( portainer unify wireguard watchtower w e t wwwwwwwwww qqqqqqqqq)
mypc=kast
mymnu=installatie
dockerset=kast
composefile=/home/pi/iotmenu/cfg/docker-compose_kast.yml
envfile=/home/pi/iotmenu/cfg/hub_kast.env
msgcolor=`echo "\033[01;31m"` # bold red IS DE OUDE gebruikte moet TZT WEG
msgcolor_red=`echo "\033[01;31m"` # bold red
msgcolor_green=`echo "\033[01;32m"` # bold green
msgcolor_blue=`echo "\033[01;36m"` # bold blue
normal=`echo "\033[00;00m"` # normal white
menu=`echo "\033[36m"` # Blue
number=`echo "\033[33m"` # yellow
bgred=`echo "\033[41m"`
fgred=`echo "\033[31m"`
t_sp0='%-3s'
t_sp1='%-16s'
t_sp2='%32s'
t_sp3='%4s'
t_sp4='%12s'
t_sp5='%-2s'
#genereer titel
for j in "${stacklist[@]}"
do
printf $t_sp0 && printf "${menu}------- RPI-$j ------" && printf $t_sp3 && printf "${normal}"
done
printf "${normal}\n\n"
#genereer docker items
maxkolom=${#stacklist[@]}
maxrijen=9
declare -A t_mitem
declare -A h_mitem
rij=1
t_stack=0
shopt -s extglob #nodig voor t_waarde om | te gebruiken in een string
t_waarde="@(" #begin waarde , dit moet t worden t_waarde='@(1|2|3|4 xxxxxxx)
t_waarde2="@(" #begin waarde , dit moet t worden t_waarde='@(10|20|30|40 xxxxxxx)
for ((rij;rij<=maxrijen;rij++)) # eerste lus is aantal rijen van 9
do
kolom=1
t_stack=0
for ((kolom;kolom<=maxkolom;kolom++)) # tweede lus is kolommen, en pak de eerste kolom
do
declare -n stack=${stacklist[$kolom-1]}
if (( $(bc <<<"$rij == 1 && $kolom > 1") )); then # check of er 10 per kolom bij moeten als t de eerste rij is en kolom =2 of hoger
t_hlp=`expr $kolom - 1` && t_mul=`expr $t_hlp \* 10`
t_mitem[$kolom]=$((t_mitem[$kolom]+$t_mul+1))
elif (( $(bc <<<"$rij == 1 && $kolom == 1") )); then
t_mitem[$kolom]=$((t_mitem[$kolom]+1))
fi
if [[ -z "${stack[$rij-1]}" ]]; then
printf $t_sp2
else #item nog een vaste lemgte geven en uitvullen zodat kolom altijd even lang is
printf $t_sp0 && printf "${number}$((t_mitem[$kolom])))${normal}" && printf " Toevoegen " && printf $t_sp1 "${stack[$rij-1]}" && printf "${normal}" #print het item op de juiste plek
sx=${t_mitem[$kolom]} && ws=${stack[$rij-1]} && h_mitem[${sx}]=${ws} && t_waarde="${t_waarde}|$((t_mitem[$kolom]))"
fi
t_mitem[$kolom]=$((t_mitem[$kolom]+1))
done
printf "${normal}\n"
done
答案1
我从头开始,而不是试图理解其中的复杂性。
#!/bin/bash
stacklist=( stack1 stack2 stack3 stack4 )
stack1=( domoticz dashboard dashticz nodered x y z ttttttttttttttt)
stack2=( mosquitto mysql nginx registry apache phpmyadmin )
stack3=( nextpvr motioneye joomla squeezebox q w r qqqqqqqqqqqqq)
stack4=( portainer unify wireguard watchtower w e t wwwwwwwwww qqqqqqqqq)
normal=`echo "\033[00;00m"` # normal white
menu=`echo "\033[36m"` # blue
number=`echo "\033[33m"` # yellow
prefix=' '
for h in "${stacklist[@]}" ; do
printf "$menu%s$normal" "$prefix------- RPI-$h ------ "
done
printf '\n\n'
for i in $(seq 0 ${#stack1[@]}) ; do
printf "$prefix"
for s in {1..4} ; do
value=stack$s[$i]
if [[ ${!value} ]] ; then
printf "$number%2d) $normal"'Toevoegen %-19s' $((i+s*10-9)) "${!value}"
else
printf '%33s' ""
fi
done
echo
done