Skip to content

Instantly share code, notes, and snippets.

@garywill
Created November 21, 2018 03:43
Show Gist options
  • Save garywill/00c222da8c10cc0d0b12f934514e2c25 to your computer and use it in GitHub Desktop.
Save garywill/00c222da8c10cc0d0b12f934514e2c25 to your computer and use it in GitHub Desktop.
bash get fd with temporary file in memory linked
#!/bin/bash
get_fd() {
local x
local tmpfile
local _fd_var=$1
for x in $(seq 10 $(ulimit -n)); do
if [[ ! -a "/proc/$BASHPID/fd/$x" ]]; then
tmpfile=$(mktemp /dev/shm/XXX.tmp)
exec {x}<>$tmpfile
printf -v "$_fd_var" %s "$x"
rm $tmpfile
return
fi
done
echo 0
}
get_fd tf_fd
echo hello >&$tf_fd
cat /dev/fd/$tf_fd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment