質問

while IFS=" " read token
do
  BUFFER="$BUFFER $token"
done < "$VAR"

I have a problem probably related to the fact that in the sh and bash shell read is being executed in a subprocess, so I can't find a correct way to save and "chain" the results of this while loop.

In short my BUFFER variable gets a reset at each cycle and I can't really think of a good portable way to make this work.

役に立ちましたか?

解決

Since $VAR is a list of filenames, I assume you need the files concatenated together, then redirected to the while loop. I think something like the following should work in :

$ VAR="
/etc/networks
/etc/papersize
"
$ while IFS=" " read token; do
      BUFFER="$BUFFER $token"
  done < <( cat $VAR )
$
$ echo $BUFFER
# symbolic names for networks, see networks(5) for more information link-local 169.254.0.0 letter
$ 

Note the <( ) process substitution is -specific, so probably won't work with .


Since you need something to work in , you can just put this the while loop in a for loop over all the filenames:

#!/bin/sh

VAR="
/etc/networks
/etc/papersize
"

for f in $VAR; do
    while IFS=" " read token; do
        BUFFER="$BUFFER $token"
    done < $f
done

echo $BUFFER
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top