# This code is worked and returned true value but not pass

**URL:** <https://forum.codecrafters.io/t/this-code-is-worked-and-returned-true-value-but-not-pass/12436>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [June 21, 2025, 10:19am UTC](https://forum.codecrafters.io/t/this-code-is-worked-and-returned-true-value-but-not-pass/12436 "2025-06-21T10:19:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SardorSohinazarov](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/sardorsohinazarov/32/13197_2.png) [@SardorSohinazarov](https://forum.codecrafters.io/u/SardorSohinazarov)\
**Post date:** [June 21, 2025, 10:19am UTC](https://forum.codecrafters.io/t/this-code-is-worked-and-returned-true-value-but-not-pass/12436/1 "2025-06-21T10:19:34Z")

</div>

````using
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;
            }
        }
    }
}```
````

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [June 21, 2025, 2:07pm UTC](https://forum.codecrafters.io/t/this-code-is-worked-and-returned-true-value-but-not-pass/12436/3 "2025-06-21T14:07:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [June 28, 2025, 7:09pm UTC](https://forum.codecrafters.io/t/this-code-is-worked-and-returned-true-value-but-not-pass/12436/4 "2025-06-28T19:09:40Z")

</div>

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [July 3, 2025, 7:10pm UTC](https://forum.codecrafters.io/t/this-code-is-worked-and-returned-true-value-but-not-pass/12436/5 "2025-07-03T19:10:09Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
