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:
parent
7cc06460e9
commit
acc68c45fc
69
exercises/shell/solutions/directories_comparison.md
Normal file
69
exercises/shell/solutions/directories_comparison.md
Normal 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"
|
||||
|
||||
|
||||
```
|
Loading…
Reference in New Issue
Block a user