From 64e23bdf2b13476976d5e95915c0554460184ab0 Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Thu, 16 Jul 2026 14:08:41 +0100 Subject: [PATCH 1/2] Implement cat command with -n and -b support --- implement-shell-tools/cat/cat.js | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 implement-shell-tools/cat/cat.js diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 000000000..60048bf84 --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,50 @@ +const fs = require("fs"); + +const args = process.argv.slice(2); + +let flag = ""; +let lineNumber = 1; + +for (const arg of args) { + + // Check for flags + if (arg === "-n" || arg === "-b") { + flag = arg; + continue; + } + + // Read the file + const content = fs.readFileSync(arg, "utf8"); + + // split into lines + const lines = content.split("\n"); + + // Remove the extra empty line if the file ends with a new line + if (lines[lines.length - 1] === "") { + lines.pop(); + } + + + for (const line of lines) { + + if (flag === "-n") { + console.log(`${lineNumber}\t${line}`); + lineNumber++; + } + + else if (flag === "-b") { + + if (line === "") { + console.log(""); + } else { + console.log(`${String(lineNumber).padStart(6)} ${line}`); + lineNumber++; + } + + } + + else { + console.log(line); + } + } +} \ No newline at end of file From 47fe6338a1eb1d7a7b675eabdc694b97f4beac7c Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Sun, 19 Jul 2026 11:56:34 +0100 Subject: [PATCH 2/2] implementing ls and wc commands --- implement-shell-tools/ls/ls.js | 41 ++++++++++++++++++++++++++++++ implement-shell-tools/wc/wc.js | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 implement-shell-tools/ls/ls.js create mode 100644 implement-shell-tools/wc/wc.js diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..6fc7559b2 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,41 @@ +const fs = require("fs"); +const path = require("path"); +const args = process.argv.slice(2); + +let onePerLine = false; +let showAll = false; +let target = "."; + +for (const arg of args) { + + if (arg === "-1") { + onePerLine = true; + } + else if (arg === "-a") { + showAll = true; + } + else { + target = arg; + } +} + +const stats = fs.statSync(target); +if (stats.isFile()) { + console.log(path.basename(target)); +} else { + + let files = fs.readdirSync(target); + + if (!showAll) { + files = files.filter(file => !file.startsWith(".")); + } + if (onePerLine) { + + for (const file of files) { + console.log(file); + } + + } else { + console.log(files.join(" ")); + } +} \ No newline at end of file diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..2369d0a97 --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,46 @@ +const fs = require("fs"); + +const args = process.argv.slice(2); + +let showWords = false; +let showChars = false; +let showLines = false; +let fileNames = []; + +// Check flags and file names +for (let item of args) { + if (item === "-l") { + showLines = true; + } else if (item === "-w") { + showWords = true; + } else if (item === "-c") { + showChars = true; + } else { + fileNames.push(item); + } +} + +// If no flags are given, show everything +if (!showLines && !showWords && !showChars) { + showLines = true; + showWords = true; + showChars = true; +} + +for (let fileName of fileNames) { + let text = fs.readFileSync(fileName, "utf8"); + + let totalLines = text.split("\n").length - 1; + let totalWords = text.trim().split(/\s+/).length; + let totalChars = Buffer.byteLength(text); + + let output = ""; + + if (showLines) output += totalLines + " "; + if (showWords) output += totalWords + " "; + if (showChars) output += totalChars + " "; + + output += fileName; + + console.log(output); +} \ No newline at end of file