help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Why read exits non-zero? How to safely use bash's built-


From: Chet Ramey
Subject: Re: [Help-bash] Why read exits non-zero? How to safely use bash's built-in read command?
Date: Mon, 7 Oct 2019 09:43:45 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.9.0

On 10/7/19 2:47 AM, Patrick Schleizer wrote:

> #!/bin/bash
> 
> set -e
> 
> test -f ./advancedsettings.ico
> 
> while IFS= read -r -d '' line; do
>    echo "$line"
>    IFS= read -r -d '' first_word _ <<< "$line"
>    echo "$first_word"
> done < "./advancedsettings.ico"

Think about this. The first read guarantees that the value of `line' will
not contain a NUL, since you use that as the delimiter. Why, then, would
you expect a NUL when you use the value in a here-string?

Since there's no NUL, reading the here-string will always return EOF before
hitting the delimiter, causing read to return 1.


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    address@hidden    http://tiswww.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]