this post was submitted on 21 Jun 2023
2 points (100.0% liked)

Linux

48331 readers
382 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

I'm a complete noob when it comes to bash, I extracted some timestamps from an xml file using xmlstarlet however its formatted as a space separated string rather than an array. I need them as an array so that I can use them in a for loop. As far as I can tell you can't do that with xmlstarlet so I need to just convert the string.

I found this thread which says I can use IFS=' ' read -a arr <<< "$line" which works for a basic string but not the one I'm trying to use it with:

2023-06-19T00:00:00+01:00 2023-06-18T00:00:00+01:00 2023-06-17T00:00:00+01:00 2023-06-16T00:00:00+01:00 2023-06-15T00:00:00+01:00 2023-06-14T00:00:00+01:00 2023-06-13T00:00:00+01:00 2023-06-10T00:00:00+01:00 2023-06-03T00:00:00+01:00 2023-05-31T00:00:00+01:00 2023-05-27T00:00:00+01:00

If I use the command on this string it only puts the first part in the array so I get 2023-06-19T00:00:00+01:00

I also tried arr=( $line ) which is also suggested in the thread but that does the same thing. Is there another way I can try to convert this, or a way to export from xmlstarlet straight to an array?

you are viewing a single comment's thread
view the rest of the comments
[–] second 1 points 1 year ago

Are you sure it's not working? Does echo ${arr[@]} return the whole array? Does echo ${arr[1]} return the second value? I think just echoing $arr without specifying an index returns only the first value.