help-bash
[Top][All Lists]
Advanced

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

Re: non working code .. loop ends after one


From: Kerin Millar
Subject: Re: non working code .. loop ends after one
Date: Wed, 1 Mar 2023 22:22:56 +0000

On Wed, 1 Mar 2023 22:11:35 +0100
alex xmb ratchev <fxmbsw7@gmail.com> wrote:

> hello ,
> im at a scp file list script
> it had few typos , then it began to work
> but only one line outta the file
> then nothing , just exit ..
> 
>  the cmd is
> bash -x scp.u shone {user}@192.168.106.97:/mnt/1/fav aa1
> 
> shone the ' ssh hash one dir '
> aa1 is a \0 sep file list

Are you sure?

In any case, the code is odd. sep=${sep:-\\0} assigns '\0' if otherwise 
undefined. [[ $sep == *\\* ]] && declare -a "sep=$( $'sep' )" will then make 
sep an array (why?) whose first element is an empty string. It is unnecessarily 
complicated and error-prone. Just let sep _be_ the empty string to begin with, 
if you expect records ending with NUL.

Despite this, it doesn't do the wrong thing in the case that sep begins by 
being unset/empty.

$ unset -v sep; sep=${sep:-\\0}; [[ $sep == *\\* ]] && declare -a "sep=( 
$'$sep' )"
$ declare -p sep
declare -a sep=([0]="")
$ printf '%s\0' foo bar | { IFS= read -rd "$sep" x; IFS= read -rd "$sep" y; 
declare -p x y; }
declare -- x="foo"
declare -- y="bar"

This works because "$sep" == "${sep[0]}" == "". If you were to produce a 
smaller test case that someone else can easily run, it could help to get to the 
bottom of it.

-- 
Kerin Millar



reply via email to

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