[Bash] Redirection of multiple lines to multiple files

--When using Bash, you may want to write a character string with multiple lines of content in a shell script to multiple files. ――At that time, multiple ʻechoor multiple>` are written one after another, and it may become long or duplicates may be noticeable. --Therefore, this time, record the method of writing (redirecting) the contents of multiple lines to multiple files at once.

result

――The contents of the result are shown below. --This is a method using Bash's ** here document ** and ** tee command **.

function multi(){
	#Definition of multi-line strings in here-documents
	cat <<-EOF
		# list data
		aaa bbb ccc
		ddd eee fff
		ggg hhh iii
	EOF
}

#Pass to tee command and write(redirect)Specify the target file
#Because it is non-output/dev/Discard to null.
multi | tee result.txt result.bak.txt > /dev/null

--After writing in any shell file, complete when execution and creation confirmation are possible.

Contents

Utilization of here documents

--Use Here document when defining multiple strings. ――The following two points were conscious at that time. -** <<- to enable indentation. ** ** --Specify this to delete the first tab character and adjust the appearance. -** Put it in the function. ** ** --The written content, whether it is directly specified or a variable, is put in a function and made into a constant. ――It leads to "universality of organizing and using written contents (pipes, etc.)".

Use of tee command

-** Use the ** tee command that outputs and writes at the same time from input. --By doing so, by passing the defined contents to tee, it will output and write to the specified file at the same time. -** * However, since it does not output, pass the contents to / dev / null and do not output **

reference

Recommended Posts

[Bash] Redirection of multiple lines to multiple files
Summary of how to import files in Python 3
Convert multiple jpg files to one PDF file
Batch conversion of Excel files to JSON [Python]
[Java] How to switch between multiple versions of Java
I wanted to know the number of lines in multiple files, so I tried to get it with a command
How to write string concatenation in multiple lines in Python
How to display multiple images of galaxies in tiles
Quickly list multiple lines of text in your code
Summary of how to share state with multiple functions
Multiple inheritance of classes
Copy of multiple List
[Command] Command to get a list of files containing double-byte characters
I tried using "Syncthing" to synchronize files on multiple PCs
[R] [Python] Memo to read multiple csv files in multiple zip files
Summary of how to write .proto files used in gRPC
Commands and files to check the version of CentOS Linux
Command to list all files in order of file name