|
1 |
| -[](https://app.codecrafters.io/users/codecrafters-bot?r=2qF) |
| 1 | +# Build Your Own Shell |
2 | 2 |
|
3 |
| -This is a starting point for JavaScript solutions to the |
4 |
| -["Build Your Own Shell" Challenge](https://app.codecrafters.io/courses/shell/overview). |
| 3 | +This project is a custom implementation of a POSIX-compliant shell built in JavaScript. It supports executing external commands, built-in commands, and features like tab completion, command parsing, and redirection. |
5 | 4 |
|
6 |
| -In this challenge, you'll build your own POSIX compliant shell that's capable of |
7 |
| -interpreting shell commands, running external programs and builtin commands like |
8 |
| -cd, pwd, echo and more. Along the way, you'll learn about shell command parsing, |
9 |
| -REPLs, builtin commands, and more. |
| 5 | +## Features |
10 | 6 |
|
11 |
| -**Note**: If you're viewing this repo on GitHub, head over to |
12 |
| -[codecrafters.io](https://codecrafters.io) to try the challenge. |
| 7 | +- **Built-in Commands**: |
| 8 | + - `cd`: Change the current working directory. |
| 9 | + - `pwd`: Print the current working directory. |
| 10 | + - `echo`: Print arguments to standard output. |
| 11 | + - `exit`: Exit the shell. |
| 12 | + - `type`: Display information about a command (e.g., whether it's built-in or external). |
13 | 13 |
|
14 |
| -# Passing the first stage |
| 14 | +- **External Commands**: |
| 15 | + - Executes programs available in the system's `PATH`. |
15 | 16 |
|
16 |
| -The entry point for your `shell` implementation is in `app/main.js`. Study and |
17 |
| -uncomment the relevant code, and push your changes to pass the first stage: |
| 17 | +- **Tab Completion**: |
| 18 | + - Autocompletes built-in commands and external executables. |
| 19 | + - Handles cases where multiple matches exist, completing to the longest common prefix or displaying all matches. |
18 | 20 |
|
19 |
| -```sh |
20 |
| -git commit -am "pass 1st stage" # any msg |
21 |
| -git push origin master |
| 21 | +- **Redirection**: |
| 22 | + - Supports output redirection (`>`, `>>`) and error redirection (`2>`, `2>>`). |
| 23 | + |
| 24 | +- **Error Handling**: |
| 25 | + - Gracefully handles invalid commands and displays appropriate error messages. |
| 26 | + |
| 27 | +## Getting Started |
| 28 | + |
| 29 | +### Prerequisites |
| 30 | + |
| 31 | +- **Node.js**: Ensure you have Node.js (version 21 or higher) installed on your system. |
| 32 | + |
| 33 | +### Installation |
| 34 | + |
| 35 | +1. Clone the repository: |
| 36 | + ```bash |
| 37 | + git clone https://github.com/your-username/build-your-own-shell.git |
| 38 | + cd build-your-own-shell |
| 39 | + ``` |
| 40 | + |
| 41 | +2. Install dependencies |
| 42 | + ```bash |
| 43 | + npm install |
| 44 | + ``` |
| 45 | + |
| 46 | +### Running the Shell |
| 47 | +To start the shell, run: |
| 48 | + ```bash |
| 49 | + ./your_program.sh |
| 50 | + ``` |
| 51 | + |
| 52 | +You will see a prompt ($ ) where you can enter commands. |
| 53 | + |
| 54 | +### Usage |
| 55 | + |
| 56 | +#### Built-in Commands |
| 57 | + |
| 58 | +- cd <directory>: Change the current working directory. |
| 59 | +```bash |
| 60 | +$ cd /path/to/directory |
| 61 | +``` |
| 62 | + |
| 63 | +- pwd: Print the current working directory. |
| 64 | +```bash |
| 65 | +$ pwd |
22 | 66 | ```
|
23 | 67 |
|
24 |
| -Time to move on to the next stage! |
| 68 | +- echo <text>: Print text to standard output. |
| 69 | +```bash |
| 70 | +$ echo Hello, World! |
| 71 | +``` |
| 72 | + |
| 73 | +- exit: Exit the shell. |
| 74 | +```bash |
| 75 | +$ exit |
| 76 | +``` |
| 77 | + |
| 78 | +- type <command>: Display information about a command. |
| 79 | +```bash |
| 80 | +$ type echo |
| 81 | +echo is a shell builtin |
| 82 | +``` |
| 83 | + |
| 84 | +#### External Commands |
| 85 | +You can run any executable available in your system's PATH. For example: |
| 86 | +$ ls -l |
| 87 | +$ node --version |
| 88 | + |
| 89 | +#### Tab Completion |
| 90 | +Press <TAB> to autocomplete commands or filenames. |
| 91 | +If multiple matches exist, press <TAB> twice to display all matches. |
| 92 | + |
| 93 | +#### Redirection |
| 94 | +- Redirect standard output to a file: |
| 95 | +$ echo "Hello" > output.txt |
| 96 | + |
| 97 | +- Append to a file: |
| 98 | +$ echo "World" >> output.txt |
| 99 | + |
| 100 | +- Redirect standard error: |
| 101 | +$ ls nonexistentfile 2> error.log |
| 102 | + |
| 103 | +- Append standard error: |
| 104 | +$ ls nonexistentfile 2>> error.log |
| 105 | + |
| 106 | +### Development |
| 107 | + |
| 108 | +#### Project Structure |
| 109 | + |
| 110 | +- `app/main.js`: Entry point for the shell. |
| 111 | +- `app/InputHandler.js`: Handles user input and tab completion. |
| 112 | +- `app/OutputHandler.js`: Handles output and redirection. |
| 113 | +- `app/CommandRegistry.js`: Manages built-in and external commands. |
| 114 | +- `app/<CommandName>.js`: Implementations of built-in commands (e.g., CdCommand, PwdCommand). |
25 | 115 |
|
26 |
| -# Stage 2 & beyond |
| 116 | +#### Adding a New Built-in Command |
27 | 117 |
|
28 |
| -Note: This section is for stages 2 and beyond. |
| 118 | +- Create a new file in the app directory (e.g., MyCommand.js). |
| 119 | +- Extend the Command class and implement the execute method. |
| 120 | +- Register the command in main.js: |
29 | 121 |
|
30 |
| -1. Ensure you have `node (21)` installed locally |
31 |
| -1. Run `./your_program.sh` to run your program, which is implemented in |
32 |
| - `app/main.js`. |
33 |
| -1. Commit your changes and run `git push origin master` to submit your solution |
34 |
| - to CodeCrafters. Test output will be streamed to your terminal. |
|
0 commit comments