--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.
――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.
--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 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 **
Recommended Posts