Skip to content

Commit 17e7856

Browse files
committed
complete implement-shell-tools task
1 parent b16b7dd commit 17e7856

File tree

6 files changed

+136
-13
lines changed

6 files changed

+136
-13
lines changed

implement-shell-tools/cat/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Your task is to implement your own version of `cat`.
66

77
It must act the same as `cat` would, if run from the directory containing this README.md file, for the following command lines:
88

9-
* `cat sample-files/1.txt`
10-
* `cat -n sample-files/1.txt`
11-
* `cat sample-files/*.txt`
12-
* `cat -n sample-files/*.txt`
13-
* `cat -b sample-files/3.txt`
9+
- `cat sample-files/1.txt`
10+
- `cat -n sample-files/1.txt`
11+
- `cat sample-files/*.txt`
12+
- `cat -n sample-files/*.txt`
13+
- `cat -b sample-files/3.txt`
1414

1515
Matching any additional behaviours or flags are optional stretch goals.
1616

implement-shell-tools/cat/cat.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const fs = require("fs");
2+
3+
// input from terminal
4+
const args = process.argv.slice(2);
5+
6+
// check flags
7+
const hasN = args.includes("-n");
8+
const hasB = args.includes("-b");
9+
10+
// get only file names
11+
const files = args.filter((arg) => !arg.startsWith("-"));
12+
13+
let count = 1;
14+
15+
// go through each file
16+
for (let i = 0; i < files.length; i++) {
17+
const content = fs.readFileSync(files[i], "utf-8");
18+
19+
const lines = content.split("\n");
20+
21+
// go through each line
22+
for (let j = 0; j < lines.length; j++) {
23+
const line = lines[j];
24+
25+
// if -b (number non-empty lines)
26+
if (hasB) {
27+
if (line.trim() !== "") {
28+
console.log(count + " " + line);
29+
count++;
30+
} else {
31+
console.log("");
32+
}
33+
}
34+
// if -n (number all lines)
35+
else if (hasN) {
36+
console.log(count + " " + line);
37+
count++;
38+
}
39+
// no flags
40+
else {
41+
console.log(line);
42+
}
43+
}
44+
}

implement-shell-tools/ls/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Your task is to implement your own version of `ls`.
66

77
It must act the same as `ls` would, if run from the directory containing this README.md file, for the following command lines:
88

9-
* `ls -1`
10-
* `ls -1 sample-files`
11-
* `ls -1 -a sample-files`
9+
- `ls -1`
10+
- `ls -1 sample-files`
11+
- `ls -1 -a sample-files`
1212

1313
Matching any additional behaviours or flags are optional stretch goals.
1414

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/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Your task is to implement your own version of `wc`.
66

77
It must act the same as `wc` would, if run from the directory containing this README.md file, for the following command lines:
88

9-
* `wc sample-files/*`
10-
* `wc -l sample-files/3.txt`
11-
* `wc -w sample-files/3.txt`
12-
* `wc -c sample-files/3.txt`
13-
* `wc -l sample-files/*`
9+
- `wc sample-files/*`
10+
- `wc -l sample-files/3.txt`
11+
- `wc -w sample-files/3.txt`
12+
- `wc -c sample-files/3.txt`
13+
- `wc -l sample-files/*`
1414

1515
Matching any additional behaviours or flags are optional stretch goals.
1616

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)