Skip to main content
added 2 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
{ err=$(exec 2>&1 >&3; ls -ld /x /bin); exec 0<&3; out=$(cat); } 3>&1 
{ err=$(exec 2>&1 >&3; ls -ld /x /bin); exec 0<&3; out=$(cat); } 3>&1 
{ err=$(exec 2>&1 >&3; ls -ld /x /bin); exec 0<&3; out=$(cat); } 3>&1 
{ err=$(exec 2>&1 >&3; ls -ld /x /bin); exec 0<&3; out=$(cat); } 3>&1 
added 2 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
#! /bin/zsh - zmodload zsh/zselect zmodload zsh/system (){exec {wo}>$1 {ro}<$1} <(:) # like yash's wo>>|ro (but on Linux only) (){exec {we}>$1 {re}<$1} <(:) ls -d / /x >&$wo 2>&$we & exec {wo}>&- {we}>&- out= err= o_done=0 e_done=0 while ((! (o_done && e_done))) && zselect -A ready $ro $re; do if ((${#ready[$ro]})); then sysread -i $ro && out+=$REPLY || o_done=1 fi if ((${#ready[$re]})); then sysread -i $re && err+=$REPLY || e_done=1 fi done 
#! /bin/zsh zmodload zsh/zselect zmodload zsh/system (){exec {wo}>$1 {ro}<$1} <(:) # like yash's wo>>|ro (but on Linux only) (){exec {we}>$1 {re}<$1} <(:) ls -d / /x >&$wo 2>&$we & exec {wo}>&- {we}>&- out= err= o_done=0 e_done=0 while ((! (o_done && e_done))) && zselect -A ready $ro $re; do if ((${#ready[$ro]})); then sysread -i $ro && out+=$REPLY || o_done=1 fi if ((${#ready[$re]})); then sysread -i $re && err+=$REPLY || e_done=1 fi done 
#! /bin/zsh - zmodload zsh/zselect zmodload zsh/system (){exec {wo}>$1 {ro}<$1} <(:) # like yash's wo>>|ro (but on Linux only) (){exec {we}>$1 {re}<$1} <(:) ls -d / /x >&$wo 2>&$we & exec {wo}>&- {we}>&- out= err= o_done=0 e_done=0 while ((! (o_done && e_done))) && zselect -A ready $ro $re; do if ((${#ready[$ro]})); then sysread -i $ro && out+=$REPLY || o_done=1 fi if ((${#ready[$re]})); then sysread -i $re && err+=$REPLY || e_done=1 fi done 
added 492 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

After exec 0<&3, fds 0, 1, and 3 are all pointing to that same open file description (created when your terminal emulator opened the slave side of the pseudo-terminal pair it created before executing your shell in the case of the command run in the terminal case above).

Then in out=$(cat), for the process executing cat the $(...) changes fd 1 to the writing end of a pipe, while 0 is still the tty device. So cat will read from the terminal device, so things you're typing on the keyboard (and if it wasn't a terminal device, you would probably get an error as the fd was probably open in write-only mode).

After exec 0<&3, fds 0, 1, and 3 are all pointing to that same open file description (created when your terminal emulator opened the slave side of the pseudo-terminal pair it created before executing your shell in the case of the command run in the terminal above).

Then in out=$(cat), the $(...) changes fd 1 to the writing end of a pipe, while 0 is still the tty device. So cat will read from the terminal device, so things you're typing on the keyboard (and if it wasn't a terminal device, you would probably get an error as the fd was probably open in write-only mode).

After exec 0<&3, fds 0, 1, and 3 are all pointing to that same open file description (created when your terminal emulator opened the slave side of the pseudo-terminal pair it created before executing your shell in the case of the command run in the terminal case above).

Then in out=$(cat), for the process executing cat the $(...) changes fd 1 to the writing end of a pipe, while 0 is still the tty device. So cat will read from the terminal device, so things you're typing on the keyboard (and if it wasn't a terminal device, you would probably get an error as the fd was probably open in write-only mode).

added 492 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading