This project is read-only.

Executing grep command

Mar 4, 2014 at 3:37 AM
I am using the ssh.net library and I am trying to run the following command:
string theCommand=grep <stringToLookFor> <filePathAndName>
client.RunCommand(theCommand);

The process hangs.

Then I tried using asynch:
SshCommand cmd=client.CreateCommand("grep "+stringInFile+" " + directory+fileName);
var asynch=cmd.BeginExecute();
while (!asynch.IsCompleted)
        {
           System.Threading.Thread.Sleep(100);

        }
        cmd.EndExecute(asynch);
And it does not return neither.

Any ideas on how I can run this grep command?
Thanks.
Mar 4, 2014 at 6:48 AM
grep waits if the second argument is empty.

$ grep bla
=> hangs till signal
$ grep bla bla
=> instant return

I guess your command is not correct. Ensure you are calling grep correctly.

Btw.: Please use the code tag to paste code for readability
Mar 4, 2014 at 2:44 PM
Hi,
I have the two parameters: grep test /dir/to/file.txt. I ran my code in debug mode and it looks exactly as it does when I hard code the values.
Still hanging.
Mar 4, 2014 at 2:55 PM
I can't reproduce it, sorry.

Works for me.
            try
            {
                client.Connect();

                SshCommand cmd = client.CreateCommand("grep bla /tmp/foo");
                var asynch = cmd.BeginExecute();
                while (!asynch.IsCompleted)
                {
                    Console.WriteLine("Waiting...");
                    System.Threading.Thread.Sleep(100);
                }
                cmd.EndExecute(asynch);

                Console.WriteLine("Exit-Status: " + cmd.ExitStatus);
                Console.WriteLine("Result: " + cmd.Result);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
Mar 4, 2014 at 2:57 PM
You are right, I know what it is now. I have an enter in my data. I am sorry. It was just not visible at all. Thanks.