0

I use this command to track changes on a file on ubuntu:

tail -f /var/log/auth.log

as the file changes I see the output printed on the console. It is very nice.

My bash script therefore looks something like this:

#!/bin/bash

tail -fn0 /var/log/auth.log | \
while read line ; do
    x=$(echo "$line" | grep -o "sftp-server")

    # if line contains sftp-server then:
    if [ "$x" == "sftp-server" ]; then
        # ... do work with line <------------------
    fi

done

I need to run this bash script in mono (c#). So my question is how can I do that with c#? If I where to do something like the code in c# bellow will it be doing the same thing?

var fileStream = File.OpenRead("/var/log/auth.log");

while (true)
{
        Thread.Sleep(500);

        byte[] buffer = new byte[1024];

        StreamReader reader = new StreamReader(fileStream);

        if (fileStream.CanRead)
        {
            var line = reader.ReadLine();

            if (string.IsNullOrEmpty(line) == false)
            {
                    // do work with line.... <-----------------------
            }
        }                
}
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • `OpenRead` is equivalent to `new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);` which excludes other from opening to file for writes. Try opening the stream with FileShare.ReadWrite. – Mike Zboray Apr 06 '18 at 20:14
  • I guess you answered my question @spender. thanks for the help! – Tono Nam Apr 06 '18 at 20:15

0 Answers0