How to use a variable to indicate a file descriptor in bash?
The accepted answer is correct, but as of bash 4.1, you can use automatic file descriptor allocation, and in that case you don't need eval
:
file=aexec {id}<>"$file"
Then you can use it like this:
echo test >&${id}
or:
fsck -v -f -C ${id} /dev/something
You have to use eval
and put the entire expression in quotes.
eval "exec $id<>$file"
And do that every time you want to use $id
.
I found the discussion in the answer of tobias.pal very interesting: https://stackoverflow.com/a/32689974/1184842
for (( FD=3 ; FD < 100 ; FD++ )) ; do exec {FD}> file.$FD ; echo $FD >&${FD}; done
This would not work, since exec {FD}> file.${FD}
would be the same descriptor over all values of $FD, right? (Denio Mariz)
I solved this by using an array as stated by Drew Chapin:
#!/bin/bash# global variables for temp file handlingdeclare -a TEMPORARY_FILES_WRITE;declare -a TEMPORARY_FILES_READ;function createTempFile() { local usecase="$1" local id="$2" local tmpfile=$(mktemp) # Create a temporal file in the default temporal folder of the system # Lets do some magic for the tmpfile to be removed when this script ends, even if it crashes exec {TEMPORARY_FILES_WRITE[$id]}>"$tmpfile" exec {TEMPORARY_FILES_READ[$id]}<"$tmpfile" rm "$tmpfile" # Delete the file, but file descriptors keep available for this script} for (( FD=3 ; FD < 100 ; FD++ )) ; do TEMP_FILE_COUNTER=$((TEMP_FILE_COUNTER + 1)) createTempFile "Iteration $FD" $FD ; echo $FD >&${TEMPORARY_FILES_WRITE[$FD] ;doneexample=$(cat <&${TEMPORARY_FILES_READ[50]})echo $example
This will output 50.