Bash 程序似乎不接受 EOF 输入,而是卡住了

Bash program appears to not accept EOF input and instead gets stuck

提问人:133U 提问时间:1/6/2023 最后编辑:133U 更新时间:1/9/2023 访问量:73

问:

我想自动化我的 Mint 安装。我的 Mint 安装的一个主要部分是使用 LUKS 分区对整个磁盘进行加密。为了能够通过我的Yubikey解锁我的设备,我必须运行以下行:

yubikey-luks-enroll -d /dev/sda3 -s 7

您可以通过安装获得并且工作正常。但是,使用 EOF 运行此命令不起作用。sudo apt install yubikey-luks

运行上述命令时,系统会要求我输入密码两次,然后输入 LUKS 部分的密码:

$ sudo yubikey-luks-enroll -d /dev/sda3 -s 7
setting disk to /dev/sda3.
setting slot to 7.
This script will utilize slot 7 on drive /dev/sda3.  If this is not what you intended, exit now!
Adding yubikey to initrd
Please enter the yubikey challenge password. This is the password that will only work while your yubikey is installed in your computer:
Please enter the yubikey challenge password again:
Please provide an existing passphrase. This is NOT the passphrase you just entered, this is the passphrase that you currently use to unlock your LUKS encrypted drive:

我可以使用以下 bash 脚本自动执行此步骤:

read PART
read -s DISKPWD
read -s PWD1
read -s PWD2

sudo yubikey-luks-enroll -d $PART -s 7 <<-EOF
$PWD1
$PWD2
$DISKPWD
EOF

不幸的是,这只会导致以下输出:

setting disk to /dev/sda3.
setting slot to 7.
This script will utilize slot 7 on drive /dev/sda3.  If this is not what you intended, exit now!
Adding yubikey to initrd

更糟糕的是,终端卡住了 - 你可以输入东西,但它没有效果。只需 CTRL + Z 即可。

如果有人想知道,我必须使用相同的凭据设置多个 Yubikey,这就是我想自动化该过程的原因。

我还尝试在没有EOF的情况下在终端中运行该命令,这工作正常。在终端中使用 EOF 运行命令会导致与上述相同的错误。删除 Bash 文件中的 EOF 时,该命令有效。

我不是一个有经验的 Linux 用户。也许这只是一个简单的错误。我希望一切都是可以理解和清楚的。

问候 133U

bash ubuntu eof linux-mint yubikey

评论

2赞 Brian61354270 1/6/2023
欢迎来到 StackOverflow!这个问题可能更适合超级用户,或者可能更适合 Unix 和 Linux SE服务器故障。StackOverflow 用于特定的编程问题,而这更像是一个使用问题。否则就是好问题!yubikey-luks-enroll
1赞 jhnc 1/6/2023
为什么通过程序请求数据并传递给程序比程序直接请求更自动化?read
1赞 Gordon Davisson 1/6/2023
我怀疑这是直接从终端读取,而不是从标准输入读取。检查其文档(和/或源代码)。顺便说一句,您所说的“EOF”被正确地称为“here-document”;“EOF”只是一个任意分隔符。yubikey-luks-enroll
0赞 Eric Marceau 1/8/2023
这听起来很愚蠢,但是您是否尝试过没有“-”的“<<EOF”?我总是使用“EnDoFiNpUt”作为此处文档边界的字符串。
0赞 133U 1/9/2023
嘿,谢谢你所有的回答!正如 Brian 所指出的,我将切换到超级用户(superuser.com/questions/1761885/...)。与此同时,我发现其他安装程序(如 Anaconda 安装程序)的行为也相同。所以也许戈登是对的。是否有解决方法可以将输入输入到终端而不是 std-in?我想这取决于终端?我在 mint 中使用 std 终端。如果没有EOF前面的“-”,它不起作用,但感谢您的建议:)

答:

0赞 jhnc 1/9/2023 #1

yubikey-luks-enroll 是一个 shell 脚本。

要读取密码,它会调用 ./lib/cryptsetup/askpass

因此,您可以简单地编写一个修改版本,替换调用。askpass

请注意安全地处理明文密码。