我正在尝试使用 Expect 命令,一切正常,直到我尝试将命令的结果通过管道传输到 Gzip 文件中。这是 bash 文件的片段:
XYZ=$(expect -c "
spawn python log-connector.py -s $strt -e $end -i 600 -a https://server:9000 -u someaccount -f ./conf/firewall-fields2.txt -q ./conf/query.txt -z | gzip >> /data/sources/results-$strt2-$end2.json.gz
expect \"Password:\"
send \"$pass\r\"
interact
")
echo "$XYZ"
$start
- 变量(开始日期)
$end
- 变量(结束日期)
$strt2
- 变量(开始日期为 00:00)
$end2
- 变量(结束日期为 23:59)
$pass
- 用户先前输入的密码。
如果我删除
| gzip >> /data/sources/results-$strt2-$end2.json.gz
从上面来看,它按预期工作。当我添加它时,我收到以下错误:
send: spawn id exp7 not open
while executing
usage: API [-h] [-s START] [-e END] [-d DAY] [-i INTERVAL]
[-a LOGGER] [-l LOGFILE] [-f FIELDS] [-q QUERY]
[-o OUTPUT] [-t FORMAT] [-u USER] [-p PASS] [-z]
API: error: unrecognized arguments: | gzip >> /data/sources/results-20180604-20180604.json.gz
答案1
您可能想使用 shell 来解释管道和重定向:我将使用 Heredoc 使引用更容易
XYZ=$(expect <<END_EXPECT
set timeout -1
spawn sh -c {python log-connector.py -s $strt -e $end -i 600 -a https://server:9000 -u someaccount -f ./conf/firewall-fields2.txt -q ./conf/query.txt -z | gzip >> /data/sources/results-$strt2-$end2.json.gz}
expect "Password:"
send "$pass\r"
expect eof
END_EXPECT
)
echo "$XYZ"