コマンドの stdout を Expect 入力に送信するにはどうすればよいですか?

コマンドの stdout を Expect 入力に送信するにはどうすればよいですか?

LastPass CLI ユーティリティを使用してパスワードを自動入力するシェル + expect スクリプトを作成したいと考えています。expectスクリプトのパスワード入力に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スクリプト内で関数を使用してシェルexpectと同じ動作を実現したい$(...)

以下の例を参照してください。スクリプトなしでフィードする以下の
外部プログラムを使用します。4expect.shexpect

#!/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外部プログラムからの「質問」を待機し、現在のディレクトリにある合計ファイル(外部プログラムの出力を取得しls、 )をによってegrep使用されるスクリプトを示します。expectsend

#!/usr/bin/expect -f

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

puts "\nDone.\n"
exit

関連情報