Execute Script With Arguments

Aug 22, 2011 at 6:01 AM

I am trying to execute a bash shell script from Windows on a remote Linux machine.

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))
                cmd = client.CreateCommand(string.Format("sh -x -s < {0}", script));                
            return cmd.ExitStatus;

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));

The sample script is:

    # check-user-is-not-root.sh
    echo "Currently running $0 script"
    echo "This Parameter Count is        [$#]"
    echo "All Parameters            [$@]"

The output is:

    Currently running bash script
    This Parameter Count is        [0]
    All Parameters            []

I am a scripting newbie so any pointers would be greatly appreciated.

Aug 23, 2011 at 12:43 PM

I think the problem here is not with the argument but with "<" command.

What you saying here is to get input from some other file but I have a feeling that this feature is only available in shell scenarios.

I am not sure about it however, its just my guess at the moment.