Bash:我无法 运行 双 while 循环中的 eval 命令

Bash: I am unable to run the eval command inside a double while loop

我的目标是有一个程序循环遍历两个文件并评估一个单独的 shell 脚本,其中包含来自 file1 和 file2 的所有行组合。我通过将它移出 while 循环来验证我的 eval 行是否有效。

#!/bin/bash
while read line1
do 
    while read line2
    do 
        eval "ssh_connect $line1 $line2"
    done < $FILE2
done < $FILE1

ssh_connect 根据命令行参数中提供的用户名和密码创建新的 ssh 连接。

set username [lindex $argv 0];
set password [lindex $argv 1];
puts "$password"
puts "$username"
spawn ssh $username@<location>.com
expect "assword:"
send "$password\r"
interact

我已经验证了上面的脚本可以正常工作。但是,当我从 while 循环的 sub-shell 内部调用它时,它提示输入密码并且没有按预期输入。

如何修改我的第一个 shell 脚本,以便它正确评估第二个脚本

问题是 Expect 脚本中的 interact 切换为从标准输入读取。由于此时 stdin 被重定向到 $FILE2,因此它会读取该文件中的所有内容。当内部循环重复时,文件中没有任何内容,因此循环终止。

您需要保存脚本的原始标准输入,并将 ssh_connect 的输入重定向到那个。

#!/bin/bash
exec 3<&0 # duplicate stdin on FD 3
while read line1
do 
    while read line2
    do 
        eval "ssh_connect $line1 $line2" <&3
    done < $FILE2
done < $FILE1