io redirection - Bash read function returns error code when using new line delimiter -


i have script returning multiple values from, each on new line. capture values bash variables using read builtin (as recommended here).

the problem when use new line character delimiter read, seem non-zero exit code. playing havoc rest of scripts, check result of operation.

here cut-down version of doing:

$ read -d '\n' b c < <(echo -e "1\n2\n3"); echo $?; echo $a $b $c 1 1 2 3 

notice exit status of 1.

i don't want rewrite script (the echo command above) use different delimiter (as makes sense use new lines in other places of code).

how read play nice , return 0 exit status when reads 3 values?

update

hmmm, seems may using "delimiter" wrongly. man page:

-d *delim*     first character of delim used terminate input line,   rather newline. 

therefore, 1 way achieve desired result this:

read -d '#' b c < <(echo -e "1\n2\n3\n## end ##"); echo $?; echo $a $b $c 

perhaps there's nicer way though?

-d wrong thing use here. want 3 separate calls read:

{ read a; read b; read c; } < <(echo $'1\n2\n3\n') 

be sure input ends newline final read has exit status of 0.

if don't know how many lines in input ahead of time, need read values array. in bash 4, takes single call readarray:

readarray -t arr < <(echo $'1\n2\n3\n') 

prior bash 4, need use loop:

while read value;     arr+=("$value") done < <(echo $'1\n2\n3\n') 

read reads single line of input; -d option changes read's idea of terminates line. example:

$ while read -d'#' value; >    echo "$value" > done << eof > a#b#c# > eof b c 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -