将参数传递给执行脚本时来砸 [英] Passing parameters to bash when executing a script
问题描述
我试图远程Linux机器上从Windows运行一个bash shell脚本。
I am trying to execute a bash shell script from Windows on a remote Linux machine.
我使用C#和 SSH.Net 库。
脚本住在Windows中,不能在Linux机器上安装。我用它加载脚本作为字符串'File.ReadAllText(...)脚本读取。使用SSH.Net我再执行在Linux上的脚本:
The scripts live on the windows box and cannot be installed on the Linux machine. I read in the script using 'File.ReadAllText(...)' which loads in the script as string. Using SSH.Net I then execute the script on Linux:
SshCommand cmd;
using (var client = new SshClient(ConnectionInfo))
{
client.Connect();
cmd = client.CreateCommand(string.Format("sh -x -s < {0}", script));
cmd.Execute();
client.Disconnect();
}
return cmd.ExitStatus;
这工作时,脚本没有任何参数。但是,如果我需要一些论据下面执行脚本传递,但PARAMS缺失:
This works when the script doesn't have any parameters. But if I need to pass in some arguments the following executes the script but the params are missing:
cmd = client.CreateCommand(string.Format("sh -x -s p={0} < {1}", parameterString, script));
示例脚本是:
#!/bin/bash
# check-user-is-not-root.sh
echo "Currently running $0 script"
echo "This Parameter Count is [$#]"
echo "All Parameters [$@]"
输出是:
Currently running bash script
This Parameter Count is [0]
All Parameters []
更新
有关我现在在经批准的答案使用curl(如<一个href=\"http://stackoverflow.com/questions/4642915/passing-parameters-to-bash-when-executing-a-script-fetched-by-curl\">here:).
For now I am using curl (like in the approved answer here:).
cmd = client.CreateCommand(string.Format("curl http://10.10.11.11/{0} | bash -s {1}", scriptName, args))
但我仍然认为有必须与参数的bash脚本来读取并在SSH在远程Linux机器运行的方式。
But I still think there must be a way to read in a bash script with arguments and run it across ssh on a remote Linux box.
任何帮助将大大AP preciated。
Any help would be greatly appreciated.
推荐答案
也许你最好还是运行 SSH用户@主机命令;另一个;更多的
或者如果你真的要使用一个明确的 SH
,像 SSH用户@主机SH -c'命令;另外,更多的
。这也应该拯救你把剧本中的临时文件。
Perhaps you are better off running ssh user@host 'command; another; more'
or if you really have to use an explicit sh
, something like ssh user@host "sh -c 'command; another; more'"
. That should also save you from putting the script in a temporary file.
这篇关于将参数传递给执行脚本时来砸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!