如何使用变量来指示 bash 中的文件描述符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8295908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:06:47  来源:igfitidea点击:

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

bashioexecfile-descriptor

提问by WH's HeV

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

我想使用 bash 变量来指示文件描述符,如下所示:

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?

那么,如何在 exec 命令中使用变量来表示文件描述符呢?

采纳答案by eduffy

You have to use evaland put the entire expression in quotes.

您必须使用eval整个表达式并将其放在引号中。

eval "exec $id<>$file"

And do that every time you want to use $id.

每次你想使用$id.

回答by Paul Tobias

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:

接受的答案是正确的,但从bash 4.1 开始,您可以使用自动文件描述符分配,在这种情况下,您不需要eval

file=a
exec {id}<>"$file"

Then you can use it like this:

然后你可以像这样使用它:

echo  test >&${id}

or:

或者:

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

回答by jan

I found the discussion in the answer of tobias.pal very interesting: https://stackoverflow.com/a/32689974/1184842

我发现tobias.pal的回答中的讨论很有趣: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)

这是行不通的,因为exec {FD}> file.${FD}$FD 的所有值都是相同的描述符,对吗?(德尼奥·马里兹)

I solved this by using an array as stated by Drew Chapin:

我通过使用 Drew Chapin 所述的数组解决了这个问题:

#!/bin/bash
# global variables for temp file handling
declare -a TEMPORARY_FILES_WRITE;
declare -a TEMPORARY_FILES_READ;

function createTempFile() {
    local usecase=""
    local id=""
    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.

这将输出 50。