[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    Re: read stdin into variable?
From:       Wolf <wolf () wolfsden ! cz>
Date:       2020-11-08 22:35:50
Message-ID: 20201108223550.glghzkkckeb7jnvr () wolfsden ! cz
[Download RAW message or body]

[Attachment #2 (multipart/signed)]


On 2020-11-08 21:41:51 +0000, Harald van Dijk wrote:
> 
> There are two good workarounds. One is yours, which ensures read a b is not
> executed in a subshell environment. Depending on the use case, another good
> alternative is
> 
> echo 12 34 | {
>   read a b
>   echo "'${a}''${b}'"
> }
> 
> This still executes `read a b` in a subshell environment, but lets the
> following commands that use $a and $b also execute in that same subshell
> environment. This workaround can be used when the following commands do not
> need to read from the original stdin.

If the original stdin is needed, exec can be used. As in (the echo 1 2 is
supposed to simulate "the original stdin"):

echo 1 2 | {
	exec 3<&0
	echo 3 4 | {
		read a b
		printf '%s %s\n' "$a" "$b"
		read a b <&3
		printf '%s %s\n' "$a" "$b"
	}
}

But yes, this has the potential to get bit confusing fairly quickly.

Have a nice day,

W.

-- 
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.

["signature.asc" (application/pgp-signature)]

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic