带/ dev / stdin的陈旧文件描述符

时间:2014-07-03 23:51:12

标签: bash

我正在尝试编写一个脚本来循环.ssh/authorized_keys中的条目并使用它们执行操作,即打印指纹并将它们附加到新位置。这就是我到目前为止所做的:

echo "$SSH_KEYS" | while read key ; do 
    ssh-keygen -lf /dev/stdin <<< "$key"
    echo "$key" >> newplace
done

不幸的是,这给了我以下错误:

/dev/stdin: Stale file handle

我在Ubuntu 14.04内核3.13.0-24-generic上运行Bash 4.3.11。

在运行Bash 4.3.8的同一个内核上运行正常。此时更改我的Bash版本似乎不是一个选项,这是生产中某些内容的自动脚本。

我在another question here on StackOverflow中找到了此解决方案,但似乎无法使用此更高版本的Bash。

1 个答案:

答案 0 :(得分:1)

我认为你正走在正确的轨道上,但你想要的是:

while read key; do
    ssh-keygen -lf /dev/stdin <<< "$key"
    echo "$key" >> newplace
done < .ssh/authorized_keys

相反:

echo "$SSH_KEYS" | while read key ; do 
    ssh-keygen -lf /dev/stdin <<< "$key"
    echo "$key" >> newplace
done 

请注意,只需将文件直接送入echo循环的stdin,而不是管道while的输出。

这对我有用:

$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

我也在Ubuntu 14.04上,但似乎有人也可能看到了这个问题:https://github.com/docker/docker/issues/2067

解决方法是将每个密钥写入临时文件,处理它,然后删除该文件。

相关问题