From 41ca29ea4e0e1a51bb8156af84fc5e838149cbf2 Mon Sep 17 00:00:00 2001 From: pathywang Date: Mon, 13 Jul 2026 21:57:28 +0100 Subject: [PATCH 1/8] Implement basic cat command --- implement-shell-tools/cat/cat.js | 7 +++++++ 1 file changed, 7 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..aabbdcdc5 --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,7 @@ +const fs = require("fs"); + +const filename = process.argv[2]; + +const content = fs.readFileSync(filename, "utf8"); + +process.stdout.write(content); \ No newline at end of file From 296d98a5061ce88efb4a8f71a22409bf2f05a15f Mon Sep 17 00:00:00 2001 From: pathywang Date: Mon, 13 Jul 2026 22:13:15 +0100 Subject: [PATCH 2/8] support multiple files --- implement-shell-tools/cat/cat.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index aabbdcdc5..f98d5200c 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -1,7 +1,8 @@ const fs = require("fs"); -const filename = process.argv[2]; +const files = process.argv.slice(2); -const content = fs.readFileSync(filename, "utf8"); - -process.stdout.write(content); \ No newline at end of file +for (const file of files) { + const content = fs.readFileSync(file, "utf8"); + process.stdout.write(content); +} \ No newline at end of file From a25b21efd3ebaa88b9ca0b01851848815c2ec1d9 Mon Sep 17 00:00:00 2001 From: pathywang Date: Mon, 13 Jul 2026 22:23:18 +0100 Subject: [PATCH 3/8] add number line flag --- implement-shell-tools/cat/cat.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index f98d5200c..9d30800e9 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -1,8 +1,19 @@ const fs = require("fs"); -const files = process.argv.slice(2); +const args = process.argv.slice(2); -for (const file of files) { - const content = fs.readFileSync(file, "utf8"); +const numberLines = args[0] === "-n"; + +const filename = numberLines ? args[1] : args[0]; + +const content = fs.readFileSync(filename, "utf8"); + +const lines = content.split("\n"); + +if (numberLines) { + lines.forEach((line, index) => { + console.log(`${String(index + 1).padStart(6)}\t${line}`); + }); +} else { process.stdout.write(content); } \ No newline at end of file From 0c1f995543b8fef9d456d384536cd8b55858be15 Mon Sep 17 00:00:00 2001 From: pathywang Date: Mon, 13 Jul 2026 22:28:03 +0100 Subject: [PATCH 4/8] add number line for all files --- implement-shell-tools/cat/cat.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index 9d30800e9..d31339eb4 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -4,16 +4,21 @@ const args = process.argv.slice(2); const numberLines = args[0] === "-n"; -const filename = numberLines ? args[1] : args[0]; +const files = numberLines ? args.slice(1) : args; -const content = fs.readFileSync(filename, "utf8"); +let lineNumber = 1; -const lines = content.split("\n"); +for (const file of files) { + const content = fs.readFileSync(file, "utf8"); -if (numberLines) { - lines.forEach((line, index) => { - console.log(`${String(index + 1).padStart(6)}\t${line}`); - }); -} else { - process.stdout.write(content); + const lines = content.split("\n"); + + for (const line of lines) { + if (numberLines) { + console.log(`${String(lineNumber).padStart(6)}\t${line}`); + lineNumber++; + } else { + console.log(line); + } + } } \ No newline at end of file From 2213c390c77d2a54030ac88cf9eaf40e271c60cb Mon Sep 17 00:00:00 2001 From: pathywang Date: Mon, 13 Jul 2026 22:33:49 +0100 Subject: [PATCH 5/8] add number only non-blank lines --- implement-shell-tools/cat/cat.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index d31339eb4..980562a87 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -2,23 +2,32 @@ const fs = require("fs"); const args = process.argv.slice(2); -const numberLines = args[0] === "-n"; +const numberAll = args[0] === "-n"; +const numberNonBlank = args[0] === "-b"; -const files = numberLines ? args.slice(1) : args; +const files = (numberAll || numberNonBlank) + ? args.slice(1) + : args; let lineNumber = 1; for (const file of files) { const content = fs.readFileSync(file, "utf8"); - const lines = content.split("\n"); for (const line of lines) { - if (numberLines) { + + if (numberAll) { console.log(`${String(lineNumber).padStart(6)}\t${line}`); lineNumber++; + + } else if (numberNonBlank && line !== "") { + console.log(`${String(lineNumber).padStart(6)}\t${line}`); + lineNumber++; + } else { console.log(line); } + } } \ No newline at end of file From e66f6f35f806ad8413b4314136efb9f87b39df5a Mon Sep 17 00:00:00 2001 From: pathywang Date: Mon, 13 Jul 2026 23:35:27 +0100 Subject: [PATCH 6/8] add ls.js file in --- implement-shell-tools/ls/ls.js | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 implement-shell-tools/ls/ls.js diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..e555d6c72 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,57 @@ +const fs = require("fs"); +const path = require("path"); + +const args = process.argv.slice(2); + +let oneLine = false; +let showHidden = false; +let targets = []; + +for (const arg of args) { + if (arg === "-1") { + oneLine = true; + } else if (arg === "-a") { + showHidden = true; + } else { + targets.push(arg); + } +} + +if (targets.length === 0) { + targets.push("."); +} + +function listDirectory(directory) { + let files = fs.readdirSync(directory); + + if (!showHidden) { + files = files.filter(file => !file.startsWith(".")); + } + + files.sort(); + + if (oneLine) { + console.log(files.join("\n")); + } else { + console.log(files.join(" ")); + } +} + +function listTarget(target) { + try { + const stats = fs.statSync(target); + + if (stats.isDirectory()) { + listDirectory(target); + } else { + console.log(target); + } + + } catch (error) { + console.error(`ls: cannot access '${target}': No such file or directory`); + } +} + +for (const target of targets) { + listTarget(target); +} \ No newline at end of file From 81c7e761359d07055d0ad573585959c180a2196f Mon Sep 17 00:00:00 2001 From: pathywang Date: Mon, 13 Jul 2026 23:46:33 +0100 Subject: [PATCH 7/8] implement wc --- implement-shell-tools/wc/wc.js | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 implement-shell-tools/wc/wc.js diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..505499259 --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,57 @@ +const fs = require("fs"); + +const args = process.argv.slice(2); + +let countLines = false; +let countWords = false; +let countBytes = false; +let files = []; + +for (const 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 countFile(filename) { + const content = fs.readFileSync(filename, "utf8"); + + const lines = content.split("\n").length - 1; + const words = content.trim() === "" ? 0 : content.trim().split(/\s+/).length; + const bytes = Buffer.byteLength(content); + + let output = []; + + if (countLines) { + output.push(lines); + } + + if (countWords) { + output.push(words); + } + + if (countBytes) { + output.push(bytes); + } + + output.push(filename); + + console.log(output.join(" ")); +} + +for (const file of files) { + countFile(file); +} \ No newline at end of file From 0b2a455f3f1635b855ce67b8ed3168071b852cb4 Mon Sep 17 00:00:00 2001 From: pathywang Date: Sat, 18 Jul 2026 08:01:08 +0100 Subject: [PATCH 8/8] update --- implement-shell-tools/ls/ls.js | 1 - 1 file changed, 1 deletion(-) diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js index e555d6c72..483cd2e60 100644 --- a/implement-shell-tools/ls/ls.js +++ b/implement-shell-tools/ls/ls.js @@ -1,5 +1,4 @@ const fs = require("fs"); -const path = require("path"); const args = process.argv.slice(2);