From a6701f16d40fec3da06fcbc1577a0382c87bb790 Mon Sep 17 00:00:00 2001 From: shaghayeghfar <146011477+shaghayeghfar@users.noreply.github.com> Date: Wed, 22 Jul 2026 13:00:59 +0100 Subject: [PATCH] implement-shell-tools are done --- implement-shell-tools/cat/cat.js | 48 ++++++++++++++++++++++ implement-shell-tools/ls/ls.js | 49 +++++++++++++++++++++++ implement-shell-tools/wc/wc.js | 68 ++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 implement-shell-tools/cat/cat.js create mode 100644 implement-shell-tools/ls/ls.js create mode 100644 implement-shell-tools/wc/wc.js diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 000000000..ffe23be65 --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,48 @@ +const fs = require("fs"); + +const args = process.argv.slice(2); + +let numberAllLines = false; +let numberNonBlankLines = false; +let startIndex = 0; +let lineNumber = 1; + +// Check for flags +if (args[0] === "-n") { + numberAllLines = true; + startIndex = 1; +} else if (args[0] === "-b") { + numberNonBlankLines = true; + startIndex = 1; +} + +// Read each file +for (let i = startIndex; i < args.length; i++) { + let fileName = args[i]; + + try { + let content = fs.readFileSync(fileName, "utf8"); + + let lines = content.split("\n"); + + for (let j = 0; j < lines.length; j++) { + let line = lines[j]; + + if (numberAllLines) { + console.log(lineNumber + " " + line); + lineNumber++; + } else if (numberNonBlankLines) { + if (line.trim() === "") { + console.log(line); + } else { + console.log(lineNumber + " " + line); + lineNumber++; + } + } else { + console.log(line); + } + } + } catch (error) { + console.log("Cannot read file: " + fileName); + } +} diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..ff668fbbf --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,49 @@ +const fs = require("fs"); + +const args = process.argv.slice(2); + +let onePerLine = false; +let showHidden = false; +let path = "."; // Current directory by default + +// Check for flags +for (let i = 0; i < args.length; i++) { + if (args[i] === "-1") { + onePerLine = true; + } else if (args[i] === "-a") { + showHidden = true; + } else { + path = args[i]; + } +} + +try { + // Check if the path is a file + if (fs.statSync(path).isFile()) { + console.log(path); + } else { + let files = fs.readdirSync(path); + + // Print each file + for (let i = 0; i < files.length; i++) { + let file = files[i]; + + // Skip hidden files unless -a is used + if (!showHidden && file.startsWith(".")) { + continue; + } + + if (onePerLine) { + console.log(file); + } else { + process.stdout.write(file + " "); + } + } + + if (!onePerLine) { + console.log(); + } + } +} catch (error) { + console.log("Cannot access: " + path); +} diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..1b27be980 --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,68 @@ +const fs = require("fs"); + +// Get arguments after "node wc.js" +const args = process.argv.slice(2); + +let countLines = false; +let countWords = false; +let countBytes = false; + +let files = []; + +// Read arguments +for (let arg of args) { + if (arg === "-l") { + countLines = true; + } else if (arg === "-w") { + countWords = true; + } else if (arg === "-c") { + countBytes = true; + } else { + files.push(arg); + } +} + +// If no flags are given, wc shows all three +if (!countLines && !countWords && !countBytes) { + countLines = true; + countWords = true; + countBytes = true; +} + +// Function to count one file +function countFile(fileName) { + const content = fs.readFileSync(fileName, "utf8"); + + let lines = content.split("\n").length - 1; + + let words = content + .trim() + .split(/\s+/) + .filter((word) => word.length > 0).length; + + let bytes = Buffer.byteLength(content); + + let result = ""; + + if (countLines) { + result += lines + " "; + } + + if (countWords) { + result += words + " "; + } + + if (countBytes) { + result += bytes + " "; + } + + result += fileName; + + console.log(result); +} + +// Run wc for every file + +for (let file of files) { + countFile(file); +}