using System.Text.RegularExpressions;
internal class Program
{
static List<DirectoryInfo> path = null!;
private static void Main(string[] args)
{
path =
[..Environment.GetEnvironmentVariable("PATH")!.Split(Path.PathSeparator)
.Select(p => new DirectoryInfo(p))];
while (true)
{
Console.Write("$ ");
string? commandString = Console.ReadLine();
Command command = new(commandString);
command.CommandAction.Invoke();
}
}
class Command
{
public string Name { get; }
public IEnumerable<string> Args { get; }
public string Type { get; } = "a shell builtin";
public Action CommandAction { get; }
public Command(string? commandString)
{
var splitString = Regex.Matches(commandString, @"[^\s""']+|'([^']*)'|[\s]*")
.Cast<Match>()
.Select(m => m.Value);
splitString = splitString.Select(s => (string.IsNullOrWhiteSpace(s) && s.Length > 1) ? " " : s)
.Select(s => s.Trim('\''));
Name = splitString.First();
Args = splitString.Skip(2);
foreach (var dir in path)
{
string fullPath = Path.Combine(dir.FullName, Name);
if (File.Exists(fullPath))
{
Type = fullPath; // <<<< bu yerda topilgan yo‘l /bin/cat bo‘ladi
CommandAction = () =>
{
var process = Process.Start(new ProcessStartInfo(Name, Args.Where(x => !string.IsNullOrWhiteSpace(x))));
process?.WaitForExit();
};
return;
}
}
switch (Name)
{
case "exit":
CommandAction = () => Environment.Exit(0);
return;
case "echo":
CommandAction = () => Console.WriteLine(string.Join("",Args));
return;
case "cat":
CommandAction = () =>
{
if (Args.Count() == 0)
{
Console.WriteLine("cat: missing argument");
return;
}
foreach (var arg in Args)
{
if (File.Exists(arg))
{
Console.Write(File.ReadAllText(arg));
}
}
};
return;
case "type":
Args = Args.Where(a => !string.IsNullOrWhiteSpace(a));
CommandAction = () =>
{
Command typedCommand = new(Args.First());
if (typedCommand.Type == "invalid")
Console.WriteLine($"{typedCommand.Name}: not found");
else
Console.WriteLine($"{typedCommand.Name} is {typedCommand.Type}");
};
return;
case "pwd": // print working directory
Args = Args.Where(a => !string.IsNullOrWhiteSpace(a));
CommandAction = () =>
Console.WriteLine(Directory.GetCurrentDirectory());
return;
case "cd":
Args = Args.Where(a => !string.IsNullOrWhiteSpace(a));
CommandAction = () =>
{
if (Args.Count() == 0)
{
Console.WriteLine("cd: missing argument");
return;
}
else
{
var target = Args.First();
if (target == "..")
{
Directory.SetCurrentDirectory(
Directory.GetParent(
Directory.GetCurrentDirectory())!.FullName);
}
else if (target == "~")
{
var home = Environment.GetFolderPath(
Environment.SpecialFolder.UserProfile);
Directory.SetCurrentDirectory(home);
}
else if (target.StartsWith("~/"))
{
var home = Environment.GetFolderPath(
Environment.SpecialFolder.UserProfile);
Directory.SetCurrentDirectory(Path.Combine(home, target[2..]));
}
else
{
var dir = new DirectoryInfo(target);
if (dir.Exists)
{
Directory.SetCurrentDirectory(dir.FullName);
}
else
{
Console.WriteLine($"cd: {target}: No such file or directory");
}
}
}
};
return;
default:
Args = Args.Where(a => !string.IsNullOrWhiteSpace(a));
foreach (var dir in path)
{
string path = Path.Combine(dir.FullName, Name);
if (File.Exists(path))
{
Type = path;
CommandAction = () =>
{
var process = Process.Start(new ProcessStartInfo(Name, Args.Where(x => !string.IsNullOrWhiteSpace(x))));
process?.WaitForExit();
};
return;
}
}
CommandAction = () => Console.WriteLine($"{Name}: command not found");
Type = "invalid";
return;
}
}
}
}```
Hey @SardorSohinazarov, could you let us know which stage you’re on and share any error messages you’re seeing? That’ll help us debug more effectively.