Keywords: EOF
In the usual operation and maintenance work, we often encounter such a scenario:
when executing the script, the need for automatic input N line to the contents of a file. If a minority of a few lines, you can use echo append mode, but if it is many lines, then simply using echo additional ways it is very stupid!
This time, you can use in conjunction with EOF cat command to append the contents of the line.
Here's the sort of usage EOF:
EOF is END Of File Abbreviation, custom represents terminator. Since customizations, EOF is not fixed, can freely set the alias, the linux press Ctrl- D represents EOF. EOF cat can usually fit multiple lines of text output. Its usage is as follows: EOF << // start .... EOF // End You can also customize, such as customize: BBB << // start .... BBB // End By cat with redirection can generate the file and append operation, before it be familiar with several special symbols: < : Input redirection > : output redirection >> : output redirection, is additionally not overlay content prior to << : the contents of the standard input a pair of spaced intermediate number from the command line. To cite a simple example, Example 1: # cat << EOF In the event of input prompt " > " , enter the following: > the Hello > EOF After entry, the terminal displays the following: Hello
Question:
We know from the description of the cat command, cat operation object is a file, but a cat in the cases of the operation object is not a file, but the user input.
So we can understand Example 1: first enter "Hello" in the document file, and then cat file output content.
In other words we can use a file instead of "<< EOF EOF".
Conversely, if the file operation command is input object, you can also use "<< EOF EOF" to be replaced.
In order to verify the above thinking, we test two examples:
Example 2. Consider the following disk partition script:
sfdisk -uM /dev/sda << EOF ,2048,b ,1024,83 ,1024,83 EOF According to previous thinking, the " << EOF " and " EOF " save the contents to a file between the part, and then modify the script: sfdisk -uM /dev/sda < part After testing, the way the revised partition can achieve the same result.
Example 3. The contents of a file to another file output:
# cat fileA > fileB According to previous thinking, the " << EOF EOF " alternate input object file fileA: # cat << EOF > fileB After testing, the command prompt for user input, after the input, the input contents are saved to the user in fileB. In summary, " the role << EOF EOF" is in the process of implementation of user-defined command input, which is similar to a temporary file to play the role, but more convenient and flexible than using the file.
Let's feel for use by specific examples EOF beauty:
1) an entry into the file in 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) custom EOF, such as custom 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, enter multiple lines of content to a file
[root @ Slave opt-Server] # Touch /usr/local/mysql/my.cnf // file is not created in advance can do, if you do not exist, EOF command will automatically create [root @ slave-server opt] # vim the Test. SH # ! / bin / bash cat > /usr/local/mysql/my.cnf << EOF //或者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 the Test. SH // execute the above script [root @ Slave opt-Server] # CAT /usr/local/mysql/my.cnf // check whether to write the script EOF success [ 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
-------------------------------------------------- -------------------------------
below to share a script to automatically create a new partition and mount:
[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
Original link: https: //blog.csdn.net/zongshi1992/article/details/71693045