oiooiooioiioooiioiioiooo
Goto Top

Dynamisches Menü mit Dialog für Linux Bash

Guten Tag,

in einem Unix Forum habe ich eini interessante Lösung für dynamisches Menü gefunden:

Dynamic Bash Dialog from directory listing

https://www.unix.com/shell-programming-and-scripting/279464-dynamic-bash ...

Allerdings werden hier Informationen aus einem Verzeichnis verwendet.

Ich benötige das gleich, nur soll der Script die Daten aus eine Datei entnehmen. Um genau zu sagen Spalte 2 soll als erstes Argument werden und Spalte 6 und 7, mit einem Leerzeichen getrennt, das zweite Argument.

#! /bin/bash

source ./CONFIG/config.source

#usage: Dynamic_Menu.bash /home/user/target_directory
#------
declare -a array

i=1 #Index counter for adding to array
j=1 #Option menu value generator

while read line
do		
#Dynamic dialogs require an array that has a staggered structure
#array[1]=1
#array[2]=First_Menu_Option
#array[3]=2
#array[4]=Second_Menu_Option

#	array[ $i ]=$j
#	(( j++ ))
#array=($(tail -n+1 $LISTE | cut -d " " -f2)) 
#(( j++ ))
	array[ ($i + 1) ]=$line
	(( i=($i+2) ))


done < <(tail -n+2 "$LISTE" | cut -d " " -f2 ; tail -n+2 "$LISTE" | cut -d " " -f6,7) #consume file path provided as argument  
#----------
##uncomment for debug
printf '%s\n' "${array[@]}"  
read -rsp "Press any key to continue..." -n1 key  

#Build the menu with dynamic content
TERMINAL=$(tty) #Gather current terminal session for appropriate redirection
HEIGHT=20
WIDTH=76
CHOICE_HEIGHT=16
BACKTITLE="Back_Title"  
TITLE="Dynamic Dialog"  
MENU="Choose a file:"  

CHOICE=$(dialog --clear \
                --backtitle "$BACKTITLE" \  
                --title "$TITLE" \  
                --menu "$MENU" \  
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${array[@]}" \  
                2>&1 >$TERMINAL)

Egal wie ich es nur ansetze, bekomme ich die Daten nicht richtig angeordnet. Somit bin ich für jede Hilfe wie immer sehr dankbar.

Viele Grüße

Ich

Content-Key: 541657

Url: https://administrator.de/contentid/541657

Printed on: April 20, 2024 at 02:04 o'clock

Member: bloodstix
Solution bloodstix Jan 31, 2020 updated at 10:18:40 (UTC)
Goto Top
Hallo,

werde aus deinem Skript nicht so recht schlau, liegt vermutlich an der Uhrzeit.
Aber durch ne Datei loopen und entsprechende Spalten in Variablen zu packen geht so z.B.:
while read line; do
    SPALTE2="`echo $line | awk '{ print $2 }'`"  
    SPALTE6_7="`echo $line | awk '{ print $6" "$7 }'`"  
    #mach was auch immer mit den Variablen
done < $input_file_path

Grüße
bloody
Member: OIOOIOOIOIIOOOIIOIIOIOOO
OIOOIOOIOIIOOOIIOIIOIOOO Jan 31, 2020 at 11:03:58 (UTC)
Goto Top
Guten Tag @bloodstix,

werde aus deinem Skript nicht so recht schlau, liegt vermutlich an der Uhrzeit.
Aber durch ne Datei loopen und entsprechende Spalten in Variablen zu packen geht so z.B.:

Der Script soll Daten aus einer Datei Sammeln und diese dann in Form einer Menü darstellen.

Am ende möchte dann soetwas bei raus kommen.

dialog --clear --backtitle "BACKTITLE" --title "TITLE"  --menu "MENU"  20 50 16  "ID1" "bla2 blub" "ID2" "da sda" 2>&1 >$(tty)  

while read line; do
>     SPALTE2="`echo $line | awk '{ print $2 }'`"  
>     SPALTE6_7="`echo $line | awk '{ print $6" "$7 }'`"  
>     #mach was auch immer mit den Variablen
> done < $input_file_path

Mit der Variante könnte man mit Sicherheit etwas bauen, dass die gesuchten Daten zuerst in eine Datei eingespielt werden und dann wiederum mit Hilfe von sed die gewünschte Informationen abrufen. Könnte man mit arbeiten. Aber gebe es da nicht eine Lösung, dass der Script nichts auf der Platte schreiben muss?

Viele Grüeß

@OIOOIOOIOIIOOOIIOIIOIOOO
Member: OIOOIOOIOIIOOOIIOIIOIOOO
OIOOIOOIOIIOOOIIOIIOIOOO Feb 01, 2020, updated at Feb 02, 2020 at 06:25:05 (UTC)
Goto Top
Guten Tag @bloodstix,

nun noch einen Nacht drüber geschlafen und noch einmal ran gesetzt. Und nun funktioniert es wie gewünscht:

#! /bin/bash

source ./CONFIG/config.source

#usage: Dynamic_Menu.bash /home/user/target_directory
#------
declare -a array

i=1 #Index counter for adding to array
j=1 #Option menu value generator

while read line; do
    SPALTE2="`echo $line | awk '{ print $2 }'`"  
    SPALTE6_7="`echo $line | awk '{ print $6" "$7 }'`"  
    array[ $i ]=$SPALTE2
    (( j++ ))
    array[ ($i + 1) ]=$SPALTE6_7
    (( i=($i+2) ))
done < $LISTE

#----------
##uncomment for debug
#printf '%s\n' "${array[@]:3}" 
#read -rsp "Press any key to continue..." -n1 key 

#Build the menu with dynamic content
TERMINAL=$(tty) #Gather current terminal session for appropriate redirection
HEIGHT=20
WIDTH=76
CHOICE_HEIGHT=16
BACKTITLE="Back_Title"  
TITLE="Dynamic Dialog"  
MENU="Choose a file:"  

CHOICE=$(dialog --clear \
                --backtitle "$BACKTITLE" \  
                --title "$TITLE" \  
                --menu "$MENU" \  
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${array[@]:3}" \ #Gibt die Ausgabe ab dem dritten Wert aus (KOMMENTAR muss entfernt werden)  
                 2>&1 >$TERMINAL)

Vielen Dank für deine Hilfe!

Viele Grüße

@OIOOIOOIOIIOOOIIOIIOIOOO
Member: bloodstix
bloodstix Feb 03, 2020 at 13:11:55 (UTC)
Goto Top
Glückwunsch @OIOOIOOIOIIOOOIIOIIOIOOO, freut mich das ich helfen konnte.
Manchmal hilft es ein wenig Abstand zu bekommen und das erstmal sacken zu lassen :D