Skip to content

Commit d7f15ff

Browse files
committed
clean-implement-shell-tools
1 parent 4350f48 commit d7f15ff

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

implement-shell-tools/cat/cat.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// get CLI arguments
2+
const args = process.argv.slice(2);
3+
4+
// flags
5+
const showLines = args.includes("-l");
6+
const showWords = args.includes("-w");
7+
const showBytes = args.includes("-c");
8+
9+
// get files (remove flags)
10+
const files = args.filter((arg) => !arg.startsWith("-"));
11+
12+
// helper functions
13+
function countLines(text) {
14+
return text.split("\n").length - 1;
15+
}
16+
17+
function countWords(text) {
18+
return text.trim().split(/\s+/).filter(Boolean).length;
19+
}
20+
21+
function countBytes(text) {
22+
return Buffer.byteLength(text, "utf8");
23+
}
24+
25+
// loop through files
26+
for (let i = 0; i < files.length; i++) {
27+
const file = files[i];
28+
29+
try {
30+
const content = fs.readFileSync(file, "utf8");
31+
32+
const lines = countLines(content);
33+
const words = countWords(content);
34+
const bytes = countBytes(content);
35+
36+
let output = "";
37+
38+
// if no flag → show all
39+
if (!showLines && !showWords && !showBytes) {
40+
output = `${lines} ${words} ${bytes} ${file}`;
41+
} else {
42+
if (showLines) output += `${lines} `;
43+
if (showWords) output += `${words} `;
44+
if (showBytes) output += `${bytes} `;
45+
output += file;
46+
}
47+
48+
console.log(output.trim());
49+
} catch (err) {
50+
console.error(`wc: cannot open ${file}`);
51+
}
52+
}

implement-shell-tools/ls/ls.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const fs = require("fs");
2+
3+
// input from terminal
4+
const args = process.argv.slice(2);
5+
6+
// flags
7+
const showAll = args.includes("-a");
8+
9+
// get folder (default = current folder)
10+
const folder = args.filter((arg) => !arg.startsWith("-"))[0] || ".";
11+
12+
// read directory
13+
const files = fs.readdirSync(folder);
14+
15+
// loop and print
16+
for (let i = 0; i < files.length; i++) {
17+
const file = files[i];
18+
19+
// skip hidden files unless -a is used
20+
if (!showAll && file.startsWith(".")) {
21+
continue;
22+
}
23+
24+
console.log(file);
25+
}

implement-shell-tools/wc/wc.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const fs = require("fs");
2+
3+
// get CLI arguments
4+
const args = process.argv.slice(2);
5+
6+
// flags
7+
const showLines = args.includes("-l");
8+
const showWords = args.includes("-w");
9+
const showBytes = args.includes("-c");
10+
11+
// get files (remove flags)
12+
const files = args.filter((arg) => !arg.startsWith("-"));
13+
14+
// helper functions
15+
function countLines(text) {
16+
return text.split("\n").length - 1;
17+
}
18+
19+
function countWords(text) {
20+
return text.trim().split(/\s+/).filter(Boolean).length;
21+
}
22+
23+
function countBytes(text) {
24+
return Buffer.byteLength(text, "utf8");
25+
}
26+
27+
// loop through files
28+
for (let i = 0; i < files.length; i++) {
29+
const file = files[i];
30+
31+
try {
32+
const content = fs.readFileSync(file, "utf8");
33+
34+
const lines = countLines(content);
35+
const words = countWords(content);
36+
const bytes = countBytes(content);
37+
38+
let output = "";
39+
40+
// if no flag → show all
41+
if (!showLines && !showWords && !showBytes) {
42+
output = `${lines} ${words} ${bytes} ${file}`;
43+
} else {
44+
if (showLines) output += `${lines} `;
45+
if (showWords) output += `${words} `;
46+
if (showBytes) output += `${bytes} `;
47+
output += file;
48+
}
49+
50+
console.log(output.trim());
51+
} catch (err) {
52+
console.error(`wc: cannot open ${file}`);
53+
}
54+
}

0 commit comments

Comments
 (0)