In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you what is the specific method of writing Linux Bash into the file, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Using the redirect operator to write to a file in Bash, the redirection of the output allows you to capture the output from the command and write it to the file.
The general format for redirecting and writing output to a file is as follows:
Output > filename output > > filenameCopy
The redirect operator writes the output to the specified file. If the file exists, it is truncated to zero length. Otherwise, a file is created. Be careful when using this operator, as you may overwrite important files.
The redirect operator appends the output to the given file. If the file does not exist, it is created.
You need to have write access to the file. Otherwise, you will receive an error denying permission.
This is a simple example that shows how to redirect the output of the echo command to a file:
Echo "this is a line" > file.txtCopy
To prevent overwriting existing files, use the built-in set to enable the "noclobber" option:
Set-o noclobber echo "this is a line" > file.txtCopy
Bash: file.txt: cannot overwrite existing fileCopy
> | operator allows you to override the "noclobber" option of Bash:
Set-o noclobber echo "this is a line" > | file.txtCopy
The operator appends the output to the end of the file instead of overwriting the file:
Echo "this is a line" > > file.txtCopy
Use the printf command to create complex output:
Printf "Hello, Isimm% s.\ n" $USER > file.txtCopy
If you want to write multiple lines to a file, use heredoc document (Heredoc) redirection.
For example, you can pass the content to the cat command and write it to a file:
Cat file.txt The current working directory is: $PWD You are logged in as $(whoami) EOFCopy
To add these lines, change > to > > before the file name:
Cat > file.txt The current working directory is: $PWD You are logged in as $(whoami) EOFCopy
You can write the output of any command to a file:
Date + "Year:% Y, Month:% m, Day:% d" > file.txtCopy
The output of the date command is written to the file.
Use the tee command to write files the tee command reads from standard input and writes standard output and one or more files at the same time.
Echo "this is a line" | tee file.txtCopy
The default behavior of the tee command is to overwrite the specified file, the same as the > operator. To attach the output to a file, invoke the command using the-a (--append) option:
Echo "this is a line" | tee-a file.txtCopy
If you do not want tee to write to standard output, you can redirect it to / dev/null:
Echo "this is a line" | tee file.txt > / dev/nullCopy
To write text to multiple files, specify the file as an argument to the tee command:
Echo "this is a line" | tee file_1.txt file_2.txt file_3.txtCopy
Another advantage of the tee command is that you can use it with sudo and write to files owned by other users. To append text to a file that you do not have write permission to, add sudo before tee:
Echo "this is a line" | sudo tee file.txtCopy
The output of the echo command is passed as input to tee, which elevates sudo permissions and writes text to the file.
On the specific method of writing Linux Bash file is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.