How to use a variable to indicate a file descriptor in bash?

有些话、适合烂在心里 提交于 2019-12-03 11:53:17

问题


I want to use a bash variable to indicate a file descriptor, like this:

id=6
file=a
exec $id<>$file

But the usage is wrong:

-bash: exec: 6: not found

So, how to use a variable to indicate a file descriptor in exec command?


回答1:


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.




回答2:


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=a
exec {id}<>"$file"

Then you can use it like this:

echo  test >&${id}

or:

fsck -v -f -C ${id} /dev/something



回答3:


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 handling
declare -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] ;
done

example=$(cat <&${TEMPORARY_FILES_READ[50]})
echo $example

This will output 50.



来源:https://stackoverflow.com/questions/8295908/how-to-use-a-variable-to-indicate-a-file-descriptor-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!