
我在 PowerShell 中使用 plink 和 pageant 透過 ssh 連接到 Linux 電腦並在其上執行命令。在 PS 腳本中,我嘗試提供一個 unix 腳本檔案來 plink,如下所示。
測試.ps1:
$remote_cmd_file = "test.sh"
pageant $priv_key
plink $usr@$dom -batch -m $remote_cmd_file
(變數 $usr、$dom 和 $priv_key 已正確定義,但此處未給出。)
“test.sh”檔案的全部內容是:
cd /
pwd
我得到輸出:
plink : bash: cd: command not found
At C:\workspace\test.ps1:8 char:1
+ plink $usr@$dom -batch -m $remote_cmd_file
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (bash: cd: command not found:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
/my/remote/home/directory
很明顯,第二行執行了,但第一行沒有執行。
如果我以空白行開始“test.sh”,我會收到類似的訊息,但兩個命令都會執行。我得到輸出:
plink : bash: : command not found
At C:\workspace\test.ps1:8 char:1
+ plink $usr@$dom -batch -m $remote_cmd_file
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (bash: : command not found:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
/
最後,當我用這樣的行開始腳本時
#!/bin/bash
然後我得到
plink : bash: #!/bin/bash: No such file or directory
At C:\workspace\test.ps1:8 char:1
+ plink $usr@$dom -batch -m $remote_cmd_file
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (bash: #!/bin...le or directory:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
/
但腳本中其他任何地方以 # 開頭的行都會被正確解釋為註釋,並且不會產生任何輸出。
這似乎是 plink 或 bash 的問題,但我找不到任何有關它的現有問題。作為一種解決方法,我似乎可以透過將第一行留空來確保所有命令的執行,但警告很煩人,我想知道是什麼導致了這個怪癖。
如果它有幫助的話,我的最終目標是讓這個腳本透過slurm 在遠端電腦上提交作業,轉換一些文件,然後在同一個PowerShell 腳本中處理本地電腦上的這些文件,我可以在它們上調用各種Windows 程式從命令列。
答案1
問題出在 shell 腳本檔案本身上,該檔案以位元組順序標記開始,幹擾了對其第一行的解釋。建立一個不以該字元開頭的檔案解決了該問題。