In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces linux how to use heredoc as a text editor, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
There is an unknown feature in the shell of Linux and Unix that allows you to open a do-while loop with the cat command. It's called heredoc, and no matter what shell you use, it gives you more or less a text editor. Its syntax is:
$cat > example.txt
The middle string (EOF) is essentially a condition to stop the loop. That is, if you type it separately on a line, the loop ends. During the loop, whatever you enter in the terminal is piped to the target file (in this case).
Installation
As long as you have a terminal, you can start heredoc. I have used this syntax technique in Bash, tsh, and Korn shell.
Use heredoc
To open a heredoc "session", you can use the cat command with redirection. Start by pointing to the cat command with a termination string (the common convention is EOF, which stands for "End Of File", but it can actually be any string). After terminating the string, redirect the output to a target file. You can then type directly into the terminal and use the most common shell keyboard shortcuts to handle your work. When you enter the termination string you specify on a line, your session ends. You can know that you are in a heredoc loop through the only prompt (usually >).
$cat > example.txt > Everything you type here will be placed into example.txt when I type EOF on a line by itself. Until then, you can type... > > whatever... > > you want to type. > EOF$
While the terminal is waiting for EOF, everything you enter will be placed in the target file, the prompt will be ignored, and the EOF itself is not part of the file.
Everything you type here will be placed into example.txt when I type EOF on a line by itself. Until then, you can type... Whatever... You want to type.
In reality, you may not use heredoc syntax instead of a normal text editor. It is a good fast processing method, you can enter multiple lines, but more than 10 lines or so begin to limit its effect. For example, if you don't trigger the history function of your shell, you can't edit the previous line. Depending on your shell and configuration, you may need to press the up arrow first, then press the down arrow to retrieve your text, and then use Ctrl+B to back up.
Most of your shell functions work, but there may be no undo and no error recovery.
In addition, even the simplest installed system may have at least Vi or ed installed.
However, heredoc is still very useful! It is more flexible than echo and is indispensable when you are writing shell scripts. For example, imagine that you are writing an installation script so that you can automatically install a set of custom applications. One of the apps does not generate a .dekstop file, so it will not appear in your application menu. To solve this problem, you decide to generate a .install file at installation time.
Instead of writing a .install file and then acting as an external dependency on the installation script, use heredoc in the installation script:
#! / bin/sh VERSION=$ {VERSION:-x.y.z} PKGNAM=$ {VERSION:-example} PKG= "${PKGNAM}"-"${VERSION}"-`arch`.tgz # download packagewget "${PKG}" tar txvf "${PKG}" # use here doc to create missing > $HOME/.local/share/applications/example.desktop [Desktop Entry] Version=1.0Type=ApplicationName= "${PKGNAM}" Comment= "${PKGNAM}" Exec= "${PKGNAM}"% FEOF # insert the rest of an install script...
You automatically enter text into a file without the need for a text editor (except for the one you use to write scripts, of course). Here is what the generated .clients file looks like:
[Desktop Entry] Version=1.0Type=ApplicationName=exampleComment=exampleExec=example% F
As you can see, you can use variables in heredoc, and they are parsed correctly. The EOF string does not appear in the file, it just marks the end of the heredoc.
Better than echo
Heredoc technology is generally considered easier than echo or printf, because once you "enter" the document, you are free to do whatever you want. In this sense, it is free, but it is limited compared to a suitable text editor.
Using heredoc to take quick notes and shell scripts, you no longer have to worry about how to generate configuration files dynamically.
Thank you for reading this article carefully. I hope the article "how linux uses heredoc as a text editor" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!
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.