In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use Bash to read and write files". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Learn how Bash reads and writes data differently, and when to use each method.
When you write scripts in Bash, sometimes you need to read data from a file or write data to a file. Sometimes the file may contain configuration options, while other times the file is data created by your users with your application. Each language handles this task somewhat differently, and this article demonstrates how to use Bash and other POSIX shell to process data files.
Install Bash
If you are using Linux, you may already have Bash. If not, you can find it in your software repository.
On macOS, you can use the default terminal, Bash or Zsh, depending on the version of macOS you are running.
There are several ways to experience Bash on Windows, including Windows Subsystem for Linux (WSL), which is officially supported by Microsoft.
After installing Bash, open your favorite text editor and get ready to start.
Use Bash to read files
In addition to shell, Bash is also a scripting language. There are several ways to read data from Bash. You can create a data stream and parse the output, or you can load the data into memory. Both of these methods are effective ways to obtain information, but each method has quite specific use cases.
Cite documents in Bash
When you "invoke source" a file in Bash, you ask Bash to read the contents of the file and expect it to contain valid data, which Bash can put into the data model it builds. You don't want to invoke data from old files, but you can use this method to read configuration files and functions.
(LCTT translation note: in Bash, you can use source or. Command to read in a file, this behavior is called "sourcing", which originally means "one-time (trial) purchase", "find a supplier", "obtain" and so on. Considering the context and pronunciation of Bash, I suggest that it can be translated as "quote" or inappropriate for your discussion and reference-- wxy)
For example, create a file named example.sh and enter the following:
#! / bin/sh greet opensource.com echo "The meaning of life is $var"
Run this code and see that it failed:
$bash. / example.sh./example.sh: line 3: greet: command not foundThe meaning of life is
Bash does not have a command called greet, so it cannot be executed on that line, and there is no variable record called var, so the file is meaningless. To solve this problem, create a file called include.sh:
Greet () {echo "Hello ${1}"} var=42
Modify your example.sh script and add a source command:
#! / bin/sh source include.sh greet opensource.com echo "The meaning of life is $var"
Run the script and you can see the work:
$bash. / example.shHello opensource.comThe meaning of life is 42
The greet command is brought into your shell environment because it is defined in the include.sh file, and it even recognizes parameters (opensource.com in this case). The variable var is also set and imported.
Parsing files in Bash
Another way to "enter" data into Bash is to parse it into a data stream. There are many ways to do this. You can use grep or cat or any command that can get the data and pipe it to standard output. In addition, you can use something built into Bash: redirection. Redirection itself is not very useful, so in this example, I also use the built-in echo command to print the result of the redirection:
#! / bin/sh echo $(
< include.sh ) 将其保存为 stream.sh 并运行它来查看结果: $ bash ./stream.shgreet() { echo "Hello ${1}" } var=42$ 对于 include.sh 文件中的每一行,Bash 都会将该行打印(或 echo)到你的终端。先用管道把它传送到一个合适的解析器是用 Bash 读取数据的常用方法。例如, 假设 include.sh 是一个配置文件, 它的键和值对用一个等号(=)分开. 你可以用 awk 甚至 cut 来获取值: #!/bin/sh myVar=`grep var include.sh | cut -d'=' -f2` echo $myVar 试着运行这个脚本: $ bash ./stream.sh42用 Bash 将数据写入文件 无论你是要存储用户用你的应用创建的数据,还是仅仅是关于用户在应用中做了什么的元数据(例如,游戏保存或最近播放的歌曲),都有很多很好的理由来存储数据供以后使用。在 Bash 中,你可以使用常见的 shell 重定向将数据保存到文件中。 例如, 要创建一个包含输出的新文件, 使用一个重定向符号: #!/bin/sh TZ=UTCdate >Date.txt
Run the script several times:
$bash. / date.sh$ cat date.txtTue Feb 23 22:25:06 UTC 2021$ bash. / date.sh$ cat date.txtTue Feb 23 22:25:12 UTC 2021
To append data, use two redirection symbols:
#! / bin/sh TZ=UTCdate > > date.txt
Run the script several times:
$bash. / date.sh$ cat date.txtTue Feb 23 22:25:12 UTC 2021Tue Feb 23 22:25:17 UTC 2021Tue Feb 23 22:25:19 UTC 2021Tue Feb 23 22:25:22 UTC 2021Bash easy programming
The advantage of Bash is that it is easy to learn, because you can build complex programs with only a few basic concepts. For complete documentation, please refer to the excellent Bash documentation on GNU.org.
This is the end of "how to use Bash to read and write files". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.