Skip to content

Instantly share code, notes, and snippets.

@kherge
Last active November 3, 2021 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kherge/092e4c28187d4cf5fd1f02d8b3b3d311 to your computer and use it in GitHub Desktop.
Save kherge/092e4c28187d4cf5fd1f02d8b3b3d311 to your computer and use it in GitHub Desktop.
Using temporary pipes to run shell code from binary executables.
#!/bin/sh
###
# Uses the 3 pipe to run commands.
#
# In Rust: https://stackoverflow.com/a/54858159
##
inner()
{
echo 'echo "Hello, before!"' >&3
# If we don't sleep, printf may run before shell code above.
sleep 0.1
printf 'Name? '
read -r NAME
echo 'echo "Hello, '"$NAME"'!"' >&3
echo break >&3
}
# Create the name for the pipe.
PIPE="$(mktemp -u)"
# Create the pipe.
mkfifo "$PIPE"
# Alias it to "3".
exec 3<>"$PIPE"
# Remove the actual file.
rm "$PIPE"
# Run a command that uses the pipe.
(inner 0<&0 &)
# Evaluate output from 3.
while read -r THIRD; do
eval $THIRD
done <&3
# Wait for background task.
wait
# Close the pipe.
exec 3>&-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment