如何將指令的標準輸出傳送到 Expect 輸入?

如何將指令的標準輸出傳送到 Expect 輸入?

我想編寫一個 shell + Expect 腳本,透過 LastPass CLI 實用程式自動填入我的密碼lpass。我不確定如何完成將返回的密碼發送lpass到期望腳本中的密碼輸入。

到目前為止,expect 腳本看起來像這樣:

# The beginning isn't important
expect -exact "\r
Please enter your username and password.\r
Username:"
send -- "my-username\r"
expect -exact "my-username\r
Password:"
send -- $(lpass show --password service\ im\ connecting\ to)
expect -exact "\r
# The rest of the expect script follows

我不確定 $(...) 中的位實際上應該如何寫...

答案1

您希望exec在腳本中使用函數來獲得與shell 中expect相同的行為$(...)

請參閱下面的範例:
讓我們使用下面的外部程序4expect.sh,我們將不使用腳本來提供expect

#!/bin/sh
# Test program : let set filename as "4expect.sh"
# in the same directory where expect script will work

echo; read -p 'question: ' answer
echo "Got answer:>${answer}<"

這裡我們的expect腳本將等待來自外部程式的“問題”,並向其提供當前目錄中將由's使用的所有文件(獲取外部程式ls和的輸出) :egrepexpectsend

#!/usr/bin/expect -f

spawn -noecho ./4expect.sh
expect -re "question" { send -- [exec ls -la . | egrep "^total" ]\r }
interact

puts "\nDone.\n"
exit

相關內容