heredoc (docs emblebed)
un heredoc es la manera de «embutir» un documento de texto dentro de un script para volcar a nuestro criterio.
ejemplos
el EOF usado en los ejemplos no deja de ser una etiqueta, no es mandatory… podríamos usar MITEXTO, la cuestión es definir principio y fin con la misma etiqueta
sintaxis básica
no se permite indentación?
#!/bin/bash OUT=/tmp/output.txt cat <<EOF Status of backup as on $(date) Backing up files $HOME and /etc/ EOF echo "Starting backup using rsync..."
+ indentación natural del doc
permite indentación del documento!
#!/bin/bash OUT=/tmp/output.txt cat <<-EOF Status of backup as on $(date) Backing up files $HOME and /etc/ EOF echo "Starting backup using rsync..."
+ no bash expansion
#!/bin/bash OUT=/tmp/output.txt # No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. # If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document # are not expanded. So EOF is quoted as follows cat <<'EOF' >$OUT Status of backup as on $(date) Backing up files $HOME and /etc/ EOF
resultado:
Status of backup as on $(date) Backing up files $HOME and /etc/
+ redirección (también permite >>)
#!/bin/bash OUT=/tmp/output.txt cat <<EOF >$OUT Status of backup as on $(date) Backing up files $HOME and /etc/ EOF
en memoria!
#!/bin/bash OUT=/tmp/output.txt ## in memory here docs ## thanks https://twitter.com/freebsdfrau exec 9<<EOF Status of backup as on $(date) Backing up files $HOME and /etc/ EOF ## continue echo "Starting my script..." echo "Doing something..." ## do it cat <&9 >$OUT echo "Starting backup using rsync..."
/via: https://www.cyberciti.biz/faq/using-heredoc-rediection-in-bash-shell-script-to-write-to-file/