• Willkommen im Linux Club - dem deutschsprachigen Supportforum für GNU/Linux. Registriere dich kostenlos, um alle Inhalte zu sehen und Fragen zu stellen.

[gelöst] Bash Script output in Datei schreiben

Hallo zusammen,

irgendwie stehe ich gerade auf dem Schlauch -__
Ich habe ein Bash script geschrieben wo ein Funktion aufgerufen wird, die eine Datei schreiben soll.
In dieser Datei sind wiederum viele Variablen, diese werden aber nicht mitgeschrieben.

Beispiel:

Code:
#!/bin/bash
cat > output <<End-of-message
-------------------------------------
This is line 1 of the message.
This is line 2 of the message.
This is line 3 of the message.
This is line 4 of the message.
$NAME <-- dies soll auch in die Datei geschrieben werden
This is the last line of the message.
-------------------------------------
End-of-message

Welche Ansätze gibt es hier ?

Danke schön ;-)
 

framp

Moderator
Teammitglied
man bash schrieb:
Here Documents

This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.

The format of here-documents is:

<<[-]word
here-document
delimiter

No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. In the latter case, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and `.

If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.

Code:
#!/bin/bash
cat > output <<"End-of-message"
-------------------------------------
This is line 1 of the message.
This is line 2 of the message.
This is line 3 of the message.
This is line 4 of the message.
$NAME <-- dies soll auch in die Datei geschrieben werden
This is the last line of the message.
-------------------------------------
End-of-message
liefert das gewuenschte Ergebnis ;)
 
Hallöchen Framp,

schön dich wieder lesen zu können =)

man cat hatte ich gemacht aber auf die simple idee zu kommen man bash zu machen -_-
Ab und zu sollte man den PC ausmachen und sich ablenken lassen ;-)

Danke schön !

ps.: Das ist das "Projektchen" an dem ich sitze:

https://github.com/urbanswelt/DelugePi
 

framp

Moderator
Teammitglied
urbanswelt schrieb:
...man cat hatte ich gemacht aber auf die simple idee zu kommen man bash zu machen -_-
Ich wusste auch nicht wie es geht - konnte mich aber dran erinnern dass die here-documents dort bei der bash beschrieben sind ;)

Ja, die Pi ist ein nettes kleines Schatzkaestchen :thumbs:
 
Oben