【发布时间】:2016-02-25 01:21:32
【问题描述】:
我正在尝试遍历 Bash 数组(包列表),并在循环内使用一些 expect 命令来自动化交互式输入。
脚本应询问几个问题以提供密码短语和存储 rpm 包的路径。之后,数组由列表和循环器组成,用于签署 rpm 包(使用expect 代码自动输入密码)并将包推送到 RedHat Satellite。
循环不起作用,我无法将数组正确导出到expect。
```根据 Glenn 的建议更新了代码。
#/bin/bash
read -srep $'Please, insert passphrase:\n\n' PASSPHRASE
read -rep $'Please, provide a path for your package(s) being signed and pushed:\#n\n' PPATH
mapfile PLIST < <(find "$PPATH" -name "*.rpm")
export PASSPHRASE PLIST
for package in "${PLIST[@]}"
do
echo "$package"
export package
/usr/bin/expect<<EOF
set force_conservative 0 ;
;
set filename [lindex $argv 0]
set timeout -1
spawn rpm --resign $::env(package)
close $spawn_id
expect "*phrase:"
send -- "$PASSPHRASE\r"
expect eof
EOF
done
unset PASSPRASE
这是我得到的:
spawn rpm --resign x86_64/myrpm9.rpm
expect: spawn id exp4 not open
while executing
"expect "*phrase:""
spawn rpm --resign x86_64/myrpm1.rpm
expect: spawn id exp4 not open
while executing
"expect "*phrase:""
更新 2: 我继续努力解决这个问题。为了方便起见,我对 bash 变量进行了硬编码。我几乎想通了,显然不工作的东西仍然是来自 bash 的环境变量。期望脚本可以正确解释它,但是如果我用文件名替换变量,它会以某种方式给出“无法访问文件”(意味着它找不到文件)。
#/bin/bash
PASSPHRASE="IamPass"
PPATH="/home/user/strcompat-debuginfo-0.9.2.7-1.x86_64.rpm"
mapfile PLIST < <(find "$PPATH" -name "*.rpm")
export PASSPHRASE PLIST
for package in "${PLIST[@]}"
do
export package
/usr/bin/expect -d <<EOF
set force_conservative 0 ;
set filename [lindex $argv 0]
set timeout -1
spawn rpm --resign $::env(package)
expect { "*phrase:"
{ send -- "$PASSPHRASE\r"; incr i; exp_continue }
eof exit
}
EOF
done
调试模式下的输出:
[user@linuxbox ~]$ ./script.sh
expect version 5.44.1.15
argv[0] = /usr/bin/expect argv[1] = -d
set argc 0
set argv0 "/usr/bin/expect"
set argv ""
executing commands from command file
spawn rpm --resign /home/user/strcompat-debuginfo-0.9.2.7-1.x86_64.rpm
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {18487}
expect: does "" (spawn_id exp4) match glob pattern " "*phrase:"\n\t{ send -- "IamPass\r"; incr i; exp_continue }\n\t# close \n\teof exit\n\t"? no
cannot access file /home/user/strcompat-debuginfo-0.9.2.7-1.x86_64.rpm
expect: does "cannot access file /home/user/strcompat-debuginfo-0.9.2.7-1.x86_64.rpm\r\n\r\n" (spawn_id exp4) match glob pattern " "*phrase:"\n\t{ send -- "IamPass\r"; incr i; exp_continue }\n\t# close \n\teof exit\n\t"? no
expect: read eof
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "cannot access file /home/user/strcompat-debuginfo-0.9.2.7-1.x86_64.rpm\r\n\r\n"
正常输出:
[user@linuxbox ~]$ ./script.sh
spawn rpm --resign /home/user/strcompat-debuginfo-0.9.2.7-1.x86_64.rpm
cannot access file /home/user/strcompat-debuginfo-0.9.2.7-1.x86_64.rpm
[user@linuxbox ~]$ echo $?
0
您的任何建议将不胜感激! 提前致谢!
【问题讨论】:
-
Benjamin W.,感谢您的编辑。
-
你能解释一下为什么在生成后立即关闭 spawn_id 吗?
-
嗯,没错。我只是把它放在那里,因为如果我把它放在 send -- "$PASSPHRASE\r" 之后它会失败:spawn rpm -- resign x86_64/myrpm0.rpm 无法访问文件 x86_64/myrpm0.rpm 发送:spawn id exp4 not openwhile execution "send -- "plain_passphrase_here!\r"
-
嗨格伦。再次玩这个脚本(需要完成它),虽然没有运气。您能否就错误和解决方案提出建议?谢谢!
标签: arrays bash loops expect eof