변수 열 정렬

변수 열 정렬

동적 메뉴가 준비되었습니다. 마지막 부분은 열을 정렬하는 것입니다. 대부분의 경우 이것이 작동했지만(이유는 확실하지 않음) 마지막 부분이 올바르게 정렬되지 않았습니다. 스크립트를 추출하고 테스트 데이터를 여기에 넣어 직접 재현했습니다.

  1. 열 끝이 정렬되지 않았습니다.
  2. 제목이 정렬되지 않았습니다.

도와 주셔서 감사합니다!

#!/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

관련 정보