我的目标是让一个程序循环遍历两个文件,并使用来自file1和file2的所有行组合计算一个单独的shell脚本。我通过将eval行移出while循环来验证它是否有效。
#!/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循环的子shell中调用它时,它会提示输入密码,但并没有按预期将其放入。
如何修改第一个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
关于linux - Bash:我无法在双while循环内运行eval命令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35807283/