Here documents (heredoc) tips
cat <<EOF > config.props url=http://stack1.com component=123 name=config timeout=10 EOF
Creates config.props with the contents:
url=http://stack1.com
component=123
name=config
timeout=10
tr [a-z] [A-Z] << EOF The cat sat on the mat The cow jumped over the moon EOF
Gives the output:
THE CAT SAT ON THE MAT
THE COW JUMPED OVER THE MOON
cat << END_TEXT | awk '{print $3 " " $2 " " $1}'
one two three
three two one
END_TEXT
Gives the output:
three two one
one two three
cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: env-config
restartPolicy: Never
EOF
Creates a kubernetes busybox Pod
Using heredocs with sudo
example 1:
sudo bash -c "cat >> /etc/hosts" << "EOF" 10.0.1.100 ansible 10.0.1.101 kube_controller 10.0.1.102 kube_node_1 10.0.1.103 kube_node_2 10.0.1.104 kube_node_3 EOF
example 2:
cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo [influxdb] name = InfluxDB Repository - RHEL \$releasever baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable enabled = 1 gpgcheck = 1 gpgkey = https://repos.influxdata.com/influxdb.key EOF