heredoc (docs emblebed)

un heredoc es la manera de «embutir» un documento de texto dentro de un script para volcar a nuestro criterio.

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

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..."

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..."
#!/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/
#!/bin/bash
OUT=/tmp/output.txt
 
cat <<EOF >$OUT
  Status of backup as on $(date)
  Backing up files $HOME and /etc/
EOF
#!/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/

  • linux/bash/heredoc.txt
  • Darrera modificació: 23/11/2017 05:10
  • per mate