Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[bash] Verständnisfrage zu select und read
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sat Apr 07, 2018 6:05 pm    Post subject: [bash] Verständnisfrage zu select und read Reply with quote

Hallo Zusammen,

ich habe hier ein kleines Problem mit einem BASH Script.

das kleine Script sieht so aus:

Code:
#!/bin/bash

PS3="Ihre Wahl : "
select auswahl in "Punkt 1" "Punkt 2" "Punkt 3" "Punkt 4" Ende
do
   case "$auswahl" in
      Ende)  echo "Ende"; break ;;
        "")  echo "Ungültige Auswahl" ;;
         *)  echo "Sie haben $auswahl gewählt"
   esac
done


Was auch funktioniert:

Code:
server01 test # ./test.sh
1) Punkt 1
2) Punkt 2
3) Punkt 3
4) Punkt 4
5) Ende
Ihre Wahl :


Wenn ich nun aber die "Punkte" aus einer Datei laden will, dann geht das nicht:

Code:
#!/bin/bash

PUNKT=$(while read line; do echo -en $line" "; done < punkt.txt)

PS3="Ihre Wahl : "
select auswahl in "$PUNKT" Ende
do
   case "$auswahl" in
      Ende)  echo "Ende"; break ;;
        "")  echo "Ungültige Auswahl" ;;
         *)  echo "Sie haben $auswahl gewählt"
   esac
done


punkt.txt:
Code:
server01 test # cat punkt.txt
Punkt 1
Punkt 2
Punkt 3
Punkt 4
server01 test #


Das ist dann die Ausgabe:

Code:
server01 test # ./test2.sh
1) Punkt 1 Punkt 2 Punkt 3 Punkt 4
2) Ende
Ihre Wahl :


Wenn ich die Leerzeichen weglasse, dann geht es.

Da ich aber die Leerzeichen benötige, würde es mich schon interessieren, wie ich das gebacken bekomme?
Back to top
View user's profile Send private message
reini123
n00b
n00b


Joined: 07 Apr 2018
Posts: 7

PostPosted: Sat Apr 07, 2018 10:38 pm    Post subject: Reply with quote

die Variable nicht geschützt durch " übergeben sollte Dir helfen.
Code:
select auswahl in $PUNKT Ende

also $PUNKTE ohne " und somit nicht als String.
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sat Apr 07, 2018 11:57 pm    Post subject: Reply with quote

Dann sieht das Ergebnis so aus:

Code:
server01 test # ./test2.sh
1) Punkt  3) Punkt  5) Punkt  7) Punkt  9) Ende
2) 1      4) 2      6) 3      8) 4
Ihre Wahl :
Back to top
View user's profile Send private message
reini123
n00b
n00b


Joined: 07 Apr 2018
Posts: 7

PostPosted: Sun Apr 08, 2018 4:32 pm    Post subject: Reply with quote

Ich vermute mal es stoert Dich die Standardausgabe neben einander^^
entwerde script per ". ./test2.sh" anstatt "./test2.sh" aufrufen oder COLUMNS definieren.
Code:
#!/bin/bash
PUNKT=$(while read line; do echo -en "$line "; done < punkt.txt)
WORD=`echo "$PUNKT"|wc -w` #Anzahl der Auswahlkriterien in Var WORD definieren.
COLUMNS="$WORD" #COLUMNS auf die jeweilige Anzahl setzen
PS3="Ihre Wahl : "

Ich hoffe das hilft Dir :D sonst wüsste ich auch nicht weiter 8O
Mƒg
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Sun Apr 08, 2018 6:32 pm    Post subject: Reply with quote

@reini123,

kann es sein, dass Du mein o.g. Problem nicht richtig verstanden hast?

Es geht darum, die Menü Items mit Leerzeichen darzustellen, wobei ich die Items aus ein er Datei auslesen will.
Back to top
View user's profile Send private message
reini123
n00b
n00b


Joined: 07 Apr 2018
Posts: 7

PostPosted: Mon Apr 09, 2018 12:13 am    Post subject: Reply with quote

Hmm ...sry,
ja hab es falsch verstanden.
ausser
Code:
PUNKT=$(while read line; do echo -en $line" "|sed 's/ /_/g'; done < punkt.txt)

fällt mir im Moment nichts ein.
Aber ein interessantes Problem das sich scheinbar auch nicht über array lösen läst 8O
Bin schon gespannt ob irgendwer sonst die Lösung weis :lol:
[/code]
Back to top
View user's profile Send private message
Yamakuzure
Advocate
Advocate


Joined: 21 Jun 2006
Posts: 2284
Location: Adendorf, Germany

PostPosted: Mon Apr 09, 2018 7:27 am    Post subject: Reply with quote

Die Lösung ist 'read' auf ein Array zu benutzen:

Code:
 ~/tmp $ cat test3.sh
#!/bin/bash

IFS=$(echo -en "\t") read -ra PUNKT <<<"$(cat punkt.txt | tr '\n' '\t')"
# Zum Anschauen:
# declare -p PUNKT
# echo

PS3="Ihre Wahl : "
select auswahl in "${PUNKT[@]}" Ende
do
   case "$auswahl" in
      Ende)  echo "Ende"; break ;;
        "")  echo "Ungültige Auswahl" ;;
         *)  echo "Sie haben $auswahl gewählt"
   esac
done

 ~/tmp $ ./test3.sh
1) Punkt 1
2) Punkt 2
3) Punkt 3
4) Punkt 4
5) Ende
Ihre Wahl : 5
Ende

Erklärung:
  • IFS=$(echo -en "\t")
    Für 'read' werden Wörter hiermit ausschließlich per Tabulator getrennt.
  • read -ra PUNKT
    Lese den folgenden Input in ein Array (-a) und ignoriere Backslashes (-r). Mit Letzterem bist du in "punkt.txt" etwas flexibler.
  • <<<"$(cat punkt.txt | tr '\n' '\t')"
    Input (<<<) aus deiner "punkt.txt", 'tr' ersetzt alle Zeilenumbrüche durch Tabulator.

_________________
Important German:
  1. "Aha" - German reaction to pretend that you are really interested while giving no f*ck.
  2. "Tja" - German reaction to the apocalypse, nuclear war, an alien invasion or no bread in the house.
Back to top
View user's profile Send private message
reini123
n00b
n00b


Joined: 07 Apr 2018
Posts: 7

PostPosted: Mon Apr 09, 2018 8:37 am    Post subject: Reply with quote

Danke :D
Code:
#!/bin/bash
IFS=$'\n'
PUNKT=`while read line; do echo "$line"; done < punkt.txt`
COLUMNS=0
PS3="Ihre Wahl : "
select auswahl in $PUNKT Ende
do
   case "$auswahl" in
      Ende)  echo "Ende"; break ;;
        "")  echo "Ungültige Auswahl" ;;
         *)  echo "Sie haben $auswahl gewählt"
   esac
done

bin auch gerade auf IFS gekommen :D
Halbe Nacht ist mir das nicht aus den Kopf gegangen ... da war doch was :roll:
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Mon Apr 09, 2018 4:22 pm    Post subject: Reply with quote

1000 THX @ Yamakuzure,

funktioniert hervorragend! :wink: :D

Ich habe es mal etwas erweitert, dass ich aus der Schleife komme,

Code:
#!/bin/bash

IFS=$(echo -en "\t") read -ra PUNKT <<<"$(cat punkt.txt | tr '\n' '\t')"
# Zum Anschauen:
# declare -p PUNKT
# echo

PS3="Ihre Wahl : "
select auswahl in "${PUNKT[@]}" Ende
do
   case "$auswahl" in
      Ende)
        echo "Ende"
        break
        ;;

       "") 
        echo "Ungültige Auswahl"
        ;;

        *)
        echo "Sie haben $auswahl gewählt"
        break
        ;;

   esac
done



Die Frage ist nun, ob es eine Möglichkeit gibt, die gemachte Eingabe abzufragen?

Ich meine, wenn ich z.B, "2" eingebe, wie kann ich das ausgeben?

"$auswahl" gibt ja den Text aus, ich hätte aber gerne die Ziffern, geht das überhaupt?
Back to top
View user's profile Send private message
reini123
n00b
n00b


Joined: 07 Apr 2018
Posts: 7

PostPosted: Mon Apr 09, 2018 6:39 pm    Post subject: Reply with quote

Wenn ich Dich nicht wieder falsch verstehe brauchst du REPLY ( The default variable for the read builtin. )
Code:
 echo "Sie haben "$REPLY" gewählt"

Ich hoffe Du meinst das? :D
Back to top
View user's profile Send private message
Yamakuzure
Advocate
Advocate


Joined: 21 Jun 2006
Posts: 2284
Location: Adendorf, Germany

PostPosted: Tue Apr 10, 2018 7:25 am    Post subject: Reply with quote

reini123 wrote:
Wenn ich Dich nicht wieder falsch verstehe brauchst du REPLY ( The default variable for the read builtin. )
Code:
 echo "Sie haben "$REPLY" gewählt"

Ich hoffe Du meinst das? :D
Kurzer Test:
Code:
 ~/tmp $ cat test3.sh
#!/bin/bash

IFS=$(echo -en "\t") read -ra PUNKT <<<"$(cat punkt.txt | tr '\n' '\t')"

PS3="Ihre Wahl : "
select auswahl in "${PUNKT[@]}" Ende
do
    case "$auswahl" in
        Ende)
            echo "Ende"
            break
            ;;
        "")
            echo "Ungültige Auswahl"
            ;;
        *)
            echo "Sie haben $REPLY ($auswahl) gewählt"
            break
            ;;
    esac
done
 ~/tmp $ ./test3.sh
1) Punkt 1
2) Punkt 2
3) Punkt 3
4) Punkt 4
5) Ende
Ihre Wahl : 3
Sie haben 3 (Punkt 3 ) gewählt
Funzt! :-)

Dass nach dem "Punkt 3" in der Antwort noch ein Leerzeichen auftaucht müsste man noch beheben, aber ansonsten läuft das.
_________________
Important German:
  1. "Aha" - German reaction to pretend that you are really interested while giving no f*ck.
  2. "Tja" - German reaction to the apocalypse, nuclear war, an alien invasion or no bread in the house.
Back to top
View user's profile Send private message
reini123
n00b
n00b


Joined: 07 Apr 2018
Posts: 7

PostPosted: Tue Apr 10, 2018 10:00 am    Post subject: Reply with quote

Die Leerzeichen einfach wegschneiden am Zeilenende, also wenn die sonst nicht gebraucht werden :?:
Code:
IFS=$(echo -en "\t") read -ra PUNKT <<<"$(cat punkt.txt |sed 's/ *$//'| tr '\n' '\t')"

ZB mit sed :D
Hoffe das funzt 8)
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Tue Apr 10, 2018 4:13 pm    Post subject: Reply with quote

Nun, das ist nicht, was ich gemeint habe.

Ich möchte wissen, welche Taste gedrückt wurde.
Back to top
View user's profile Send private message
Yamakuzure
Advocate
Advocate


Joined: 21 Jun 2006
Posts: 2284
Location: Adendorf, Germany

PostPosted: Wed Apr 11, 2018 6:27 am    Post subject: Reply with quote

3PO wrote:
Nun, das ist nicht, was ich gemeint habe.

Ich möchte wissen, welche Taste gedrückt wurde.
Was fehlt bei
Code:
Ihre Wahl : 3
Sie haben 3 (Punkt 3 ) gewählt
denn? Da steht doch, welche Taste gedrückt wurde...
_________________
Important German:
  1. "Aha" - German reaction to pretend that you are really interested while giving no f*ck.
  2. "Tja" - German reaction to the apocalypse, nuclear war, an alien invasion or no bread in the house.
Back to top
View user's profile Send private message
reini123
n00b
n00b


Joined: 07 Apr 2018
Posts: 7

PostPosted: Wed Apr 11, 2018 10:04 am    Post subject: Reply with quote

Vielleicht gehts mit
Code:
echo -e "Sie haben Taste \033[33m-->\033[0m\033[41m$REPLY\033[0m\033[33m<--\033[0m gedrueckt und ($auswahl) gewählt"

8) :lol:
Ps.: Kleine Fonts sind eine Verschwoerung gegen uns aelteren Leute :D
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Wed Apr 11, 2018 3:47 pm    Post subject: Reply with quote

Sorry, ich hatte das:

Quote:
[...] REPLY ( The default variable for the read builtin. )


total überlesen. :roll:
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum