从windows机器中的power shell执行shell脚本

时间:2017-02-20 16:55:06

标签: linux bash shell

我正在尝试从Windows机器中的power shell执行shell脚本。所以开始编写shell脚本并使用下面的命令调用到linux框中:

PS>.\pscp.exe .\abc.sh root@1.2.3.4:/root` and gave the permissions through power shell by using below command  
PS>.\plink.exe -pw "L***e" root@1.2.3.4 -P 22 "chmod +x abc.sh"` after that verified that shell script copied or not  
PS>.\plink.exe -pw "L***e" root@1.2.3.4 -P 22 "ls"

所以它被复制了。之后当我使用以下命令执行时:

PS>.\plink.exe -pw "L***e" root@1.2.3.4 -P 22 "bash ./abc.sh"`

从某种意义上说它没有工作,如:

./abc.sh: line 4: $'\r': command not found ./abc.sh: 
line 5: syntaxerror near unexpected token `$'\r'' '/abc.sh: 
line 5: `function abc()
PS>.\plink.exe -pw "L***e" root@1.2.3.4 -P 22 "./abc.sh"
bash: ./abc.sh: /bin/bash^M: bad interpreter: No such file or directory

如果有任何有价值的建议值得赞赏并提前感谢......

2 个答案:

答案 0 :(得分:2)

我没有使用太多的windows文本编辑器,但是“^ M:糟糕的解释器:没有这样的文件或目录” - 表示有回车的windows版本。因此,将脚本文件转换为unix样式可能会有所帮助。

参考:https://kb.iu.edu/d/acux

答案 1 :(得分:0)

首先安装包

PS>.\plink.exe -pw "L***e" root@1.2.3.4 -P 22 "yum install dos2unix -y"

之后运行以下命令

    PS>.\plink.exe -pw "L***e" root@1.2.3.4 -P 22 "dos2unix abc.sh"
dos2unix: converting file abc.sh to UNIX format ...

之后,将执行权限分配给脚本文件

PS>.\plink.exe -pw "L***e" root@1.2.3.4 -P 22 "chmod + abc.sh"

最后给出脚本文件路径,然后执行脚本。

PS>.\plink.exe -pw "L***e" root@1.2.3.4 -P 22 "/root/abc.sh"