Add solution for directories_comparison exercise (#241)

* add solution

* Update question language

* Update question language

* Print "No difference" if no difference between dirs

Co-authored-by: Luke LeVasseur <llevasseur@flexport.com>
This commit is contained in:
Luke LeVasseur 2022-09-02 07:53:01 -04:00 committed by GitHub
parent 7cc06460e9
commit acc68c45fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,69 @@
## Directories Comparison
### Objectives
1. You are given two directories as arguments and the output should be any difference between the two directories
### Solution
```
#!/usr/bin/env bash
help () {
echo "Usage: compare <filename1> <filename2>"
echo
}
validate_args() {
# Ensure that 2 arguments are passed
if [ $# != 2 ]
then
help
exit 1
fi
i=1
for dir in "$@"
do
# Validate existence of directories
if [ ! -d "$dir" ]
then
echo "Directory $dir does not exist"
exit 1
fi
echo "Directory $i: $dir"
i=$((i + 1))
done
echo
}
compare() {
echo "Comparing directories..."
echo
diff -r "$1" "$2"
if [ $? -eq 0 ]
then
echo "No difference"
fi
exit 0
}
while getopts ":h" option; do
case $option in
h) # display Help
help
exit 0;;
\?) # invalid option
echo "Error: Invalid option"
exit 1;;
esac
done
validate_args "$@"
compare "$1" "$2"
```