forked from root/public
Compare commits
2 Commits
master
...
DEV-4382-q
Author | SHA1 | Date |
---|---|---|
![]() |
47e9efa7d8 | 1 year ago |
![]() |
2d107bcd67 | 1 year ago |
2 changed files with 93 additions and 0 deletions
@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env bash |
||||
|
||||
# Unofficial Bash Strict Mode |
||||
set -euo pipefail |
||||
IFS=' |
||||
' |
||||
script_dir=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd) |
||||
|
||||
rm -rf better-cat-files |
||||
mkdir better-cat-files |
||||
cd better-cat-files |
||||
|
||||
for i in {1..3}; do |
||||
echo "This is line $i of file2" >> "file2.txt" |
||||
done |
||||
|
||||
for i in {1..10}; do |
||||
echo "This is line $i of file3" >> "file3.txt" |
||||
echo "This is line $i of commented" >> "commented.txt" |
||||
done |
||||
|
||||
echo "This is the content of file1" > "file1.txt" |
||||
echo "# commented" > "commented.txt" |
||||
|
||||
challenge() { |
||||
submitted=$(bash "$script_dir"/student/better-cat.sh "$@") |
||||
expected=$(bash "$script_dir"/solutions/better-cat.sh "$@") |
||||
diff <(echo "$submitted") <(echo "$expected") |
||||
} |
||||
|
||||
challenge "file1.txt" |
||||
challenge "file1.txt" "file2.txt" "file3.txt" |
||||
challenge *.txt |
||||
challenge |
||||
challenge -c "commented.txt" |
||||
challenge -l "file1.txt" |
||||
challenge -r "file1.txt" |
||||
challenge -cl "commented.txt" |
||||
# add test for file that doesn't exist |
||||
|
||||
cd .. |
||||
rm -rf better-cat-files |
@ -0,0 +1,51 @@
|
||||
#!/bin/bash |
||||
|
||||
while getopts "clr" opt; do |
||||
case $opt in |
||||
c) exclude_comments=true;; |
||||
l) show_length=true;; |
||||
r) show_recap=true;; |
||||
*) echo "Usage: $0 [-c] [-l] [-r] [file1] [file2] ..." >&2 |
||||
exit 1;; |
||||
esac |
||||
done |
||||
|
||||
shift $((OPTIND-1)) |
||||
|
||||
if [[ $# -eq 0 ]]; then |
||||
set -- * |
||||
fi |
||||
|
||||
line_count=0 |
||||
char_count=0 |
||||
|
||||
for file in "$@"; do |
||||
if [[ ! -e $file ]]; then |
||||
echo "$0: $file: No such file or directory" >&2 |
||||
continue |
||||
fi |
||||
|
||||
line_num=0 |
||||
while read -r line; do |
||||
line_num=$((line_num+1)) |
||||
if [[ $exclude_comments && $line == \#* ]]; then |
||||
continue |
||||
fi |
||||
if [[ $show_length ]]; then |
||||
echo "$line_num ($(( ${#line} ))): $line" |
||||
else |
||||
echo "$line_num: $line" |
||||
fi |
||||
char_count=$((char_count+${#line})) |
||||
done < "$file" |
||||
|
||||
line_count=$((line_count+line_num)) |
||||
done |
||||
|
||||
if [[ $show_recap ]]; then |
||||
echo "Total: $line_count lines, $char_count characters" |
||||
fi |
||||
|
||||
if [[ $line_count -eq 0 ]]; then |
||||
exit 1 |
||||
fi |
Loading…
Reference in new issue