Skip to main content
 首页 » 编程设计

macos之终端循环适用于 mac 但不适用于 pc python

2024年10月24日18youxin

我有一个在 mac 上使用的终端循环命令:

for i in {1..30}; do python Rev1stRT.py A${i}F.txt FirstRevRtA${i}.txt; done 

这适用于名为 A1F.txt A2F.txt A3F.txt 等等的一堆文件 我已经为学生发送命令(她使用电脑),但她收到以下错误:

Missing opening '(' after keyword 'for'. 
At line:1 char:5 
for <<<< i in {1..30}; do python Rev1stRT.py A${i}F.txt FirstRevRtA${i}.txt; done 
CategoryInfo    : ParserError: (OpenParenToken:TokenId) [], ParentContainsErrorRecordException 
FullyQualifiedErrorId: MissingOpenParenthesisAfterKeyword   

但是,如果她尝试将同一脚本作为单个命令运行(即 python Rev1stRT.py A2F.txt FirstRevRtA2.txt),一切正常(这意味着她拥有数据文件和脚本文件按照我的指示放在正确的文件夹中)

更新(运行中的 Powershell 循环):

 $dirs = Get-ChildItem $directory | where {$_.psiscontainer -eq $false}  
 
 foreach ($d in $dirs){ python script.py $d "${d}_output.txt";}` 

请您参考如下方法:

Windows Powershell 中的循环与 bash 的语法不同,后者是 Mac 终端的默认 shell。

假设安装了 Python 并将其添加到 Path 环境变量中,这样的事情会起作用:

for ($i=1; $i -le 30; $i++) { 
    python Rev1stRT.py "A$iF.txt" "FirstRevRtA$i.txt"; 
} 

引用:https://social.technet.microsoft.com/wiki/contents/articles/4542.powershell-loops.aspx