Re: How to make `read` fail if there is not enough fields in the input?
From:
Peng Yu
Subject:
Re: How to make `read` fail if there is not enough fields in the input?
Date:
Thu, 5 Dec 2019 11:19:12 +0800
>> Iād like to know whether there is enough field in a TSV row. But the status
>> code does not return an error code. Is there a way to let read return
>> non-zero if there is not enough fields? Thanks.
>>
>> $ IFS=$'\t' read -r x y <<< x
>> $ echo $?
>> 0
>
> $ IFS=$'\t' read -r -a array <<< x && (( "${#array[@]}" == 2 ))
> $ echo $?
> 1
> $ IFS=$'\t' read -r -a array <<< x$'\t'y && (( "${#array[@]}" == 2 ))
> $ echo $?
> 0
>
But this is too verbose. I also want to directly refer to x and y rather than
the array. Anything as succinct as the original bash code?
Regards,
Peng