scripting - 相当于powershell中的bash "expect"

标签 scripting powershell

我正在使用 powershell 运行另一个 powershell 脚本,在某些时候另一个脚本要求一些输入,我希望能够读取来自另一个脚本的输出并基于该脚本的输入。类似于您可以在 bash 上使用 expect 执行的操作。

有任何想法吗?

谢谢

最佳答案

您只需在您的 powershell 中使用 Expect 程序。有用。 Powershell 也是一个shell,你可以运行powershell 编写的代码,它调用bash 代码,它再次调用powershell。

波纹管是一个测试,它通过了。

It "can work with tcl expect" {
    $bs = @'
    echo "Do you wish to install this program?"
    select yn in "Yes" "No"; do
    case $yn in
        Yes ) echo "install"; break;;
        No ) exit;;
    esac
done
'@

$bsf = New-TemporaryFile
$bs | Set-Content -Path $bsf

$tcls = @'
#!/bin/sh
# exp.tcl \
exec tclsh "$0" ${1+"$@"}

package require Expect
set timeout 100000
spawn {spawn-command}
expect {
    "Enter password: $" {
        exp_send "$password\r"
        exp_continue
    }
    "#\? $" {
            exp_send "1"
    }
    eof {}
    timeout {}
}
'@

$tclf = New-TemporaryFile
$tcls -replace "{spawn-command}",("bash",$bsf -join " ") | Set-Content -Path $tclf
"bash", $tclf -join " " | Invoke-Expression

Remove-Item $bsf
Remove-Item $tclf
}

让我解释一下这个测试。
  • 创建一个需要输入的 bash 文件。
  • 创建一个 tcl 文件,该文件调用在第一步中创建的 bash。
  • 从 powershell 调用 tcl 程序,它可以工作,不会等待输入。
  • 关于scripting - 相当于powershell中的bash "expect",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7800368/

    相关文章:

    bash - 从 Bash 命令在文本文件中查找和替换

    linux - 使用 gnu parallel 执行 for 循环

    c# - 从 C# Azure Function Apps 调用 PowerShell

    powershell - PowerShell属性问题

    windows - 将文件夹拆分为一定大小的较小文件夹

    javascript - 在网站中自动选择关键字

    perl - 什么会导致 Perl 转储核心?

    powershell - 如何平衡有关在 Windows 上安装 Scoop 的安全性(PowerShell 执行策略更改)问题?

    powershell - 我的 if 循环正在生效,即使它不应该生效

    linux - 如何在 shell 脚本中使用 line 作为可选函数?