Created
November 21, 2018 03:43
-
-
Save garywill/00c222da8c10cc0d0b12f934514e2c25 to your computer and use it in GitHub Desktop.
bash get fd with temporary file in memory linked
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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