[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Readig multiple null-separated values
From: |
František Kučera |
Subject: |
Re: [Help-bash] Readig multiple null-separated values |
Date: |
Sat, 11 Jan 2020 20:10:48 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 |
Dne 27. 07. 19 v 10:58 František Kučera napsal(a):
> To read a record in a cycle I wanted to use the read command with proper
> parameters first, but it seemed impossible and I ended with a custom function
> which work as I need:
>
> read_nullbyte() { for v in "$@"; do export "$v"; read -r -d '' "$v"; done }
>
> Usage example:
>
> printf 'a\0aaa\0b\0bbb\0c\0ccc' | while read_nullbyte a1 a2; do echo a1=$a1
> a2=$a2; done
I found a bug in this function. It should be:
read_nullbyte() { local IFS=; for v in "$@"; do export "$v"; read -r -d ''
"$v"; done }
because with default IFS it truncates whitespace from the beginning and end of
the value. Test:
printf 'a\0 b b \0c' | while read_nullbyte "x"; do echo ">$x<"; done
Franta
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Re: [Help-bash] Readig multiple null-separated values,
František Kučera <=