Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand bash readarray

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to understand bash readarray". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand bash readarray.

§0x01 Origin

There are the following requirements in the work, to parse the contents of a file, but the file is in json format, I do not want to use jq. The rows that meet the requirements are filtered out by grep and then processed in the for loop. And then found that this can not be achieved.

Lines=$ (grep xxxx / path/to/file.json) for line in ${lines}; do echo ${line} done

The content of the grep output becomes one line. Comparing the running environment with ubuntu, it is found that the content of lines is different. In the runtime, it is one line (the newline character disappears), and in ubuntu it is multiple lines. After changing the sed, it is confirmed that it is the problem with the bash implementation. You can't update the version of bash in the runtime environment, so you have to consider other implementations.

§0x02 readarry

After looking up the data, we found that readarray, a built-in function of bash, can implement this requirement, and it also has an individual named mapfile. Personally, I think readarray is easier to understand. Its actual way is as follows:

# declare that it is a list delcare-a linesreadarray-t lines <

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report