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
41 changes: 41 additions & 0 deletions implement-shell-tools/cat/my-cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env node

const fs = require("fs");

const args = process.argv.slice(2);

let numberLines = false;
let numberNonEmpty = false;

if (args[0] === "-n") {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if someone passed both flags (e.g. node my-cat.js -b -n /some/file)? What do you think should happen?

numberLines = true;
args.shift();
} else if (args[0] === "-b") {
numberNonEmpty = true;
args.shift();
}
let count = 1;

args.forEach((file) => {
const content = fs.readFileSync(file, "utf-8");
const lines = content.split(/\r?\n/);


if (numberLines){
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is a bit repetitive - in both the numberLines case and the numberNonEmpty case you have the same code adding a prefix and incrementing the count.

Can you think how to avoid this, so we can see more easily what's the same between each branch, and what the differences are?

lines.forEach((line) => {
console.log(`${count} ${line}`);
count++;
});
} else if (numberNonEmpty){
lines.forEach((line) => {
if (line !== "") {
console.log(`${count} ${line}`);
count++;
} else {
console.log("");
}
});
} else {
console.log(content);
}
Comment on lines +38 to +40
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines aren't indented correctly

});
42 changes: 42 additions & 0 deletions implement-shell-tools/cat/my-cat.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

set -euo pipefail

number_lines=false
number_non_empty=false

# check for flags
if [ "${1:-}" = "-n" ]; then
number_lines=true
shift
elif [ "${1:-}" = "-b" ]; then
number_non_empty=true
shift
fi

count=1

# Loop through all files

for file in "$@"
do
while IFS= read -r line
do
if [ "$number_lines" = true ]; then
echo "$count $line"
count=$((count+1))

else if [ "$number_non_empty" = true ]; then
if [ -n "$line" ]; then
echo "$count $line"
count=$((count+1))
else
echo ""
fi

else
echo "$line"
fi
fi
done < "$file"
done
41 changes: 41 additions & 0 deletions implement-shell-tools/ls/my-ls.js
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I ran ls -1 -a sample-files vs node my-ls.js -1 -a sample-files I saw slightly different output - can you check what's causing the difference?

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env node

const fs = require("node:fs");
const path = require("node:path");

function parseArgs(args) {
let showAll = false;
let targetDir = ".";

for (const arg of args) {
if (arg === "-a") {
showAll = true;
} else if (!arg.startsWith("-")) {
targetDir = arg;
}
}

return { showAll, targetDir };
}

function listDirectory(dirPath, showAll) {
let files = fs.readdirSync(dirPath);

if (!showAll) {
files = files.filter(file => !file.startsWith("."));
}

return files.sort();
}

function main() {
const args = process.argv.slice(2);
const { showAll, targetDir } = parseArgs(args);

const files = listDirectory(targetDir, showAll);
console.log(files.join("\n"));
}

main();


34 changes: 34 additions & 0 deletions implement-shell-tools/ls/my-ls.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
set -euo pipefail

show_all=false

# handle -a
if [ "${1:-}" = "-a" ]; then
show_all=true
shift
fi

# directory (default current)
dir="."

if [ "${1:-}" != "" ]; then
dir="$1"
fi

#choose pattern
if [ "$show_all" = true ]; then
files="$dir"/.*
else
files="$dir"/*
fi
# loop
for file in $files
do
name="$(basename "$file")"

if [ "$name" = "." ] || [ "$name" = ".." ]; then
continue
fi
echo "$name"
done
67 changes: 67 additions & 0 deletions implement-shell-tools/wc/my-wc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env node

const fs = require("node:fs");

function countFile(filePath) {
const content = fs.readFileSync(filePath, "utf8");

const lines = content.split("\n").length - 1;
const words = content.trim() ? content.trim().split(/\s+/).length : 0;
const chars = Buffer.byteLength(content, "utf8");

return { lines, words, chars };
}

function main() {
const args = process.argv.slice(2);

let flag = null;
let files = [];

for (const arg of args) {
if (arg === "-l" || arg === "-w" || arg === "-c") {
flag = arg;
} else {
files.push(arg);
}
}

let totalLines = 0;
let totalWords = 0;
let totalChars = 0;

for (const file of files) {
const { lines, words, chars } = countFile(file);

totalLines += lines;
totalWords += words;
totalChars += chars;

if (flag === "-l") {
console.log(`${lines} ${file}`);
} else if (flag === "-w") {
console.log(`${words} ${file}`);
} else if (flag === "-c") {
console.log(`${chars} ${file}`);
} else {
console.log(`${lines} ${words} ${chars} ${file}`);
}
Comment on lines +40 to +48
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works if you specify exactly one flag, but the real wc supports multiple flags at the same time, e.g. wc -w -l /some/file - can you support this too?

}

if (files.length > 1) {
if (flag === "-l") {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite repetitive with the per-file printing - can you avoid that duplication?

console.log(`${totalLines} total`);
} else if (flag === "-w") {
console.log(`${totalWords} total`);
} else if (flag === "-c") {
console.log(`${totalChars} total`);
} else {
console.log(`${totalLines} ${totalWords} ${totalChars} total`);
}
}
}

main();



45 changes: 45 additions & 0 deletions implement-shell-tools/wc/my-wc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

set -euo pipefail

flag="all"

if [ "${1:-}" = "-l" ]; then
flag="l"
shift
elif [ "${1:-}" = "-w" ]; then
flag="w"
shift
elif [ "${1:-}" = "-c" ]; then
flag="c"
shift
fi

for file in "$@"
do
if [ -d "$file" ]; then
continue
fi

lines=0
words=0
chars=0

while IFS= read -r line
do
lines=$((lines + 1))
words=$((words + $(echo "$line" | wc -w)))
chars=$((chars + ${#line} + 1))
done <"$file"

if [ "$flag" = "l" ]; then
echo "$lines $file"
elif [ "$flag" = "w" ]; then
echo "$words $file"
elif [ "$flag" = "c" ]; then
echo "$chars $file"
else
echo "$lines $words $chars $file"
fi
done

Loading