Reprinted from http://www.cnblogs.com/kevingrace/p/6257490.html
In the ordinary operation and maintenance work, we often encounter such a scenario:
When executing a script, you need to input N lines into a file automatically. If it's a few lines, you can also use echo additions, but if it's a lot of lines, it's foolish to use echo additions alone!
At this point, you can use EOF combined with cat command to add line content.
Following is an overview of the use of EOF:
EOF is the abbreviation of END Of File, which means a custom terminator. Since it is customized, EOF is not fixed. You can set aliases at will. Press ctrl-d on linux to represent EOF.
EOF usually works with cat to output multiple lines of text.
Its usage is as follows:
<<EOF //start .... EOF //End
You can also customize, such as customizing:
<<BBB //start .... BBB //End
With cat and redirection, you can generate files and add operations. Before cat, you should be familiar with several special symbols:
<: Input redirection > Output redirection Output redirection, appending, not overwriting previous content < <: Standard input is the middle of a pair of separators from the command line.
Below is a concrete example to feel the beauty of EOF usage:
1) Enter content into the file test.sh.
[root@slave-server opt]# cat << EOF >test.sh > 123123123 > 3452354345 > asdfasdfs > EOF [root@slave-server opt]# cat test.sh 123123123 3452354345 asdfasdfs
Additional content
[root@slave-server opt]# cat << EOF >>test.sh > 7777 > 8888 > EOF [root@slave-server opt]# cat test.sh 123123123 3452354345 asdfasdfs 7777 8888
cover
[root@slave-server opt]# cat << EOF >test.sh > 55555 > EOF [root@slave-server opt]# cat test.sh 55555
2) Customize EOF, such as wang
[root@slave-server opt]# cat << wang > haha.txt > ggggggg > 4444444 > 6666666 > wang [root@slave-server opt]# cat haha.txt ggggggg 4444444 6666666
3) You can write scripts to enter multiple lines of content into a file.
[root@slave-server opt]# touch /usr/local/mysql/my.cnf // / file can be created without advance. If it does not exist, it will be created automatically in the EOF command. [root@slave-server opt]# vim test.sh #!/bin/bash cat > /usr/local/mysql/my.cnf << EOF //Or cat << EOF >/usr/local/mysql/my.cnf [client] port = 3306 socket = /usr/local/mysql/var/mysql.sock [mysqld] port = 3306 socket = /usr/local/mysql/var/mysql.sock basedir = /usr/local/mysql/ datadir = /data/mysql/data pid-file = /data/mysql/data/mysql.pid user = mysql bind-address = 0.0.0.0 server-id = 1 sync_binlog=1 log_bin = mysql-bin [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES port = 3306 EOF [root@slave-server opt]# sh test.sh// Execute the above script [root@slave-server opt]# cat /usr/local/mysql/my.cnf // / / Check if EOF in the script was written successfully [client] port = 3306 socket = /usr/local/mysql/var/mysql.sock [mysqld] port = 3306 socket = /usr/local/mysql/var/mysql.sock basedir = /usr/local/mysql/ datadir = /data/mysql/data pid-file = /data/mysql/data/mysql.pid user = mysql bind-address = 0.0.0.0 server-id = 1 sync_binlog=1 log_bin = mysql-bin [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES port = 3306
Here's a script to automatically create new partitions and mount them:
[root@es-node1 ~]# cat auto_add_disk.sh #!/bin/bash fdisk /dev/sdb <<EOF n p 1 wq EOF /sbin/mkfs.ext4 /dev/sdb1 && /bin/mkdir -p /data && /bin/mount /dev/sdb1 /data echo 'LABEL=data_disk /data ext4 defaults 0 2' >> /etc/fstab