What is bash's "| while read file do" and "done" in Powershell ?
For example, in bash the script is
cd tmp &&
inotifywait -me close_write,create,move --format %f *.tmp | while read file
do rclone move "$file" dropbox:
done
What should it be like in Powershell?
I only found that && could be replaced with -and
How about the rest?
Use the
ForEach-Objectcmdlet, which processes each input object one by one.With external programs, it is each output line that constitutes an object, i.e. PowerShell streams the (stdout) output from external programs line by line through its pipeline.
Note:
In PowerShell,
*.tmponly expands to the list of matching file names on Unix-like platforms, which requires use of PowerShell (Core) 7+, the modern, cross-platform edition of PowerShell.*.tmpwith(Get-Item *.tmp).Namethere. (For more control over what gets matched, you can useGet-ChildIteminstead ofGet-Item).The automatic
$_variable is used to refer to the input line at hand.Enclosing variable references in
"..."is not needed in PowerShell (in the case at hand,$_is sufficient - no need for"$_"), even if the values contain spaces (PowerShell neither uses word-splitting nor most of the other shell expansions that Bash performs).As for the
cd tmp && ...part, i.e. use of&&:You can use this syntax as-is in PowerShell (Core) 7+, which supports
&&and||, the pipeline chain operators,However, you can not in Windows PowerShell (and
-andis not a substitute)[1]; there, use something likecd -ErrorAction Stop tmp(cdis a built-in alias ofSet-Location) orcd tmp; if ($?) { ... }.[1] See this answer for why.