Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions implement-shell-tools/cat/cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// get CLI arguments
const args = process.argv.slice(2);

// flags
const showLines = args.includes("-l");
const showWords = args.includes("-w");
const showBytes = args.includes("-c");

// get files (remove flags)
const files = args.filter((arg) => !arg.startsWith("-"));

// helper functions
function countLines(text) {
return text.split("\n").length - 1;
}

function countWords(text) {
return text.trim().split(/\s+/).filter(Boolean).length;
}

function countBytes(text) {
return Buffer.byteLength(text, "utf8");
}

// loop through files
for (let i = 0; i < files.length; i++) {
const file = files[i];

try {
const content = fs.readFileSync(file, "utf8");

const lines = countLines(content);
const words = countWords(content);
const bytes = countBytes(content);

let output = "";

// if no flag → show all
if (!showLines && !showWords && !showBytes) {
output = `${lines} ${words} ${bytes} ${file}`;
} else {
if (showLines) output += `${lines} `;
if (showWords) output += `${words} `;
if (showBytes) output += `${bytes} `;
output += file;
}

console.log(output.trim());
} catch (err) {
console.error(`wc: cannot open ${file}`);
}
}
25 changes: 25 additions & 0 deletions implement-shell-tools/ls/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fs = require("fs");

// input from terminal
const args = process.argv.slice(2);

// flags
const showAll = args.includes("-a");

// get folder (default = current folder)
const folder = args.filter((arg) => !arg.startsWith("-"))[0] || ".";

// read directory
const files = fs.readdirSync(folder);

// loop and print
for (let i = 0; i < files.length; i++) {
const file = files[i];

// skip hidden files unless -a is used
if (!showAll && file.startsWith(".")) {
continue;
}

console.log(file);
}
54 changes: 54 additions & 0 deletions implement-shell-tools/wc/wc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const fs = require("fs");

// get CLI arguments
const args = process.argv.slice(2);

// flags
const showLines = args.includes("-l");
const showWords = args.includes("-w");
const showBytes = args.includes("-c");

// get files (remove flags)
const files = args.filter((arg) => !arg.startsWith("-"));

// helper functions
function countLines(text) {
return text.split("\n").length - 1;
}

function countWords(text) {
return text.trim().split(/\s+/).filter(Boolean).length;
}

function countBytes(text) {
return Buffer.byteLength(text, "utf8");
}

// loop through files
for (let i = 0; i < files.length; i++) {
const file = files[i];

try {
const content = fs.readFileSync(file, "utf8");

const lines = countLines(content);
const words = countWords(content);
const bytes = countBytes(content);

let output = "";

// if no flag → show all
if (!showLines && !showWords && !showBytes) {
output = `${lines} ${words} ${bytes} ${file}`;
} else {
if (showLines) output += `${lines} `;
if (showWords) output += `${words} `;
if (showBytes) output += `${bytes} `;
output += file;
}

console.log(output.trim());
} catch (err) {
console.error(`wc: cannot open ${file}`);
}
}
Loading