如何在Powershell中使用bash的“| while read file do”和“done”?

问题描述 投票:0回答:1

Powershell 中 bash 的“| while read file do”和“done”是什么?

例如,在 bash 中,脚本是

cd tmp &&
inotifywait -me close_write,create,move --format %f *.tmp | while read file
do  rclone move "$file" dropbox:
done

Powershell 中应该是什么样子?

我只发现&&可以用-and代替 剩下的呢?

bash powershell syntax inotify inotifywait
1个回答
0
投票

使用

ForEach-Object
cmdlet,它会一一处理每个输入对象。

对于 外部程序每条输出行构成一个对象,即 PowerShell 通过其管道逐行传输来自外部程序的(标准输出)输出。

inotifywait -me close_write,create,move --format %f *.tmp | ForEach-Object { rclone move $_ dropbox: }
注意使用

自动$_

变量
来引用手头的输入行。

另请注意,在 PowerShell 中,不需要将变量引用括在

"..."

 中(在当前情况下,
$_ 就足够了 - 不需要 "$_"
),即使值包含空格(PowerShell 也不使用单词) -分裂也不是 Bash 的大多数其他 shell 扩展)。

© www.soinside.com 2019 - 2024. All rights reserved.