Bash 在powershell中等待命令输出中的文本
如何将下面的bash语句转换为PowerShellBash 在powershell中等待命令输出中的文本,bash,powershell,Bash,Powershell,如何将下面的bash语句转换为PowerShell ( docker-compose -f docker-compose.yml logs -f & ) | grep -q "Initialization Complete" 该语句跟踪docker日志,直到找到文本“Initialization Complete”,然后允许脚本继续 我已经完成了这一步,但不确定在找到文本后如何继续执行脚本 docker-compose -f docker-compose.yml logs -f | O
( docker-compose -f docker-compose.yml logs -f & ) | grep -q "Initialization Complete"
该语句跟踪docker日志,直到找到文本“Initialization Complete”,然后允许脚本继续
我已经完成了这一步,但不确定在找到文本后如何继续执行脚本
docker-compose -f docker-compose.yml logs -f | Out-String -Stream | Select-String "Initialization Complete"
通常,PowerShell的
tail-f
等效于Get Content-Wait
但是,Bash子shell((…)
)与后台进程(&
)的巧妙组合并没有直接的PowerShell等价物
相反,您必须使用循环来监视PowerShell中的后台进程:
# Start the Docker command as a background job.
$jb = Start-Job { docker-compose -f docker-compose.yml logs -f }
# Loop until the data of interest is found.
while ($jb.HasMoreData) {
# Receive new data output by the background command, if any,
# and break out of the loop once the string of interest is found.
Receive-Job $jb -OutVariable output |
ForEach-Object { if ($_ -match "Initialization Complete") { break } }
# With a stream that is kept open, $jb.HasMoreData keeps reporting $true.
# To avoid a tight loop, we sleep a little whenever nothing was received.
if ($null -eq $output) { Start-Sleep -Seconds 1 }
}
# Clean up the background job, if it has completed.
if ($jb.Status -eq 'Complete') { Remove-Job $jb }