如何将以下bash语句翻译为PowerShell?
<tr>
该语句关闭docker日志,直到它找到文本&#34; Initialization Complete&#34;,然后允许脚本继续。
我已经做到这一点,但我不确定如何在找到文本后继续执行脚本。
( docker-compose -f docker-compose.yml logs -f & ) | grep -q "Initialization Complete"
答案 0 :(得分:2)
通常,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 }